-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_test.go
359 lines (301 loc) · 7.61 KB
/
options_test.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package opt
import (
"fmt"
"testing"
"github.com/davecgh/go-spew/spew"
hjson "github.com/hjson/hjson-go"
)
func TestOptions(t *testing.T) {
//first option
txt := "key=value;pass=abcd123s;"
op := New()
err := op.Parse(txt)
if err != nil {
t.Error(err)
}
t.Logf("@Options: \n%s", op.Format("\n"))
t.Logf(" -->%s", op.String())
//second without ;
txt = "key=value;pass=abcd123s;kv=xxxx"
err = op.Parse(txt)
if err != nil {
t.Error(err)
}
t.Logf("@Options: \n%s", op.Format("\n"))
t.Logf(" -->%s", op.String())
//third option
txt = "key=value;pass=abcd123s\\; \\ntest;kv=invalid"
err = op.Parse(txt)
if err != nil {
t.Error(err)
}
t.Logf("@Options: \n%s", op.Format("\n"))
t.Logf(" -->%s", op.String())
//fourth option
txt = "key=value;pass=abcd123s\\; test;kv=escaped\\\\dd\\=; "
err = op.Parse(txt)
if err != nil {
t.Error(err)
}
t.Logf("@Options: \n%s", op.Format("\n"))
t.Logf(" -->%s", op.String())
//fifth option
txt = `key=value;pass=abcd123s\; \
a test;kv=escaped\\dd\=;`
err = op.Parse(txt)
if err != nil {
t.Error(err)
}
t.Logf("@Options: \n%s", op.Format("\n"))
t.Logf(" -->%s", op.String())
}
func TestHjson(t *testing.T) {
sampleText := []byte(`
{
# specify rate in requests/second
rate: 1000
array: [10]
password: abcdeasdfd
}`)
cfgText := []byte(`
{
port: /dev/ttyAMA0
baudrate: 2400
readTimeout: 3 #timeout in seconds
unit: uSv/h
decimalPlace: 4
minLength: 13
timeZone: Asia/Jakarta
endLine: a\r\nb
}`)
var dat map[string]interface{}
if err := hjson.Unmarshal(sampleText, &dat); err != nil {
t.Error(err)
}
op := New()
op.Assign(dat)
t.Logf("@Options: \n%s", op.Format("\n"))
obj, _ := op.GetObject("array")
fmt.Printf("@Options: %v, %T\n", op, obj)
vi := op.GetFloat64Array("array")
fmt.Printf("@Options: %+v, %T\n", vi, vi)
var mcfg map[string]interface{}
if err := hjson.Unmarshal(cfgText, &mcfg); err != nil {
t.Error(err)
}
op.Assign(mcfg)
t.Logf("@Config: \n%s", op.Format("\n"))
}
func TestHjsonOnly(t *testing.T) {
text := []byte(`
{
YDOC_AWS-Batan-L02m_L02m:
[
{id: 11, name: "Temp"}
{id: 20, name: "Hum"}
{id: 30, name: "Speed"}
]
YDOC_AWS-Batan-L10m_L10m:
[
{id: 31, name: "Temp"}
{id: 32, name: "Hum"}
{id: 33, name: "Delta T"}
]
YDOC_AWS-Batan-L60m_L60m:
[
{id: 41, name: "Temp"}
{id: 42, name: "Hum"}
{id: 43, name: "Rain"}
]
}`)
var dat map[string]interface{}
if err := hjson.Unmarshal(text, &dat); err != nil {
t.Fatal(err)
}
spew.Dump(dat)
t.Logf("%+v", dat)
}
func TestHjsonOptions(t *testing.T) {
cfgText := []byte(`
{
addr: localhost:21
user: anonymous
pass: abcd@abcd
readTimeout: 3 #timeout in seconds
checkInterval: 10 #every ten seconds
timeZone: Asia/Jakarta
# Sensors mapping for each altitude
# Valid: Temp, Hum, Press, Direct, Speed, Rain, Solrad, DeltaT, Bat, Direct STD, Speed STD
sensors: {
YDOC_AWS-Batan-L02m_L02m: {
Hum: 10
Temp: 20
}
YDOC_AWS-Batan-L10m_L10m: {
"Direct STD": 50
Rain: 300
}
}
}
`)
var dat map[string]interface{}
// Decode and a check for errors.
if err := hjson.Unmarshal(cfgText, &dat); err != nil {
t.Error(err)
}
spew.Dump(dat)
op := New()
op.Assign(dat)
t.Logf("@Config: \n%s", op.Format("\n"))
sensors, ok := op.GetObject("sensors")
if !ok {
//
t.Logf("Log failed")
t.Fail()
}
sensMap, ok := sensors.(map[string]interface{})
if !ok {
t.Logf("Cast 1 failed")
t.Fail()
}
for prefix, def := range sensMap {
defMap, ok := def.(map[string]interface{})
if !ok {
t.Logf("Cast 2 failed")
t.Fail()
}
t.Log(prefix)
for sens, id := range defMap {
id64 := int64(id.(float64))
t.Logf("%s=%v, %03d", sens, id, id64)
}
}
topt := op.Get("sensors")
spew.Printf("Dump sensors %v", topt)
spew.Dump(topt)
}
func TestGetOptions(t *testing.T) {
cfgText := `{
vInt: 10
vInt64: 456789
vFloat: 6.542134
vFloat2: 1e+4
vBool: true
vStr: Ini string
}`
op, err := FromText(cfgText, FormatHJSON)
if err != nil {
t.Log(err)
t.FailNow()
}
t.Logf("vInt=%v", op.GetInt("vInt", 0))
t.Logf("vInt64=%v", op.GetInt64("vInt64", 0))
t.Logf("vFloat=%v", op.GetFloat("vFloat", 0.0))
t.Logf("vFloat2=%v", op.GetFloat("vFloat2", 0))
t.Logf("vBool=%v", op.GetBool("vBool", false))
//test string
t.Logf("vStr=%v, %v", op.GetString("vStr", "abcd"), op.GetInt("vStr", 20))
}
func TestFunctionality(t *testing.T) {
cfgText := `
{
version: v1.0.0
server: {
id: conoco01
listenAddr: ":8081"
acceptTimeout: 10 # timeout (dalam detik)
writeTimeout: 10 # timeout (dalam detik)
maxReceive: 50
maxSend: 50
device: ruptela
}
ruptela: {
message: "http://localhost:8081/api/"
report: "http://localhost:8081/reportAPI"
appendIMEI: false
serviceTimeout: 10 # REST Service timeout (dalam detik)
}
gpsdata: {
storage: sql
sql: {
driver: postgres
connection: "user=isi-user password=isi-password dbname=OSLOGREC_MTRACK host=127.0.0.1 port=5432 connect_timeout=30 sslmode=disable"
maxIdle: 10 #Max jumlah koneksi idle
maxOpen: 10 #Max jumlah koneksi open
maxLifetime: 60 #Maximum lifetime (dalam detik)
insertQuery: INSERT INTO "GPS_DATA"("IMEI", "DATA_LOG", "FLAG", "DATE_INS", "DESC", "GPS_CODE", "DATA_LEN") VALUES($1, $2, $3, $4, $5, $6, $7)
}
}
log: {
#Known types: console, file, sql
type: console, file, sql
console: {
# use default options
}
#for file (uncomment type)
# type: file
file: {
name: ./applog.log
append: true
}
#for sql (uncomment type)
# SQLite -> driver: sqlite3
# PostgreSQL -> driver: postgres
# type: sql
sql: {
driver: sqlite3
connection: ./applog.sqlite
maxIdle: 10 #Max jumlah koneksi idle
maxOpen: 10 #Max jumlah koneksi open
maxLifetime: 60 #Maximum lifetime (dalam detik)
createQuery: CREATE TABLE applog(id INTEGER PRIMARY KEY AUTOINCREMENT, ts DATETIME, app VARCHAR(100), content TEXT)
insertQuery: INSERT INTO applog(ts, app, content) VALUES(?, ?, ?)
}
}
}
`
op, err := FromText(cfgText, FormatHJSON)
if err != nil {
t.Log(err)
t.FailNow()
}
//get version, expected v1.0.0
ver := op.GetString("version", "")
if ver != "v1.0.0" {
t.Fatalf("Expecting paramerer is 'v1.0.0' got '%v'", ver)
}
server := op.Get("server")
if server.IsEmpty() {
t.Fatalf("Options should not empty")
}
id1 := server.GetString("id", "id1")
id2 := op.GetString("server.id", "id2")
if id1 != id2 {
t.Fatalf("%v <> %v (must be equal)", id1, id2)
}
t.Logf("server Id: %v == %v", id1, id2)
ok := op.Exists("log.http")
if ok {
t.Fatalf("log.http must not exists")
}
//test
ov := op.Set("log.http", "http://google.com")
nv := op.GetString("log.http", "failed")
t.Logf("Old value = %v, new value = %v", ov, nv)
oldName := op.Set("log.file.name", "newlog.txt")
newName := op.GetString("log.file.name", "failed")
t.Logf("Old value = %v, new value = %v", oldName, newName)
mFile := op.Get("log.file")
t.Logf("mFile: %+v", mFile)
eMap := op.Get("log.service")
t.Logf("eMap: '%+v'", eMap)
op.Set("log.service.api", "http://apiend.com")
sMap := op.Get("log.service")
t.Logf("sMap: '%+v'", sMap)
//dump contents
t.Log(op.AsJSON())
//as string?
t.Logf("String: %s", op)
//format?
t.Logf("Format: %v", op.Format("\n"))
}