-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_slice.go
57 lines (40 loc) · 873 Bytes
/
string_slice.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
package unmarshal
import (
"strings"
"github.com/buger/jsonparser"
)
type StringSlice []string
func (i *StringSlice) UnmarshalJSON(b []byte) error {
var data, dataType, _, err = jsonparser.Get(b)
if err != nil {
return err
}
if len(data) == 0 {
*i = []string{}
return nil
}
str := string(data)
switch dataType {
case jsonparser.String:
*i = strings.Split(str, ",")
return nil
case jsonparser.Number, jsonparser.Boolean:
*i = []string{str}
return nil
case jsonparser.Null:
*i = []string{}
return nil
case jsonparser.Object:
var slice []string
err := jsonparser.ObjectEach(data, func(key, value2 []byte, valueType2 jsonparser.ValueType, offset int) error {
slice = append(slice, string(value2))
return nil
})
if err != nil {
return err
}
*i = slice
return nil
}
return newError(dataType, "[]string")
}