aboutsummaryrefslogtreecommitdiffstats
path: root/skate/zippy.go
diff options
context:
space:
mode:
authorMartin Czygan <martin.czygan@gmail.com>2021-06-14 22:42:47 +0200
committerMartin Czygan <martin.czygan@gmail.com>2021-06-14 22:42:47 +0200
commit23910bdad2983a4b79aa2b5a1d36d3d0822cb06d (patch)
treef55445b87ea1332f9c5773c46babc2be07ec91e0 /skate/zippy.go
parent40d928650c26860481ba92f386d324b173c1b420 (diff)
downloadrefcat-23910bdad2983a4b79aa2b5a1d36d3d0822cb06d.tar.gz
refcat-23910bdad2983a4b79aa2b5a1d36d3d0822cb06d.zip
add cut batch helper
Diffstat (limited to 'skate/zippy.go')
-rw-r--r--skate/zippy.go25
1 files changed, 18 insertions, 7 deletions
diff --git a/skate/zippy.go b/skate/zippy.go
index 70d42fc..b4f2804 100644
--- a/skate/zippy.go
+++ b/skate/zippy.go
@@ -328,19 +328,22 @@ func ZippyBrefAugment(bref, raw io.Reader, w io.Writer) error {
// First, iterate over all matches and sort out duplicates, e.g.
// docs that have the same source and target id.
- matched, err := uniqueMatches(g.G0)
+ matched, err := uniqueMatches(CutBatch(g.G0, 2))
if err != nil {
return err
}
var refs = make([]*Ref, len(g.G1))
for i := 0; i < len(refs); i++ {
- var ref Ref
- if err := json.Unmarshal([]byte(g.G1[i]), &ref); err != nil {
+ var (
+ data []byte = []byte(Cut(g.G1[i], 2))
+ ref Ref
+ )
+ if err := json.Unmarshal(data, &ref); err != nil {
return err
}
refs[i] = &ref
}
- matchedRefsExtend(matched, refs) // XXX: Still duplicates (?)
+ matchedRefsExtend(matched, refs)
for _, bref := range matched {
if err := enc.Encode(bref); err != nil {
return err
@@ -419,13 +422,21 @@ func uniqueMatches(docs []string) (result []*BiblioRef, err error) {
return result, nil
}
-// Cut returns a specific column (1-indexed, like CutSep) from a tabular
-// file, returns empty string if column is invalid.
+// CutBatch runs Cut over a list of lines.
+func CutBatch(lines []string, column int) (result []string) {
+ for _, line := range lines {
+ result = append(result, Cut(line, column))
+ }
+ return result
+}
+
+// Cut returns a specific column (1-indexed) from a line, 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.
+// CutSep allows to specify a separator, column is 1-indexed.
func CutSep(line, sep string, column int) string {
parts := strings.Split(strings.TrimSpace(line), sep)
if len(parts) < column {