@@ -108,6 +108,42 @@ pub fn encode_pretty[T](typed_data T) string {
108
108
return raw_decoded.prettify_json_str ()
109
109
}
110
110
111
+ // i8 - TODO
112
+ pub fn (f Any) i8 () i8 {
113
+ match f {
114
+ i8 {
115
+ return f
116
+ }
117
+ i16 , int , i64 , u8 , u16 , u32 , u64 , f32 , f64 , bool {
118
+ return i8 (f)
119
+ }
120
+ string {
121
+ return f.i8 ()
122
+ }
123
+ else {
124
+ return 0
125
+ }
126
+ }
127
+ }
128
+
129
+ // i16 - TODO
130
+ pub fn (f Any) i16 () i16 {
131
+ match f {
132
+ i16 {
133
+ return f
134
+ }
135
+ i8 , int , i64 , u8 , u16 , u32 , u64 , f32 , f64 , bool {
136
+ return i16 (f)
137
+ }
138
+ string {
139
+ return f.i16 ()
140
+ }
141
+ else {
142
+ return 0
143
+ }
144
+ }
145
+ }
146
+
111
147
// int uses `Any` as an integer.
112
148
pub fn (f Any) int () int {
113
149
match f {
@@ -118,9 +154,6 @@ pub fn (f Any) int() int {
118
154
return int (f)
119
155
}
120
156
string {
121
- if f == 'false' || f == 'true' {
122
- return int (f.bool ())
123
- }
124
157
return f.int ()
125
158
}
126
159
else {
@@ -139,9 +172,6 @@ pub fn (f Any) i64() i64 {
139
172
return i64 (f)
140
173
}
141
174
string {
142
- if f == 'false' || f == 'true' {
143
- return i64 (f.bool ())
144
- }
145
175
return f.i64 ()
146
176
}
147
177
else {
@@ -160,9 +190,6 @@ pub fn (f Any) u64() u64 {
160
190
return u64 (f)
161
191
}
162
192
string {
163
- if f == 'false' || f == 'true' {
164
- return u64 (f.bool ())
165
- }
166
193
return f.u64 ()
167
194
}
168
195
else {
@@ -181,9 +208,6 @@ pub fn (f Any) f32() f32 {
181
208
return f32 (f)
182
209
}
183
210
string {
184
- if f == 'false' || f == 'true' {
185
- return f32 (f.bool ())
186
- }
187
211
return f.f32 ()
188
212
}
189
213
else {
@@ -202,9 +226,6 @@ pub fn (f Any) f64() f64 {
202
226
return f64 (f)
203
227
}
204
228
string {
205
- if f == 'false' || f == 'true' {
206
- return f64 (f.bool ())
207
- }
208
229
return f.f64 ()
209
230
}
210
231
else {
@@ -220,8 +241,14 @@ pub fn (f Any) bool() bool {
220
241
return f
221
242
}
222
243
string {
244
+ if f == 'false' {
245
+ return false
246
+ }
247
+ if f == 'true' {
248
+ return true
249
+ }
223
250
if f.len > 0 {
224
- return f != '0' && f != '0.0' && f != 'false'
251
+ return f != '0' && f != '0.0'
225
252
} else {
226
253
return false
227
254
}
@@ -309,3 +336,59 @@ pub fn (f Any) to_time() !time.Time {
309
336
}
310
337
}
311
338
}
339
+
340
+ fn map_from [T](t T) map [string ]Any {
341
+ mut m := map [string ]Any{}
342
+ $if T is $Struct {
343
+ $for field in T.fields {
344
+ value := t.$(field.name)
345
+
346
+ $if field.is_array {
347
+ mut arr := []Any{}
348
+ for variable in value {
349
+ arr << Any (variable)
350
+ }
351
+ m[field.name] = arr
352
+ arr.clear ()
353
+ } $else $if field.is_struct {
354
+ m[field.name] = map_from (value)
355
+ } $else $if field.is_map {
356
+ // TODO
357
+ } $else $if field.is_alias {
358
+ // TODO
359
+ } $else $if field.is_option {
360
+ // TODO
361
+ } $else {
362
+ // TODO improve memory usage when convert
363
+ $if field.typ is string {
364
+ m[field.name] = value.str ()
365
+ } $else $if field.typ is bool {
366
+ m[field.name] = t.$(field.name).str ().bool ()
367
+ } $else $if field.typ is i8 {
368
+ m[field.name] = t.$(field.name).str ().i8 ()
369
+ } $else $if field.typ is i16 {
370
+ m[field.name] = t.$(field.name).str ().i16 ()
371
+ } $else $if field.typ is int {
372
+ m[field.name] = t.$(field.name).str ().int ()
373
+ } $else $if field.typ is i64 {
374
+ m[field.name] = t.$(field.name).str ().i64 ()
375
+ } $else $if field.typ is f32 {
376
+ m[field.name] = t.$(field.name).str ().f32 ()
377
+ } $else $if field.typ is f64 {
378
+ m[field.name] = t.$(field.name).str ().f64 ()
379
+ } $else $if field.typ is u8 {
380
+ m[field.name] = t.$(field.name).str ().u8 ()
381
+ } $else $if field.typ is u16 {
382
+ m[field.name] = t.$(field.name).str ().u16 ()
383
+ } $else $if field.typ is u32 {
384
+ m[field.name] = t.$(field.name).str ().u32 ()
385
+ } $else $if field.typ is u64 {
386
+ m[field.name] = t.$(field.name).str ().u64 ()
387
+ } $else {
388
+ // return error("The type of `${field.name}` can't be decoded. Please open an issue at https://github.com/vlang/v/issues/new/choose")
389
+ }
390
+ }
391
+ }
392
+ }
393
+ return m
394
+ }
0 commit comments