-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreader.go
171 lines (133 loc) · 3.35 KB
/
reader.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
package jsonld_helper
import (
"fmt"
"reflect"
"strconv"
)
type Reader struct {
value any
length int
}
func of(value any) JsonLDReader {
jsonldReader := Reader{
value: value,
length: 1,
}
if isArray(reflect.TypeOf(value)) {
if _, ok := value.([]any); ok {
jsonldReader.length = len(value.([]any))
}
if _, ok := value.([]map[string]any); ok {
jsonldReader.length = len(value.([]map[string]any))
}
}
return jsonldReader
}
func (r Reader) Value() any { return r.value }
func (r Reader) Length() int { return r.length }
func (r Reader) ReadKey(key string) JsonLDReader { return readKey(r.value, key) }
func (r Reader) ReadIndex(index int) JsonLDReader { return readIndex(r.value, index) }
func (r Reader) Get() any { return getValue(r.value) }
func (r Reader) GetOrElse(defaultValue any) any { return getValue(r.value) }
func (r Reader) GetOrThrow(err error) (any, error) { return getValue(r.value), nil }
func (r Reader) StringOrElse(defaultValue string) string {
value, err := r.StringOrThrow(nil)
if err != nil {
return defaultValue
}
return value
}
func (r Reader) StringOrThrow(err error) (string, error) {
value := getValue(r.value)
stringValue, ok := value.(string)
if ok {
return stringValue, nil
}
boolValue, boolOK := value.(bool)
if boolOK {
return fmt.Sprintf("%t", boolValue), nil
}
intValue, intOK := value.(int)
if intOK {
return fmt.Sprintf("%d", intValue), nil
}
floatValue, floatOK := value.(float64)
if floatOK {
return fmt.Sprintf("%f", floatValue), nil
}
if err != nil {
return "", err
}
return "", fmt.Errorf("not a string")
}
func (r Reader) BoolOrElse(defaultValue bool) bool {
value, err := r.BoolOrThrow(nil)
if err != nil {
return defaultValue
}
return value
}
func (r Reader) BoolOrThrow(err error) (bool, error) {
value := getValue(r.value)
if boolValue, ok := value.(bool); ok {
return boolValue, nil
}
if stringValue, ok := value.(string); ok {
if stringValue == "true" || stringValue == "false" || stringValue == "1" || stringValue == "0" {
return strconv.ParseBool(stringValue)
}
}
if intValue, ok := value.(int); ok {
if intValue == 1 || intValue == 0 {
return intValue == 1, nil
}
}
if err != nil {
return false, err
}
return false, fmt.Errorf("not a bool")
}
func (r Reader) IntOrElse(defaultValue int) int {
value, err := r.IntOrThrow(nil)
if err != nil {
return defaultValue
}
return value
}
func (r Reader) IntOrThrow(err error) (int, error) {
value := getValue(r.value)
if intValue, ok := value.(int); ok {
return intValue, nil
}
if stringValue, ok := value.(string); ok {
if intValue, err := strconv.Atoi(stringValue); err == nil {
return intValue, nil
}
}
if err != nil {
return 0, err
}
return 0, fmt.Errorf("not a int")
}
func (r Reader) FloatOrElse(defaultValue float64) float64 {
value, err := r.FloatOrThrow(nil)
if err != nil {
return defaultValue
}
return value
}
func (r Reader) FloatOrThrow(err error) (float64, error) {
value := getValue(r.value)
if intValue, ok := value.(float64); ok {
return intValue, nil
}
if stringValue, ok := value.(string); ok {
if floatValue, err := strconv.ParseFloat(stringValue, 64); err == nil {
return floatValue, nil
}
}
if err != nil {
return 0, err
}
return 0, fmt.Errorf("not a float")
}