aboutsummaryrefslogtreecommitdiffstats
path: root/skate/map.go
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/map.go
parentf9ec7d506d7648a2aa9ce0d4d9d79023e29a2b45 (diff)
downloadrefcat-1e111eed2500db175ac4c2132822276485e4b4a4.tar.gz
refcat-1e111eed2500db175ac4c2132822276485e4b4a4.zip
simplify fixed field extractor
Diffstat (limited to 'skate/map.go')
-rw-r--r--skate/map.go35
1 files changed, 4 insertions, 31 deletions
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