-
Notifications
You must be signed in to change notification settings - Fork 1
/
target.go
228 lines (198 loc) · 5.12 KB
/
target.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"path"
"strconv"
"strings"
"time"
)
// Target ...
type Target struct {
Connection string `json:"connection"`
Fetch string `json:"fetch"`
Params []Param `json:"params"`
Prefetch bool `json:"prefetch"`
Nest []*Nest `json:"nest"`
Script string `json:"script"`
Split *Split `json:"split"`
Timezone string `json:"timezone"`
connection *Connection
extract string
prefetch string
params []interface{}
location *time.Location
}
// Nest ...
type Nest struct {
ChildKey string `json:"childKey"`
Connection string `json:"connection"`
Fetch string `json:"fetch"`
ParentKey string `json:"parentKey"`
Timezone string `json:"timezone"`
connection *Connection
fetch string
location *time.Location
}
// Param ...
type Param struct {
Name string `json:"name"`
Type string `json:"type"`
Default string `json:"default"`
}
// Split ...
type Split struct {
By string `json:"by"`
Layout string `json:"layout"`
Value string `json:"value"`
}
func loadTarget() *Target {
name := path.Join(args.target, defaultTarget)
checkFileExists(name)
buffer, err := ioutil.ReadFile(name)
check(err)
var target *Target
json.Unmarshal(buffer, &target)
resolveTarget(target)
return target
}
func resolveTarget(target *Target) {
resolveTargetQuery(target)
resolveTargetConnection(target)
resolveTargetLocation(target)
resolveTargetParams(target)
resolveTargetNest(target)
resolveTargetPrefetch(target)
}
func paramName(param Param, i int) string {
if param.Name != "" {
return fmt.Sprintf("\"%s\"", param.Name)
}
return fmt.Sprintf("#%d", i+1)
}
func applyTargetDefaultParams(target *Target) {
for i := range target.Params {
if target.Params[i].Default != "" {
if len(args.params) < i+1 {
args.params = append(args.params, target.Params[i].Default)
}
}
}
}
func resolveTargetQuery(target *Target) {
var err error
if target.Fetch == "" {
target.Fetch = defaultExtract
}
name := path.Join(args.target, target.Fetch)
checkFileExists(name)
target.extract, err = loadFile(name)
check(err)
}
func resolveTargetConnection(target *Target) {
if target.Connection == "" {
target.Connection = defaultConnection
}
target.connection = connect(target.Connection)
}
func resolveTargetLocation(target *Target) {
if target.Timezone != "" {
var err error
target.location, err = time.LoadLocation(target.Timezone)
check(err)
} else if target.connection.location != nil {
target.location = target.connection.location
}
}
func resolveTargetParams(target *Target) {
applyTargetDefaultParams(target)
if len(args.params) != len(target.Params) {
stop("Incorrect number of arguments specified", 1)
}
if len(args.params) > 0 {
target.params = make([]interface{}, len(args.params))
for i := range args.params {
target.params[i] = resolveTargetParam(args.params[i], target.Params[i], target.location, i)
}
}
}
func resolveTargetParam(text string, param Param, location *time.Location, i int) interface{} {
var (
value interface{}
err error
)
if text == "null" {
value = nil
} else if param.Type == "integer" {
value, err = strconv.Atoi(text)
} else if param.Type == "date" {
var t time.Time
t, err = parseTime(text)
if err == nil {
value = formatTimeDb(location, t)
}
} else if param.Type == "string" {
value = text
} else if param.Type == "boolean" {
value, err = strconv.ParseBool(text)
} else if param.Type == "float" {
value, err = strconv.ParseFloat(text, 32)
} else {
stop(fmt.Sprintf("Invalid type \"%s\" specified for param %s", param.Type, paramName(param, i)), 1)
}
if err != nil {
stop(fmt.Sprintf("Unable to convert specified value \"%s\" to type \"%s\" for param %s", text, param.Type, paramName(param, i)), 1)
}
return value
}
func resolveTargetNest(target *Target) {
var err error
if target.Nest != nil {
if len(target.Nest) > 0 && target.Nest[0].Fetch == "" {
target.Nest[0].Fetch = defaultNest
}
for i, nest := range target.Nest {
if nest.Fetch == "" {
stop(fmt.Sprintf("Unspecified query for nest #%d", i+1), 1)
}
name := path.Join(args.target, nest.Fetch)
checkFileExists(name)
nest.fetch, err = loadFile(name)
check(err)
if nest.Connection != "" {
nest.connection = connect(nest.Connection)
} else {
nest.connection = target.connection
}
if nest.Timezone != "" {
nest.location, err = time.LoadLocation(nest.Timezone)
check(err)
} else if target.location != nil {
nest.location = target.location
}
}
}
}
func resolveTargetPrefetch(target *Target) {
var err error
if target.Prefetch {
name := path.Join(args.target, defaultPrefetch)
checkFileExists(name)
target.prefetch, err = loadFile(name)
check(err)
if strings.Index(target.extract, "(%s)") == -1 {
stop("missing \"(%s)\" token in extract query", 1)
}
}
}
func validateTarget(target *Target) {
if target.Split != nil {
if target.Split.By != "date" {
stop("Invalid value for \"split\" specified", 1)
}
if strings.HasSuffix(args.out, ".json") {
stop("A file was specified for \"-o\" where a directory was expected", 1)
}
}
}