Skip to content

Commit f1ca266

Browse files
committed
toml: add conversion of ast inf and nan to Any
1 parent 43a1d2c commit f1ca266

3 files changed

Lines changed: 37 additions & 12 deletions

File tree

vlib/toml/tests/value_query_test.v

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import toml
2+
import strconv
23

34
const toml_text = '
45
modules = [ "ui", "toml" ]
@@ -15,6 +16,11 @@ colors = [
1516
"yellow",
1617
[ "transparent" ]
1718
]
19+
20+
[values]
21+
nan = nan
22+
inf = inf
23+
minus-inf = -inf
1824
'
1925

2026
fn test_value_query_in_array() {
@@ -29,4 +35,12 @@ fn test_value_query_in_array() {
2935
assert value == 'toml'
3036
value = toml_doc.value('errors[11]').default_to('<none>').string()
3137
assert value == '<none>'
38+
39+
value = toml_doc.value('values.nan').string()
40+
assert value == 'nan'
41+
42+
mut value_u64 := toml_doc.value('values.inf').u64()
43+
assert value_u64 == strconv.double_plus_infinity
44+
value_u64 = toml_doc.value('values.minus-inf').u64()
45+
assert value_u64 == strconv.double_minus_infinity
3246
}

vlib/toml/to/to.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type DocOrAny = toml.Any | toml.Doc
1212
pub fn json(doa DocOrAny) string {
1313
match doa {
1414
toml.Doc {
15-
return any_to_json(doa.ast_to_any(doa.ast.table))
15+
return any_to_json(toml.ast_to_any(doa.ast.table))
1616
}
1717
toml.Any {
1818
return any_to_json(doa)

vlib/toml/toml.v

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub fn parse_dotted_key(key string) ?[]string {
152152

153153
// to_any converts the `Doc` to toml.Any type.
154154
pub fn (d Doc) to_any() Any {
155-
return d.ast_to_any(d.ast.table)
155+
return ast_to_any(d.ast.table)
156156
}
157157

158158
// value queries a value from the TOML document.
@@ -193,17 +193,17 @@ fn (d Doc) value_(value ast.Value, key []string) Any {
193193
}
194194

195195
if key.len <= 1 {
196-
return d.ast_to_any(ast_value)
196+
return ast_to_any(ast_value)
197197
}
198198
// `match` isn't currently very suitable for these types of sum type constructs...
199199
if ast_value is map[string]ast.Value || ast_value is []ast.Value {
200200
return d.value_(ast_value, key[1..])
201201
}
202-
return d.ast_to_any(value)
202+
return ast_to_any(value)
203203
}
204204

205205
// ast_to_any converts `from` ast.Value to toml.Any value.
206-
pub fn (d Doc) ast_to_any(value ast.Value) Any {
206+
pub fn ast_to_any(value ast.Value) Any {
207207
match value {
208208
ast.Date {
209209
return Any(Date{value.text})
@@ -218,11 +218,22 @@ pub fn (d Doc) ast_to_any(value ast.Value) Any {
218218
return Any(value.text)
219219
}
220220
ast.Number {
221-
// if value.text.contains('inf') || value.text.contains('nan') {
222-
// return Any() // TODO
223-
//}
224-
if !value.text.starts_with('0x')
225-
&& (value.text.contains('.') || value.text.to_lower().contains('e')) {
221+
val_text := value.text
222+
if val_text == 'inf' || val_text == '+inf' || val_text == '-inf' {
223+
// NOTE values taken from strconv
224+
if !val_text.starts_with('-') {
225+
// strconv.double_plus_infinity
226+
return Any(u64(0x7FF0000000000000))
227+
} else {
228+
// strconv.double_minus_infinity
229+
return Any(u64(0xFFF0000000000000))
230+
}
231+
}
232+
if val_text == 'nan' || val_text == '+nan' || val_text == '-nan' {
233+
return Any('nan')
234+
}
235+
if !val_text.starts_with('0x')
236+
&& (val_text.contains('.') || val_text.to_lower().contains('e')) {
226237
return Any(value.f64())
227238
}
228239
return Any(value.i64())
@@ -238,7 +249,7 @@ pub fn (d Doc) ast_to_any(value ast.Value) Any {
238249
m := (value as map[string]ast.Value)
239250
mut am := map[string]Any{}
240251
for k, v in m {
241-
am[k] = d.ast_to_any(v)
252+
am[k] = ast_to_any(v)
242253
}
243254
return am
244255
// return d.get_map_value(m, key_split[1..].join('.'))
@@ -247,7 +258,7 @@ pub fn (d Doc) ast_to_any(value ast.Value) Any {
247258
a := (value as []ast.Value)
248259
mut aa := []Any{}
249260
for val in a {
250-
aa << d.ast_to_any(val)
261+
aa << ast_to_any(val)
251262
}
252263
return aa
253264
}

0 commit comments

Comments
 (0)