Skip to content

Commit dbf469e

Browse files
authored
toml: move to_burntsushi functionality (#12492)
1 parent 7cdc906 commit dbf469e

File tree

2 files changed

+71
-74
lines changed

2 files changed

+71
-74
lines changed

vlib/toml/tests/burntsushi.toml-test_test.v

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import os
22
import toml
3+
import toml.ast
4+
import x.json2
5+
import strconv
36

47
// Instructions for developers:
58
// The actual tests and data can be obtained by doing:
@@ -140,7 +143,9 @@ fn test_burnt_sushi_tomltest() {
140143
bs_toml_json_path := os.join_path(compare_work_dir_root,
141144
os.file_name(valid_test_file).all_before_last('.') + '.json')
142145

143-
os.write_file(v_toml_json_path, toml_doc.to_burntsushi()) or { panic(err) }
146+
os.write_file(v_toml_json_path, to_burntsushi(toml_doc.ast.table)) or {
147+
panic(err)
148+
}
144149

145150
bs_json := os.read_file(valid_test_file.all_before_last('.') + '.json') or {
146151
panic(err)
@@ -206,3 +211,68 @@ fn test_burnt_sushi_tomltest() {
206211
assert true
207212
}
208213
}
214+
215+
// to_burntsushi returns a BurntSushi compatible json string converted from the `value` ast.Value.
216+
fn to_burntsushi(value ast.Value) string {
217+
match value {
218+
ast.Quoted {
219+
json_text := json2.Any(value.text).json_str()
220+
return '{ "type": "string", "value": "$json_text" }'
221+
}
222+
ast.DateTime {
223+
// Normalization for json
224+
json_text := json2.Any(value.text).json_str().to_upper().replace(' ', 'T')
225+
typ := if json_text.ends_with('Z') || json_text.all_after('T').contains('-')
226+
|| json_text.all_after('T').contains('+') {
227+
'datetime'
228+
} else {
229+
'datetime-local'
230+
}
231+
return '{ "type": "$typ", "value": "$json_text" }'
232+
}
233+
ast.Date {
234+
json_text := json2.Any(value.text).json_str()
235+
return '{ "type": "date-local", "value": "$json_text" }'
236+
}
237+
ast.Time {
238+
json_text := json2.Any(value.text).json_str()
239+
return '{ "type": "time-local", "value": "$json_text" }'
240+
}
241+
ast.Bool {
242+
json_text := json2.Any(value.text.bool()).json_str()
243+
return '{ "type": "bool", "value": "$json_text" }'
244+
}
245+
ast.Null {
246+
json_text := json2.Any(value.text).json_str()
247+
return '{ "type": "null", "value": "$json_text" }'
248+
}
249+
ast.Number {
250+
if value.text.contains('.') || value.text.to_lower().contains('e') {
251+
json_text := value.text.f64()
252+
return '{ "type": "float", "value": "$json_text" }'
253+
}
254+
i64_ := strconv.parse_int(value.text, 0, 0) or { i64(0) }
255+
return '{ "type": "integer", "value": "$i64_" }'
256+
}
257+
map[string]ast.Value {
258+
mut str := '{ '
259+
for key, val in value {
260+
json_key := json2.Any(key).json_str()
261+
str += ' "$json_key": ${to_burntsushi(val)},'
262+
}
263+
str = str.trim_right(',')
264+
str += ' }'
265+
return str
266+
}
267+
[]ast.Value {
268+
mut str := '[ '
269+
for val in value {
270+
str += ' ${to_burntsushi(val)},'
271+
}
272+
str = str.trim_right(',')
273+
str += ' ]\n'
274+
return str
275+
}
276+
}
277+
return '<error>'
278+
}

vlib/toml/toml.v

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import toml.input
99
import toml.scanner
1010
import toml.parser
1111
import time
12-
import x.json2
1312
import strconv
1413

1514
// Null is used in sumtype checks as a "default" value when nothing else is possible.
@@ -207,75 +206,3 @@ fn (d Doc) ast_to_any(value ast.Value) Any {
207206
// panic(@MOD + '.' + @STRUCT + '.' + @FN + ' can\'t convert "$value"')
208207
// return Any('')
209208
}
210-
211-
// to_burntsushi returns a BurntSushi compatible json string of the complete document.
212-
pub fn (d Doc) to_burntsushi() string {
213-
return d.to_burntsushi_(d.ast.table)
214-
}
215-
216-
// to_burntsushi returns a BurntSushi compatible json string of the complete document.
217-
fn (d Doc) to_burntsushi_(value ast.Value) string {
218-
match value {
219-
ast.Quoted {
220-
// txt := .replace(r'\\','\\').replace(r'\"','"')
221-
json_text := json2.Any(value.text).json_str()
222-
return '{ "type": "string", "value": "$json_text" }'
223-
}
224-
ast.DateTime {
225-
// Normalization for json
226-
json_text := json2.Any(value.text).json_str().to_upper().replace(' ', 'T')
227-
typ := if json_text.ends_with('Z') || json_text.all_after('T').contains('-')
228-
|| json_text.all_after('T').contains('+') {
229-
'datetime'
230-
} else {
231-
'datetime-local'
232-
}
233-
return '{ "type": "$typ", "value": "$json_text" }'
234-
}
235-
ast.Date {
236-
json_text := json2.Any(value.text).json_str()
237-
return '{ "type": "date-local", "value": "$json_text" }'
238-
}
239-
ast.Time {
240-
json_text := json2.Any(value.text).json_str()
241-
return '{ "type": "time-local", "value": "$json_text" }'
242-
}
243-
ast.Bool {
244-
json_text := json2.Any(value.text.bool()).json_str()
245-
return '{ "type": "bool", "value": "$json_text" }'
246-
}
247-
ast.Null {
248-
json_text := json2.Any(value.text).json_str()
249-
return '{ "type": "null", "value": "$json_text" }'
250-
}
251-
ast.Number {
252-
if value.text.contains('.') || value.text.to_lower().contains('e') {
253-
json_text := value.text.f64()
254-
return '{ "type": "float", "value": "$json_text" }'
255-
}
256-
i64_ := strconv.parse_int(value.text, 0, 0) or { i64(0) }
257-
return '{ "type": "integer", "value": "$i64_" }'
258-
}
259-
map[string]ast.Value {
260-
mut str := '{ '
261-
for key, val in value {
262-
json_key := json2.Any(key).json_str()
263-
str += ' "$json_key": ${d.to_burntsushi_(val)},'
264-
// str += d.to_burntsushi_(val, indent+1)
265-
}
266-
str = str.trim_right(',')
267-
str += ' }'
268-
return str
269-
}
270-
[]ast.Value {
271-
mut str := '[ '
272-
for val in value {
273-
str += ' ${d.to_burntsushi_(val)},'
274-
}
275-
str = str.trim_right(',')
276-
str += ' ]\n'
277-
return str
278-
}
279-
}
280-
return '<error>'
281-
}

0 commit comments

Comments
 (0)