-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathdata.go
More file actions
121 lines (111 loc) · 2.83 KB
/
data.go
File metadata and controls
121 lines (111 loc) · 2.83 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package dsunit
import (
"fmt"
"github.com/viant/toolbox"
"github.com/viant/toolbox/data"
"strings"
)
// TableData represents table data
type TableData struct {
Table string
Value interface{} //deprecated, use Data attribute instead
Data interface{}
AutoGenerate map[string]string `json:",omitempty"`
PostIncrement []string `json:",omitempty"`
Key string
}
// AutoGenerateIfNeeded retrieves auto generated values
func (d *TableData) AutoGenerateIfNeeded(state data.Map) error {
for k, v := range d.AutoGenerate {
var value interface{}
if v[0:2] == "${" && v[len(v)-1:] == "}" {
value = state.Expand(v)
} else {
var has bool
if strings.HasPrefix(v, "uuid.") {
v = "_udf." + v
}
value, has = state.GetValue(v)
if !has {
return fmt.Errorf("failed to autogenerate value for %v - unable to eval: %v", k, v)
}
}
state.SetValue(k, value)
}
return nil
}
// PostIncrementIfNeeded increments all specified counters by one.
func (d *TableData) PostIncrementIfNeeded(state data.Map) {
for _, key := range d.PostIncrement {
keyText := toolbox.AsString(key)
value, has := state.GetValue(keyText)
if !has {
value = 0
}
state.SetValue(keyText, toolbox.AsInt(value)+1)
}
}
// GetValues a table records.
func (d *TableData) GetValues(state data.Map) []map[string]interface{} {
if d.Data == nil { //backward compatible check
d.Data = d.Value
}
if d.Data == nil {
return []map[string]interface{}{}
}
if toolbox.IsMap(d.Data) {
var value = d.GetValue(state, d.Data)
if len(value) == 0 {
return []map[string]interface{}{}
}
return []map[string]interface{}{
value,
}
}
var result = make([]map[string]interface{}, 0)
if toolbox.IsSlice(d.Data) {
var aSlice = toolbox.AsSlice(d.Data)
for _, item := range aSlice {
value := d.GetValue(state, item)
if len(value) > 0 {
result = append(result, value)
}
}
}
return result
}
func hasNumericKeys(aMap map[string]interface{}) bool {
for k := range aMap {
if strings.HasPrefix(k, "$As") {
return true
}
}
return false
}
// GetValue returns record.
func (d *TableData) GetValue(state data.Map, source interface{}) map[string]interface{} {
expanded := state.Expand(source)
value := toolbox.AsMap(expanded)
//TODO remove this code
for k, v := range value {
var textValue = toolbox.AsString(v)
if strings.HasPrefix(textValue, "$As") {
value[k] = state.Expand(textValue)
} else if strings.HasPrefix(textValue, "$") {
delete(value, k)
} else if strings.HasPrefix(textValue, "\\$") {
value[k] = string(textValue[1:])
}
}
dataStoreState := state.GetMap(ServiceID)
var key = d.Key
if key == "" {
key = d.Table
}
if !dataStoreState.Has(key) {
dataStoreState.Put(key, data.NewCollection())
}
records := dataStoreState.GetCollection(key)
records.Push(value)
return value
}