From 1cf6d135f274ce79b09a0396367186132bc178f3 Mon Sep 17 00:00:00 2001 From: Martin Czygan Date: Sat, 24 Apr 2021 14:18:11 +0200 Subject: wip: mapper with arbitrary number of fields --- skate/cluster.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'skate/cluster.go') 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)) -- cgit v1.2.3