-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathyaml_obj_accessor.v
181 lines (153 loc) · 3.71 KB
/
yaml_obj_accessor.v
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
module yaml
pub type PathType = int | string
pub fn (this YamlValue) get(path ...PathType) ?YamlValue {
mut rtn := this
for p in path {
if p is int {
if mut rtn is YamlListValue {
if p < 0 || p >= rtn.ar.len {
return error("Invalid index to access Yaml list element: '$p'")
}
rtn = rtn.ar[p]
} else {
return error("'int' path element can only be used with Yaml lists")
}
} else if p is string {
if mut rtn is YamlMapValue {
if p in rtn.obj {
rtn = rtn.obj[p] ?
} else {
return error("Key not found in Yaml map element: '$p'")
}
} else {
return error("'string' path element can only be used with Yaml maps")
}
}
}
return rtn
}
pub fn (this YamlValue) is_list() bool {
return this is YamlListValue
}
pub fn (this YamlValue) is_map() bool {
return this is YamlMapValue
}
pub fn (this YamlValue) is_value() bool {
return match this {
string { true }
i64 { true }
f64 { true }
bool { true }
else { false }
}
}
pub fn (this YamlValue) len() int {
if this is string {
return this.len
} else if this is YamlListValue {
return this.ar.len
} else if this is YamlMapValue {
return this.obj.len
} else {
panic("Invalid YamlValue to determine 'len': $this")
}
}
pub fn (this YamlValue) is_empty() bool {
return this.len() == 0
}
pub fn (this YamlValue) string() ?string {
match this {
string {
return this
}
i64 {
return this.str()
}
f64 {
return this.str()
}
bool {
return this.str()
}
else {
return error("Invalid YamlValue to return a 'string': $this")
}
}
}
pub fn (this YamlValue) get_str(path ...PathType) ?string {
return this.get(...path) ?.string()
}
pub fn (this YamlValue) get_int(path ...PathType) ?int {
return this.get(...path) ?.int()
}
pub fn (this YamlValue) get_float(path ...PathType) ?f64 {
return this.get(...path) ?.f64()
}
pub fn (this YamlValue) get_bool(path ...PathType) ?bool {
return this.get(...path) ?.bool()
}
pub fn (this YamlValue) i8() ?i8 {
return i8(this.i64() ?)
}
pub fn (this YamlValue) i16() ?i16 {
return i16(this.i64() ?)
}
pub fn (this YamlValue) int() ?int {
return int(this.i64() ?)
}
pub fn (this YamlValue) i64() ?i64 {
if this is i64 {
return this
}
panic('Unable to determine i64 value: $this')
}
pub fn (this YamlValue) u8() ?u8 {
return u8(this.i64() ?)
}
pub fn (this YamlValue) u16() ?u16 {
return u16(this.i64() ?)
}
pub fn (this YamlValue) u32() ?u32 {
return u32(this.i64() ?)
}
pub fn (this YamlValue) u64() ?u64 {
return u64(this.i64() ?)
}
pub fn (this YamlValue) f32() ?f32 {
return f32(this.f64() ?)
}
pub fn (this YamlValue) f64() ?f64 {
if this is f64 {
return this
}
panic('Unable to determine f64 value: $this')
}
// TODO Tests are missing??
pub fn (this YamlValue) bool() ?bool {
if this is bool {
return this
}
if this is i64 {
return if this == 0 { false } else { true }
}
panic('Unable to determine f64 value: $this')
}
// get_date Return an integer with YYYYMMDD digits
// TODO To be implemented
// TODO How to make YamlValue extendable, so that USERS can extend it with their own converters?
pub fn (this YamlValue) get_date(fmt string) ?i64 {
// TODO Does i64()? panic if not an int ?!?!?
return this.string() ?.i64()
}
// get_time Return an integer with HHMMSS digits
// TODO To be implemented
// TODO How to make YamlValue extendable, so that USERS can extend it with their own converters?
pub fn (this YamlValue) get_time(fmt string) ?i64 {
return this.string() ?.i64()
}
// get_millis Return milli seconds since 1970 (UNIX style)
// TODO To be implemented
// TODO How to make YamlValue extendable, so that USERS can extend it with their own converters?
pub fn (this YamlValue) get_millis(fmt string) ?i64 {
return this.string() ?.i64()
}