-
Notifications
You must be signed in to change notification settings - Fork 0
/
modifier.go
78 lines (68 loc) · 1.57 KB
/
modifier.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
71
72
73
74
75
76
77
78
package sdtabledata
import (
"github.com/gaorx/stardust5/sdjson"
"github.com/gaorx/stardust5/sdstrings"
)
type Modifier interface {
ModifyRow(sdjson.Object) sdjson.Object
}
var (
_ Modifier = ModifierFunc(nil)
_ Modifier = Modifiers{}
)
type ModifierFunc func(sdjson.Object) sdjson.Object
type Modifiers []Modifier
func (f ModifierFunc) ModifyRow(row sdjson.Object) sdjson.Object {
return f(row)
}
func (modifiers Modifiers) ModifyRow(row sdjson.Object) sdjson.Object {
if len(modifiers) <= 0 {
return row
}
row1 := row
for _, m := range modifiers {
if m != nil {
row1 = m.ModifyRow(row1)
}
}
return row1
}
func SetColumn(col string, v any) ModifierFunc {
return func(row sdjson.Object) sdjson.Object {
if f, ok := v.(func(any) any); ok {
row[col] = f(row[col])
} else {
row[col] = v
}
return row
}
}
func SetColumns(colVals map[string]any) ModifierFunc {
if len(colVals) <= 0 {
return nil
}
return func(row sdjson.Object) sdjson.Object {
for col, v := range colVals {
if f, ok := v.(func(any) any); ok {
row[col] = f(row[col])
} else {
row[col] = v
}
}
return row
}
}
func ExpandColumnShellLikeV(col string, vars map[string]string) ModifierFunc {
return func(row sdjson.Object) sdjson.Object {
row[col] = sdstrings.ExpandShellLikeV(row[col].(string), vars)
return row
}
}
func ExpandColumnsShellLikeV(cols []string, vars map[string]string) ModifierFunc {
return func(row sdjson.Object) sdjson.Object {
for _, col := range cols {
row[col] = sdstrings.ExpandShellLikeV(row[col].(string), vars)
}
return row
}
}