-
Notifications
You must be signed in to change notification settings - Fork 271
/
unique_id_field.go
70 lines (56 loc) · 1.5 KB
/
unique_id_field.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package identifiers
import (
"fmt"
"github.com/jitsucom/jitsu/server/jsonutils"
)
//UniqueID is a struct for extracting unique ID from objects
type UniqueID struct {
jsonPath jsonutils.JSONPath
}
//NewUniqueID returns new UniqueID instance
func NewUniqueID(uniqueIDField string) *UniqueID {
return &UniqueID{jsonPath: jsonutils.NewJSONPath(uniqueIDField)}
}
//Extract returns extracted global unique ID from input object
func (uid *UniqueID) Extract(obj map[string]interface{}) string {
if obj == nil {
return ""
}
value, ok := uid.jsonPath.Get(obj)
if ok {
return fmt.Sprint(value)
}
value, ok = obj[uid.GetFlatFieldName()]
if ok {
return fmt.Sprint(value)
}
return ""
}
//ExtractAndRemove returns extracted global unique ID from input object and remove it from the objects
func (uid *UniqueID) ExtractAndRemove(obj map[string]interface{}) string {
if obj == nil {
return ""
}
value, ok := uid.jsonPath.GetAndRemove(obj)
if ok {
return fmt.Sprint(value)
}
value, ok = obj[uid.GetFlatFieldName()]
if ok {
delete(obj, uid.GetFlatFieldName())
return fmt.Sprint(value)
}
return ""
}
//Set puts ID into the object
func (uid *UniqueID) Set(obj map[string]interface{}, id string) error {
return uid.jsonPath.Set(obj, id)
}
//GetFlatFieldName returns field name (/key1/key2 -> key1_key2)
func (uid *UniqueID) GetFlatFieldName() string {
return uid.jsonPath.FieldName()
}
//GetFieldName returns field name as is
func (uid *UniqueID) GetFieldName() string {
return uid.jsonPath.String()
}