diff options
author | Martin Czygan <martin.czygan@gmail.com> | 2021-05-28 23:35:15 +0200 |
---|---|---|
committer | Martin Czygan <martin.czygan@gmail.com> | 2021-05-28 23:35:15 +0200 |
commit | 22e7e3373a0a2ee7170b65f4ceaaf5d822778feb (patch) | |
tree | e0985f2eb80f8fab397b708538ba326876844709 /skate | |
parent | a1ca3f5f468a022442c330ef3a63b7bfc217f736 (diff) | |
download | refcat-22e7e3373a0a2ee7170b65f4ceaaf5d822778feb.tar.gz refcat-22e7e3373a0a2ee7170b65f4ceaaf5d822778feb.zip |
zippy: simplify cut
Diffstat (limited to 'skate')
-rw-r--r-- | skate/zippy.go | 61 | ||||
-rw-r--r-- | skate/zippy_test.go | 2 |
2 files changed, 34 insertions, 29 deletions
diff --git a/skate/zippy.go b/skate/zippy.go index 146d2e7..43fc911 100644 --- a/skate/zippy.go +++ b/skate/zippy.go @@ -1,3 +1,5 @@ +// This file contains various "reducers", e.g. functions, that take two data +// streams and will apply a computation on matched groups. package skate import ( @@ -10,17 +12,15 @@ import ( json "github.com/segmentio/encoding/json" ) -// This file contains the two-stream (zippy) matchers. - -// groupLogf logs a message and a serialized group. +// groupLogf logs a message alongsize a serialized group. func groupLogf(g *zipkey.Group, s string, vs ...interface{}) { log.Printf(s, vs...) b, _ := json.Marshal(g) log.Println(string(b)) } -// ZippyExact takes a release and refs reader (tsv, with ident, key, doc) -// and assigns a fixed match result. XXX: allow empty keys +// ZippyExact takes a release and refs reader (key, doc) and assigns a fixed +// match result. XXX: allow empty keys func ZippyExact(releases, refs io.Reader, matchResult MatchResult, w io.Writer) error { var ( enc = json.NewEncoder(w) @@ -39,12 +39,12 @@ func ZippyExact(releases, refs io.Reader, matchResult MatchResult, w io.Writer) if len(g.G0) == 0 || len(g.G1) == 0 { return nil } - if target, err = stringToRelease(cut(g.G0[0], "\t", 2)); err != nil { + if target, err = stringToRelease(Cut(g.G0[0], 2)); err != nil { groupLogf(g, "[skip] failed to parse release: %v", err) return nil } for _, line := range g.G1 { - if ref, err = stringToRef(cut(line, "\t", 2)); err != nil { + if ref, err = stringToRef(Cut(line, 2)); err != nil { groupLogf(g, "[skip] failed to parse ref: %v", err) continue } @@ -71,8 +71,8 @@ func ZippyExact(releases, refs io.Reader, matchResult MatchResult, w io.Writer) return zipper.Run() } -// ZippyExactWiki takes a release and wiki reader (tsv, with key, doc) -// and assigns a fixed match result. +// ZippyExactWiki takes a release and wiki reader (key, doc) and assigns a +// fixed match result. func ZippyExactWiki(releases, wiki io.Reader, mr MatchResult, w io.Writer) error { var ( enc = json.NewEncoder(w) @@ -86,11 +86,11 @@ func ZippyExactWiki(releases, wiki io.Reader, mr MatchResult, w io.Writer) error if len(g.G0) == 0 || len(g.G1) == 0 { return nil } - if target, err = stringToRelease(cut(g.G0[0], "\t", 2)); err != nil { + if target, err = stringToRelease(Cut(g.G0[0], 2)); err != nil { return err } for _, line := range g.G1 { - if wiki, err = stringToWiki(cut(line, "\t", 2)); err != nil { + if wiki, err = stringToWiki(Cut(line, 2)); err != nil { return err } var bref BiblioRef @@ -129,11 +129,11 @@ func ZippyVerifyRefs(releases, refs io.Reader, w io.Writer) error { if len(g.G0) == 0 || len(g.G1) == 0 { return nil } - if pivot, err = stringToRelease(cut(g.G0[0], "\t", 2)); err != nil { + if pivot, err = stringToRelease(Cut(g.G0[0], 2)); err != nil { return err } for _, line := range g.G1 { - if re, err = stringToRelease(cut(line, "\t", 2)); err != nil { + if re, err = stringToRelease(Cut(line, 2)); err != nil { return err } result := Verify(pivot, re) @@ -175,11 +175,11 @@ func ZippyVerifyRefsOpenLibrary(olr, refs io.Reader, w io.Writer) error { return nil } // We take a single edition from OL; XXX: might want to link to work. - if pivot, err = stringToRelease(cut(g.G0[0], "\t", 2)); err != nil { + if pivot, err = stringToRelease(Cut(g.G0[0], 2)); err != nil { return err } for _, line := range g.G1 { - if re, err = stringToRelease(cut(line, "\t", 2)); err != nil { + if re, err = stringToRelease(Cut(line, 2)); err != nil { return err } // The refs have a container name, but not a title, but here we @@ -189,7 +189,7 @@ func ZippyVerifyRefsOpenLibrary(olr, refs io.Reader, w io.Writer) error { fmt.Printf("%s\t%s\t%s\t%s\t%s\n", result.Status.Short(), result.Reason.Short(), - cut(g.G0[0], "\t", 1), + CutSep(g.G0[0], "\t", 1), pivot.Title, re.Title) } @@ -200,11 +200,27 @@ func ZippyVerifyRefsOpenLibrary(olr, refs io.Reader, w io.Writer) error { return zipper.Run() } +// Cut returns a specific column (1-indexed, like CutSep) from a tabular +// file, returns empty string if column is invalid. +func Cut(line string, column int) string { + return CutSep(line, "\t", column) +} + +// CutSep allows to specify a separator. +func CutSep(line, sep string, column int) string { + parts := strings.Split(strings.TrimSpace(line), sep) + if len(parts) < column { + return "" + } else { + return parts[column-1] + } +} + // makeKeyFunc creates a function that can be used as keyFunc, selecting a // column from fields separated by sep; column is 1-indexed. func makeKeyFunc(sep string, column int) func(string) (string, error) { return func(s string) (string, error) { - if k := cut(s, sep, column); k == "" { + if k := CutSep(s, sep, column); k == "" { return k, fmt.Errorf("cannot get key: %s", s) } else { return k, nil @@ -212,17 +228,6 @@ func makeKeyFunc(sep string, column int) func(string) (string, error) { } } -// cut returns a specific column (1-indexed, like cut) from a tabular -// file, returns empty string if column is invalid. -func cut(line, sep string, column int) string { - parts := strings.Split(strings.TrimSpace(line), sep) - if len(parts) < column { - return "" - } else { - return parts[column-1] - } -} - func stringToRelease(s string) (r *Release, err error) { err = json.Unmarshal([]byte(s), &r) return diff --git a/skate/zippy_test.go b/skate/zippy_test.go index c966d67..1448472 100644 --- a/skate/zippy_test.go +++ b/skate/zippy_test.go @@ -17,7 +17,7 @@ func TestLineColumn(t *testing.T) { {"1 2 3", "\t", 1, "1 2 3"}, } for _, c := range cases { - result := cut(c.line, c.sep, c.column) + result := CutSep(c.line, c.sep, c.column) if result != c.result { t.Fatalf("got %v, want %v", result, c.result) } |