aboutsummaryrefslogtreecommitdiffstats
path: root/skate/cluster.go
diff options
context:
space:
mode:
authorMartin Czygan <martin.czygan@gmail.com>2021-04-24 00:49:10 +0200
committerMartin Czygan <martin.czygan@gmail.com>2021-04-24 00:49:10 +0200
commit19aa629f7f4c6345769784147493e2598d746dd3 (patch)
treeb652cf72872fc3183c6fdf78a4814f0180eff735 /skate/cluster.go
parent1ab13de4c6fff5db11d8d39936b4513196a56cee (diff)
downloadrefcat-19aa629f7f4c6345769784147493e2598d746dd3.tar.gz
refcat-19aa629f7f4c6345769784147493e2598d746dd3.zip
skate: move out helper function
Diffstat (limited to 'skate/cluster.go')
-rw-r--r--skate/cluster.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/skate/cluster.go b/skate/cluster.go
index 7fc4e1b..3421a0b 100644
--- a/skate/cluster.go
+++ b/skate/cluster.go
@@ -1,6 +1,7 @@
package skate
import (
+ "fmt"
"regexp"
"strings"
@@ -109,6 +110,30 @@ func KeyTitleSandcrawler(p []byte) (ident string, key string, err error) {
return ident, sandcrawlerSlugify(key), nil
}
+// CreateFixedFieldFunc creates an extractor function given a json path.
+// Currently only top level key is supported.
+func CreateFixedFieldFunc(path string) IdentifierKeyFunc {
+ f := func(p []byte) (ident string, key string, err error) {
+ var doc map[string]interface{}
+ if err = json.Unmarshal(p, &doc); err != nil {
+ return
+ }
+ v, ok := doc[path]
+ if !ok {
+ return "", "", nil
+ }
+ switch t := v.(type) {
+ case string:
+ return "", t, nil
+ case int, int64, float32, float64:
+ return "", fmt.Sprintf("%v", t), nil
+ default:
+ return "", "", nil
+ }
+ }
+ return f
+}
+
// sandcrawlerSlugify normalizes a string.
func sandcrawlerSlugify(s string) string {
slug := strings.ToLower(strings.TrimSpace(s))