aboutsummaryrefslogtreecommitdiffstats
path: root/skate
diff options
context:
space:
mode:
authorMartin Czygan <martin.czygan@gmail.com>2021-04-28 23:28:48 +0200
committerMartin Czygan <martin.czygan@gmail.com>2021-04-28 23:28:51 +0200
commit1e111eed2500db175ac4c2132822276485e4b4a4 (patch)
tree35981d939628f3a423898acd60e545e4574206e8 /skate
parentf9ec7d506d7648a2aa9ce0d4d9d79023e29a2b45 (diff)
downloadrefcat-1e111eed2500db175ac4c2132822276485e4b4a4.tar.gz
refcat-1e111eed2500db175ac4c2132822276485e4b4a4.zip
simplify fixed field extractor
Diffstat (limited to 'skate')
-rw-r--r--skate/go.mod1
-rw-r--r--skate/go.sum6
-rw-r--r--skate/map.go35
3 files changed, 11 insertions, 31 deletions
diff --git a/skate/go.mod b/skate/go.mod
index bd2093a..49ef5d2 100644
--- a/skate/go.mod
+++ b/skate/go.mod
@@ -9,5 +9,6 @@ require (
github.com/miku/parallel v0.0.0-20210205190127-d1fa15dcea0c
github.com/nsf/jsondiff v0.0.0-20210303162244-6ea32392771e
github.com/segmentio/encoding v0.2.17
+ github.com/tidwall/gjson v1.7.5
golang.org/x/text v0.3.5
)
diff --git a/skate/go.sum b/skate/go.sum
index 61baedb..a186bcd 100644
--- a/skate/go.sum
+++ b/skate/go.sum
@@ -12,6 +12,12 @@ github.com/nsf/jsondiff v0.0.0-20210303162244-6ea32392771e h1:S+/ptYdZtpK/MDstwC
github.com/nsf/jsondiff v0.0.0-20210303162244-6ea32392771e/go.mod h1:uFMI8w+ref4v2r9jz+c9i1IfIttS/OkmLfrk1jne5hs=
github.com/segmentio/encoding v0.2.17 h1:cgfmPc44u1po1lz5bSgF00gLCROBjDNc7h+H7I20zpc=
github.com/segmentio/encoding v0.2.17/go.mod h1:7E68jTSWMnNoYhHi1JbLd7NBSB6XfE4vzqhR88hDBQc=
+github.com/tidwall/gjson v1.7.5 h1:zmAN/xmX7OtpAkv4Ovfso60r/BiCi5IErCDYGNJu+uc=
+github.com/tidwall/gjson v1.7.5/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
+github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
+github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
+github.com/tidwall/pretty v1.1.0 h1:K3hMW5epkdAVwibsQEfR/7Zj0Qgt4DxtNumTq/VloO8=
+github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
golang.org/x/text v0.3.5 h1:i6eZZ+zk0SOf0xgBpEpPD18qWcJda6q1sxt3S0kzyUQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
diff --git a/skate/map.go b/skate/map.go
index 5584371..9d3c98d 100644
--- a/skate/map.go
+++ b/skate/map.go
@@ -5,10 +5,10 @@ import (
"errors"
"reflect"
"runtime"
- "strconv"
"strings"
json "github.com/segmentio/encoding/json"
+ "github.com/tidwall/gjson"
)
var (
@@ -47,7 +47,7 @@ func (f Mapper) AsTSV(p []byte) ([]byte, error) {
if err != nil {
return nil, err
}
- return append(bytes.Join(fields, bTab), bNewline...), nil
+ return bytes.Join(fields, bTab), nil
}
// WithPrefix adds a given prefix to the first element.
@@ -84,36 +84,9 @@ func Identity(p []byte) ([][]byte, error) {
// CreateFixedMapper extract the value from a given fixed top level json key.
// Returns a function that maps doc to (v, doc).
func CreateFixedMapper(field string) Mapper {
- if field == "" {
- return func(p []byte) ([][]byte, error) {
- return nil, ErrMissingFieldName
- }
- }
f := func(p []byte) ([][]byte, error) {
- var (
- doc map[string]interface{}
- v interface{}
- ok bool
- key []byte
- )
- if err := json.Unmarshal(p, &doc); err != nil {
- return nil, err
- }
- if v, ok = doc[field]; !ok {
- return nil, nil
- }
- switch w := v.(type) {
- case string:
- key = []byte(w)
- case int:
- key = []byte(strconv.Itoa(w))
- case int64:
- key = []byte(strconv.Itoa(int(w)))
- case float64:
- key = []byte(strconv.FormatFloat(w, 'f', 52, 64))
- default:
- return nil, nil
- }
+ result := gjson.GetBytes(p, field)
+ key := []byte(result.String())
return [][]byte{key, p}, nil
}
return f