Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update package to work with go mod. #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


.PHONY: data
data:
go get -u github.com/a-urth/go-bindata
go-bindata -pkg data -o data/data.go ./data/...
283 changes: 283 additions & 0 deletions data/data.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module github.com/AntoineAugusti/moduluschecking

require (
github.com/a-urth/go-bindata v0.0.0-20180209162145-df38da164efc // indirect
github.com/stretchr/testify v1.3.0
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/a-urth/go-bindata v0.0.0-20180209162145-df38da164efc h1:eXJIPWW4y4xjPWda/7ruw5PRsfcbzKTL9EhNQrBRsOU=
github.com/a-urth/go-bindata v0.0.0-20180209162145-df38da164efc/go.mod h1:D0SbCgK4DQtSNzDQzfek273VqkCnHdFCd+q2ueHGRiE=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
36 changes: 15 additions & 21 deletions parsers/fileParser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package parsers

import (
"bufio"
"os"
"path"
"bytes"
"strings"

"github.com/AntoineAugusti/moduluschecking/data"
"github.com/AntoineAugusti/moduluschecking/helpers"
m "github.com/AntoineAugusti/moduluschecking/models"
)
Expand All @@ -20,10 +20,10 @@ type LineRecord struct {

// A parser that reads data from the filesystem.
type FileParser struct {
// The path to the weights file.
weightsPath string
// The path to the substitutions file for sort codes.
substitutionsPath string
// The weights file
weightsBytes []byte
// The substitutions file.
substitutionsBytes []byte
// The actual weights for each sort code.
weights map[string]m.SortCodeData
}
Expand All @@ -33,7 +33,7 @@ func (fp FileParser) Substitutions() map[string]string {
substitutions := make(map[string]string)

jobs := make(chan LineRecord)
go readFile(fp.substitutionsPath, jobs)
go readFile(fp.substitutionsBytes, jobs)

for lineRecord := range jobs {
fields := strings.Split(lineRecord.content, " ")
Expand All @@ -49,7 +49,7 @@ func (fp FileParser) Weights() map[string]m.SortCodeData {
jobs := make(chan LineRecord)
results := make(chan m.SortCodeRange)

go readFile(fp.weightsPath, jobs)
go readFile(fp.weightsBytes, jobs)
go parseWeightsLine(jobs, results)

// Process all the sort code ranges
Expand Down Expand Up @@ -135,16 +135,10 @@ func parseWeightsLine(jobs <-chan LineRecord, results chan<- m.SortCodeRange) {
}

// Read a file and put the content in a channel.
func readFile(path string, jobs chan<- LineRecord) {
file, err := os.Open(path)
if err != nil {
panic(err)
}
defer file.Close()
func readFile(file []byte, jobs chan<- LineRecord) {

lineNumber := 1

scanner := bufio.NewScanner(file)
scanner := bufio.NewScanner(bytes.NewReader(file))
for scanner.Scan() {
jobs <- LineRecord{
content: scanner.Text(),
Expand All @@ -160,12 +154,12 @@ func readFile(path string, jobs chan<- LineRecord) {
// Create a new instance of a file parser that satisfies
// the parser interface.
func CreateFileParser() m.Parser {
goPath := path.Clean(os.Getenv("GOPATH"))
dataPath := goPath + "/src/github.com/AntoineAugusti/moduluschecking/data/"
weights := data.MustAsset("data/weights.txt")
substitutions := data.MustAsset("data/substitutions.txt")

return FileParser{
weightsPath: dataPath + "weights.txt",
substitutionsPath: dataPath + "substitutions.txt",
weights: make(map[string]m.SortCodeData),
weightsBytes: weights,
substitutionsBytes: substitutions,
weights: make(map[string]m.SortCodeData),
}
}