/
simpleyaml.go
512 lines (387 loc) · 10.5 KB
/
simpleyaml.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package simpleyaml
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2021 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"errors"
"fmt"
"gopkg.in/yaml.v2"
)
// ////////////////////////////////////////////////////////////////////////////////// //
// Yaml is YAML struct
type Yaml struct {
data interface{}
}
// ////////////////////////////////////////////////////////////////////////////////// //
// Errors
var (
ErrYAMLIsNil = errors.New("Yaml struct or data is nil")
ErrMapTypeAssertion = errors.New("Type assertion to map[string]interface{} failed")
ErrArrayTypeAssertion = errors.New("Type assertion to []interface{} failed")
ErrStringArrayTypeAssertion = errors.New("Type assertion to []string failed")
ErrBoolTypeAssertion = errors.New("Type assertion to bool failed")
ErrStringTypeAssertion = errors.New("Type assertion to string failed")
ErrByteTypeAssertion = errors.New("Type assertion to []byte failed")
ErrIntTypeAssertion = errors.New("Type assertion to int failed")
ErrFloatTypeAssertion = errors.New("Type assertion to float64 failed")
)
// ////////////////////////////////////////////////////////////////////////////////// //
// New returns a pointer to a new, empty `Yaml` object
func New() *Yaml {
return &Yaml{
data: make(map[string]interface{}),
}
}
// NewYaml returns a pointer to a new `Yaml` object after unmarshaling `body` bytes
func NewYaml(body []byte) (*Yaml, error) {
var data interface{}
err := yaml.Unmarshal(body, &data)
if err != nil {
return nil, fmt.Errorf("Can't unmarshal data: %v", err)
}
return &Yaml{data}, nil
}
// ////////////////////////////////////////////////////////////////////////////////// //
// Interface returns the underlying data
func (y *Yaml) Interface() interface{} {
if y == nil || y.data == nil {
return nil
}
return y.data
}
// Encode returns it's marshaled data as `[]byte`
func (y *Yaml) Encode() ([]byte, error) {
return y.MarshalYAML()
}
// MarshalYAML implements the yaml.Marshaler interface
func (y *Yaml) MarshalYAML() ([]byte, error) {
if y == nil || y.data == nil {
return nil, ErrYAMLIsNil
}
return yaml.Marshal(&y.data)
}
// UnmarshalYAML implements the yaml.Unmarshaler interface
func (y *Yaml) UnmarshalYAML(p []byte) error {
if y == nil {
return ErrYAMLIsNil
}
return yaml.Unmarshal(p, &y.data)
}
// Int type asserts to an `int`
func (y *Yaml) Int() (int, error) {
if y == nil || y.data == nil {
return 0, ErrYAMLIsNil
}
i, ok := (y.data).(int)
if ok {
return i, nil
}
return 0, ErrIntTypeAssertion
}
// Float type asserts to a `float64`
func (y *Yaml) Float() (float64, error) {
if y == nil || y.data == nil {
return 0.0, ErrYAMLIsNil
}
f, ok := (y.data).(float64)
if ok {
return f, nil
}
return 0.0, ErrFloatTypeAssertion
}
// Bool type asserts to a `bool`
func (y *Yaml) Bool() (bool, error) {
if y == nil || y.data == nil {
return false, ErrYAMLIsNil
}
b, ok := (y.data).(bool)
if ok {
return b, nil
}
return false, ErrBoolTypeAssertion
}
// String type asserts to a `string`
func (y *Yaml) String() (string, error) {
if y == nil || y.data == nil {
return "", ErrYAMLIsNil
}
s, ok := (y.data).(string)
if ok {
return s, nil
}
return "", ErrStringTypeAssertion
}
// Bytes type asserts to a `[]byte`
func (y *Yaml) Bytes() ([]byte, error) {
if y == nil || y.data == nil {
return nil, ErrYAMLIsNil
}
s, ok := (y.data).(string)
if ok {
return []byte(s), nil
}
return nil, ErrByteTypeAssertion
}
// Map type asserts to an `map`
func (y *Yaml) Map() (map[interface{}]interface{}, error) {
if y == nil || y.data == nil {
return nil, ErrYAMLIsNil
}
m, ok := (y.data).(map[interface{}]interface{})
if ok {
return m, nil
}
return nil, ErrMapTypeAssertion
}
// Array type asserts to an `array`
func (y *Yaml) Array() ([]interface{}, error) {
if y == nil || y.data == nil {
return nil, ErrYAMLIsNil
}
a, ok := (y.data).([]interface{})
if ok {
return a, nil
}
return nil, ErrArrayTypeAssertion
}
// Dump returns string representation of any kind of data
func (y *Yaml) Dump() string {
if y == nil || y.data == nil {
return ""
}
return fmt.Sprintf("%v", y.data)
}
// StringArray type asserts to an `array` of `string`
func (y *Yaml) StringArray() ([]string, error) {
if y == nil || y.data == nil {
return nil, ErrYAMLIsNil
}
a, err := y.Array()
if err != nil {
return nil, err
}
return convertSlice(a)
}
// MustArray guarantees the return of a `[]interface{}` (with optional default)
//
// useful when you want to interate over array values in a succinct manner:
// for i, v := range yaml.Get("results").MustArray() {
// fmt.Println(i, v)
//
func (y *Yaml) MustArray(args ...[]interface{}) []interface{} {
var def []interface{}
if len(args) > 0 {
def = args[0]
}
a, err := y.Array()
if err != nil {
return def
}
return a
}
// MustMap guarantees the return of a `map[string]interface{}` (with optional default)
//
// useful when you want to interate over map values in a succinct manner:
// for k, v := range yaml.Get("dictionary").MustMap() {
// fmt.Println(k, v)
// }
func (y *Yaml) MustMap(args ...map[interface{}]interface{}) map[interface{}]interface{} {
var def map[interface{}]interface{}
if len(args) > 0 {
def = args[0]
}
m, err := y.Map()
if err != nil {
return def
}
return m
}
// MustString guarantees the return of a `string` (with optional default)
//
// useful when you explicitly want a `string` in a single value return context:
// myFunc(yaml.Get("param1").MustString(), yaml.Get("optional_param").MustString("my_default"))
func (y *Yaml) MustString(args ...string) string {
var def string
if len(args) > 0 {
def = args[0]
}
s, err := y.String()
if err != nil {
return def
}
return s
}
// MustStringArray guarantees the return of a `[]string` (with optional default)
//
// useful when you want to interate over array values in a succinct manner:
// for i, s := range yaml.Get("results").MustStringArray() {
// fmt.Println(i, s)
// }
func (y *Yaml) MustStringArray(args ...[]string) []string {
var def []string
if len(args) > 0 {
def = args[0]
}
a, err := y.StringArray()
if err != nil {
return def
}
return a
}
// MustInt guarantees the return of an `int` (with optional default)
//
// useful when you explicitly want an `int` in a single value return context:
// myFunc(yaml.Get("param1").MustInt(), yaml.Get("optional_param").MustInt(5150))
func (y *Yaml) MustInt(args ...int) int {
var def int
if len(args) > 0 {
def = args[0]
}
i, err := y.Int()
if err != nil {
return def
}
return i
}
// MustFloat guarantees the return of a `float64` (with optional default)
//
// useful when you explicitly want a `float64` in a single value return context:
// myFunc(yaml.Get("param1").MustFloat64(), yaml.Get("optional_param").MustFloat64(5.150))
func (y *Yaml) MustFloat(args ...float64) float64 {
var def float64
if len(args) > 0 {
def = args[0]
}
f, err := y.Float()
if err != nil {
return def
}
return f
}
// MustBool guarantees the return of a `bool` (with optional default)
//
// useful when you explicitly want a `bool` in a single value return context:
// myFunc(yaml.Get("param1").MustBool(), yaml.Get("optional_param").MustBool(true))
func (y *Yaml) MustBool(args ...bool) bool {
var def bool
if len(args) > 0 {
def = args[0]
}
b, err := y.Bool()
if err != nil {
return def
}
return b
}
// Get returns a pointer to a new `Yaml` object
// for `key` in it's `map` representation
func (y *Yaml) Get(key string) *Yaml {
m, err := y.Map()
if err != nil {
return &Yaml{nil}
}
data, present := m[key]
if present {
return &Yaml{data}
}
return &Yaml{nil}
}
// GetPath searches for the item as specified by the branch
// without the need to deep dive using Get()'s
func (y *Yaml) GetPath(branch ...string) *Yaml {
yin := y
for _, key := range branch {
yin = yin.Get(key)
}
return yin
}
// GetByIndex returns a pointer to a new `Yaml` object
// for `index` in it's `array` representation
func (y *Yaml) GetByIndex(index int) *Yaml {
a, err := y.Array()
if err != nil {
return &Yaml{nil}
}
if len(a) > index {
return &Yaml{a[index]}
}
return &Yaml{nil}
}
// CheckGet returns a pointer to a new `Yaml` object and
// a `bool` identifying success or failure
//
// useful for chained operations when success is important:
// if data, ok := yaml.Get("top_level").CheckGet("inner"); ok {
// log.Println(data)
// }
func (y *Yaml) CheckGet(key string) (*Yaml, bool) {
m, err := y.Map()
if err != nil {
return &Yaml{nil}, false
}
data, present := m[key]
if present {
return &Yaml{data}, true
}
return &Yaml{nil}, false
}
// IsArray return true if yaml part is array
func (y *Yaml) IsArray() bool {
_, err := y.Array()
return err == nil
}
// IsMap return true if yaml part is map
func (y *Yaml) IsMap() bool {
_, err := y.Map()
return err == nil
}
// IsExist return false if object with given key
// is not present in `Yaml` object
func (y *Yaml) IsExist(key string) bool {
return y.Get(key).data != nil
}
// IsPathExist return false if object with given path
// is not present in `Yaml` object
func (y *Yaml) IsPathExist(path ...string) bool {
return y.GetPath(path...).data != nil
}
// IsIndexExist return false if object with given index
// is not present in `Yaml` array
func (y *Yaml) IsIndexExist(index int) bool {
return y.GetByIndex(index).data != nil
}
// GetMapKeys return slice with all map keys
func (y *Yaml) GetMapKeys() ([]string, error) {
m, err := y.Map()
if err != nil {
return nil, err
}
var result []string
for key := range m {
skey, ok := key.(string)
if ok {
result = append(result, skey)
}
}
return result, nil
}
// ////////////////////////////////////////////////////////////////////////////////// //
// convertSlice convert []interface{} slice to string slice
func convertSlice(a []interface{}) ([]string, error) {
result := make([]string, 0, len(a))
for _, item := range a {
if item == nil {
result = append(result, "")
continue
}
str, ok := item.(string)
if !ok {
return nil, ErrStringArrayTypeAssertion
}
result = append(result, str)
}
return result, nil
}