Skip to content

Commit

Permalink
Rename package files (#5)
Browse files Browse the repository at this point in the history
* Move example into test file

* Rename decoder receiver

* Rename package files
  • Loading branch information
fmenezes authored Jul 21, 2020
1 parent 0c55fca commit 34e56bc
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 105 deletions.
85 changes: 0 additions & 85 deletions codeowners.go
Original file line number Diff line number Diff line change
@@ -1,90 +1,5 @@
// Package codeowners provides funcionality to evaluate CODEOWNERS file.
package codeowners // import "github.com/fmenezes/codeowners"

import (
"bufio"
"io"
"strings"
)

// DefaultLocations provides default locations for the CODEOWNERS file
var DefaultLocations = [...]string{"CODEOWNERS", "docs/CODEOWNERS", ".github/CODEOWNERS"}

// Decoder providers functionality to read CODEOWNERS data
type Decoder struct {
scanner *bufio.Scanner
line string
done bool
}

// NewDecoder generates a new CodeOwnersScanner instance. The reader should be a reader containing the contents of the CODEOWNERS file
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{
scanner: bufio.NewScanner(r),
line: "",
done: false,
}
}

// peek will scan the next line
func (s *Decoder) peek() {
if !s.scanner.Scan() {
s.done = true
return
}
line := sanitiseLine(s.scanner.Text())
s.line = line
if len(s.line) == 0 && !s.done {
s.peek()
}
}

// sanitiseLine removes all empty space and comments from a given line
func sanitiseLine(line string) string {
i := strings.Index(line, "#")
if i >= 0 {
line = line[:i]
}
return strings.Trim(line, " ")
}

// More returns true if there are available CODEOWNERS lines to be scanned.
// And also advances to the next line.
func (s *Decoder) More() bool {
s.peek()
return !s.done
}

// Token parses the next available line in the CODEOWNERS file.
// If More was never called it will return an empty token.
// After end of file Token will always return the last line.
func (s *Decoder) Token() Token {
line := strings.ReplaceAll(s.line, "\\ ", "\\s")

data := strings.Split(line, " ")

for i := range data {
data[i] = strings.ReplaceAll(data[i], "\\s", " ")
}

return Token{
path: data[0],
owners: data[1:],
}
}

// Token providers reading capabilities for every CODEOWNERS line
type Token struct {
path string
owners []string
}

// Path returns the file path pattern
func (t Token) Path() string {
return t.path
}

// Owners returns the owners
func (t Token) Owners() []string {
return t.owners
}
86 changes: 86 additions & 0 deletions decoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package codeowners

import (
"bufio"
"io"
"strings"
)

// Decoder providers functionality to read CODEOWNERS data
type Decoder struct {
scanner *bufio.Scanner
line string
done bool
}

// NewDecoder generates a new CodeOwnersScanner instance. The reader should be a reader containing the contents of the CODEOWNERS file
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{
scanner: bufio.NewScanner(r),
line: "",
done: false,
}
}

// peek will scan the next line
func (d *Decoder) peek() {
if !d.scanner.Scan() {
d.done = true
return
}
line := sanitiseLine(d.scanner.Text())
d.line = line
if len(d.line) == 0 && !d.done {
d.peek()
}
}

// sanitiseLine removes all empty space and comments from a given line
func sanitiseLine(line string) string {
i := strings.Index(line, "#")
if i >= 0 {
line = line[:i]
}
return strings.Trim(line, " ")
}

// More returns true if there are available CODEOWNERS lines to be scanned.
// And also advances to the next line.
func (d *Decoder) More() bool {
d.peek()
return !d.done
}

// Token parses the next available line in the CODEOWNERS file.
// If More was never called it will return an empty token.
// After end of file Token will always return the last line.
func (d *Decoder) Token() Token {
line := strings.ReplaceAll(d.line, "\\ ", "\\s")

data := strings.Split(line, " ")

for i := range data {
data[i] = strings.ReplaceAll(data[i], "\\s", " ")
}

return Token{
path: data[0],
owners: data[1:],
}
}

// Token providers reading capabilities for every CODEOWNERS line
type Token struct {
path string
owners []string
}

// Path returns the file path pattern
func (t Token) Path() string {
return t.path
}

// Owners returns the owners
func (t Token) Owners() []string {
return t.owners
}
16 changes: 16 additions & 0 deletions codeowners_test.go → decoder_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package codeowners_test

import (
"fmt"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -121,3 +122,18 @@ func TestMoreNotCalled(t *testing.T) {
t.Error("Owners should be empty")
}
}

func ExampleDecoder() {
decoder := codeowners.NewDecoder(strings.NewReader(`* test@example.org
filepattern @owner`))
for decoder.More() {
token := decoder.Token()
fmt.Printf("File Pattern: %s\n", token.Path())
fmt.Printf("Owners: %v\n", token.Owners())
}
// Output:
// File Pattern: *
// Owners: [test@example.org]
// File Pattern: filepattern
// Owners: [@owner]
}
20 changes: 0 additions & 20 deletions example_codeowners_test.go

This file was deleted.

0 comments on commit 34e56bc

Please sign in to comment.