-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssign.go
More file actions
144 lines (130 loc) · 4.01 KB
/
Assign.go
File metadata and controls
144 lines (130 loc) · 4.01 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
import (
"fmt"
"reflect"
"strings"
"github.com/pkg/errors"
)
// Assign is a BinaryOperator which sets the value of the right side to the attribute or variable defined by the left side.
type Assign struct {
*BinaryOperator
}
func (a Assign) Dfl(quotes []string, pretty bool, tabs int) string {
b := a.Builder(":=", quotes, tabs)
if pretty {
b = b.Indent(tabs + 1).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 Assign) 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 Assign) Map() map[string]interface{} {
return map[string]interface{}{
"@type": ":=",
"@value": map[string]interface{}{
"left": a.BinaryOperator.Left.Map(),
"right": a.BinaryOperator.Right.Map(),
},
}
}
func (a Assign) MarshalMap() (interface{}, error) {
return a.Map(), nil
}
// Compile returns a compiled version of this node.
func (a Assign) Compile() Node {
left := a.Left.Compile()
right := a.Right.Compile()
return &Assign{&BinaryOperator{Left: left, Right: right}}
}
func (a Assign) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
switch lva := a.Left.(type) {
case Attribute:
vars, rv, err := a.Right.Evaluate(vars, ctx, funcs, quotes)
if err != nil {
return vars, rv, errors.Wrap(err, "error processing right value of "+a.Dfl(quotes, false, 0))
}
if t := reflect.TypeOf(ctx); t.Kind() != reflect.Map {
ctx = map[string]interface{}{}
}
path := lva.Name
obj := ctx
for len(path) > 0 {
if !strings.Contains(path, ".") {
reflect.ValueOf(obj).SetMapIndex(reflect.ValueOf(path), reflect.ValueOf(rv))
break
}
pair := strings.SplitN(path, ".", 2)
v := reflect.ValueOf(obj)
next := v.MapIndex(reflect.ValueOf(pair[0]))
if (reflect.TypeOf(next.Interface()).Kind() != reflect.Map) || (!v.IsValid()) || v.IsNil() {
m := map[string]interface{}{}
v.SetMapIndex(reflect.ValueOf(pair[0]), reflect.ValueOf(m))
obj = m
} else {
obj = next.Interface()
}
path = pair[1]
}
return vars, ctx, nil
case Variable:
vars, rv, err := a.Right.Evaluate(vars, ctx, funcs, quotes)
if err != nil {
return vars, rv, errors.Wrap(err, "error processing right value of "+a.Dfl(quotes, false, 0))
}
path := lva.Name
var obj interface{}
obj = vars
for len(path) > 0 {
if !strings.Contains(path, ".") {
reflect.ValueOf(obj).SetMapIndex(reflect.ValueOf(path), reflect.ValueOf(rv))
break
}
pair := strings.SplitN(path, ".", 2)
v := reflect.ValueOf(obj)
next := v.MapIndex(reflect.ValueOf(pair[0]))
if (reflect.TypeOf(next.Interface()).Kind() != reflect.Map) || (!v.IsValid()) || v.IsNil() {
m := map[string]interface{}{}
v.SetMapIndex(reflect.ValueOf(pair[0]), reflect.ValueOf(m))
obj = m
} else {
obj = next.Interface()
}
path = pair[1]
}
return vars, ctx, nil
}
return vars, ctx, errors.New("error evaluating declare. left value (" + a.Left.Dfl(quotes, false, 0) + ") must be an attribute node but is of type " + fmt.Sprint(reflect.TypeOf(a.Left)))
}