-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreferenced_value.go
65 lines (56 loc) · 1.63 KB
/
referenced_value.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
package neatly
import (
"fmt"
"github.com/viant/toolbox/data"
"strings"
)
type referenceValues map[string]*referenceValue
func (v *referenceValues) CheckUnused() error {
var unused = make([]string, 0)
for k, value := range *v {
if !value.Used {
unused = append(unused, k)
}
}
if len(unused) == 0 {
return nil
}
return fmt.Errorf("Unresolved references: '%v' ", strings.Join(unused, ","))
}
func (v *referenceValues) Add(tagName string, field *Field, object data.Map) error {
var referencedValue = &referenceValue{
Key: tagName,
Field: field,
Object: object,
}
referencedValue.Setter = func(value interface{}) {
referencedValue.Used = true
field.Set(value, referencedValue.Object)
}
(*v)[tagName] = referencedValue
return nil
}
func (v *referenceValues) Apply(tagName string, value interface{}) error {
referencedValue, ok := (*v)[tagName]
if !ok {
var referencesSoFar = make([]string, 0)
for k := range *v {
referencesSoFar = append(referencesSoFar, k)
}
return fmt.Errorf("Missing referenceValue %v in the previous rows, available[%v]", tagName, strings.Join(referencesSoFar, ","))
}
referencedValue.Setter(value)
return nil
}
func newReferenceValues() referenceValues {
var result referenceValues = make(map[string]*referenceValue)
return result
}
//referenceValue represent reference value
type referenceValue struct {
Setter func(value interface{}) //setter handler
Key string //reference key
Field *Field //field
Object data.Map //target object
Used bool //flag indicating if reference was used, if not then error
}