-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssignAdd.go
146 lines (132 loc) · 4.12 KB
/
AssignAdd.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
import (
"reflect"
"strings"
"github.com/pkg/errors"
"github.com/spatialcurrent/go-adaptive-functions/pkg/af"
)
// AssignAdd is a BinaryOperator which sets the added value of the left side and right side to the attribute or variable defined by the left side.
type AssignAdd struct {
*BinaryOperator
}
func (a AssignAdd) Dfl(quotes []string, pretty bool, tabs int) string {
b := a.Builder("+=", quotes, tabs)
if pretty {
b = b.Indent(tabs).Pretty(pretty).Tabs(tabs + 1).TrimRight(pretty)
switch a.Left.(type) {
case *Attribute:
switch a.Right.(type) {
case *Function, *Pipe:
return b.Dfl()
}
case *Variable:
switch a.Right.(type) {
case *Function, *Pipe:
return b.Dfl()
}
}
return a.BinaryOperator.Dfl("+=", quotes, pretty, tabs)
}
return b.Dfl()
}
func (a AssignAdd) Sql(pretty bool, tabs int) string {
if pretty {
switch left := a.Left.(type) {
case *Variable:
str := strings.Repeat(" ", tabs) + "WHERE " + a.Right.Sql(pretty, tabs) + "\n"
str += strings.Repeat(" ", tabs) + "INTO TEMP TABLE " + left.Sql(pretty, tabs) + ";"
return str
}
return ""
}
switch left := a.Left.(type) {
case *Variable:
return "WHERE " + a.Right.Sql(pretty, tabs) + " INTO TEMP TABLE " + left.Sql(pretty, tabs) + ";" // #nosec
}
return ""
}
func (a AssignAdd) Map() map[string]interface{} {
return a.BinaryOperator.Map("assignadd", a.Left, a.Right)
}
// Compile returns a compiled version of this node.
func (a AssignAdd) Compile() Node {
left := a.Left.Compile()
right := a.Right.Compile()
return &AssignAdd{&BinaryOperator{Left: left, Right: right}}
}
func (a AssignAdd) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
switch left := a.Left.(type) {
case Attribute:
vars, lv, rv, err := a.EvaluateLeftAndRight(vars, ctx, funcs, quotes)
if err != nil {
return vars, 0, err
}
value, err := af.Add.ValidateRun(lv, rv)
if err != nil {
return vars, 0, errors.Wrap(err, ErrorEvaluate{Node: a, Quotes: quotes}.Error())
}
if len(left.Name) == 0 {
return vars, value, nil
}
if t := reflect.TypeOf(ctx); t.Kind() != reflect.Map {
ctx = map[string]interface{}{}
}
path := left.Name
obj := ctx
for len(path) > 0 {
if !strings.Contains(path, ".") {
reflect.ValueOf(obj).SetMapIndex(reflect.ValueOf(path), reflect.ValueOf(value))
break
}
pair := strings.SplitN(path, ".", 2)
objectValue := reflect.ValueOf(obj)
next := objectValue.MapIndex(reflect.ValueOf(pair[0]))
if (reflect.TypeOf(next.Interface()).Kind() != reflect.Map) || (!objectValue.IsValid()) || objectValue.IsNil() {
m := map[string]interface{}{}
objectValue.SetMapIndex(reflect.ValueOf(pair[0]), reflect.ValueOf(m))
obj = m
} else {
obj = next.Interface()
}
path = pair[1]
}
return vars, ctx, nil
case Variable:
vars, lv, rv, err := a.EvaluateLeftAndRight(vars, ctx, funcs, quotes)
if err != nil {
return vars, 0, err
}
value, err := af.Add.ValidateRun(lv, rv)
if err != nil {
return vars, 0, errors.Wrap(err, ErrorEvaluate{Node: a, Quotes: quotes}.Error())
}
path := left.Name
var obj interface{}
obj = vars
for len(path) > 0 {
if !strings.Contains(path, ".") {
reflect.ValueOf(obj).SetMapIndex(reflect.ValueOf(path), reflect.ValueOf(value))
break
}
pair := strings.SplitN(path, ".", 2)
objectValue := reflect.ValueOf(obj)
next := objectValue.MapIndex(reflect.ValueOf(pair[0]))
if (reflect.TypeOf(next.Interface()).Kind() != reflect.Map) || (!objectValue.IsValid()) || objectValue.IsNil() {
m := map[string]interface{}{}
objectValue.SetMapIndex(reflect.ValueOf(pair[0]), reflect.ValueOf(m))
obj = m
} else {
obj = next.Interface()
}
path = pair[1]
}
return vars, ctx, nil
}
return vars, ctx, &ErrorEvaluate{Node: a, Quotes: quotes}
}