toml: move to_burntsushi functionality (#12492)

pull/12495/head
Larpon 2021-11-17 16:25:50 +01:00 committed by GitHub
parent 7cdc906683
commit dbf469e000
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 74 deletions

View File

@ -1,5 +1,8 @@
import os
import toml
import toml.ast
import x.json2
import strconv
// Instructions for developers:
// The actual tests and data can be obtained by doing:
@ -140,7 +143,9 @@ fn test_burnt_sushi_tomltest() {
bs_toml_json_path := os.join_path(compare_work_dir_root,
os.file_name(valid_test_file).all_before_last('.') + '.json')
os.write_file(v_toml_json_path, toml_doc.to_burntsushi()) or { panic(err) }
os.write_file(v_toml_json_path, to_burntsushi(toml_doc.ast.table)) or {
panic(err)
}
bs_json := os.read_file(valid_test_file.all_before_last('.') + '.json') or {
panic(err)
@ -206,3 +211,68 @@ fn test_burnt_sushi_tomltest() {
assert true
}
}
// to_burntsushi returns a BurntSushi compatible json string converted from the `value` ast.Value.
fn to_burntsushi(value ast.Value) string {
match value {
ast.Quoted {
json_text := json2.Any(value.text).json_str()
return '{ "type": "string", "value": "$json_text" }'
}
ast.DateTime {
// Normalization for json
json_text := json2.Any(value.text).json_str().to_upper().replace(' ', 'T')
typ := if json_text.ends_with('Z') || json_text.all_after('T').contains('-')
|| json_text.all_after('T').contains('+') {
'datetime'
} else {
'datetime-local'
}
return '{ "type": "$typ", "value": "$json_text" }'
}
ast.Date {
json_text := json2.Any(value.text).json_str()
return '{ "type": "date-local", "value": "$json_text" }'
}
ast.Time {
json_text := json2.Any(value.text).json_str()
return '{ "type": "time-local", "value": "$json_text" }'
}
ast.Bool {
json_text := json2.Any(value.text.bool()).json_str()
return '{ "type": "bool", "value": "$json_text" }'
}
ast.Null {
json_text := json2.Any(value.text).json_str()
return '{ "type": "null", "value": "$json_text" }'
}
ast.Number {
if value.text.contains('.') || value.text.to_lower().contains('e') {
json_text := value.text.f64()
return '{ "type": "float", "value": "$json_text" }'
}
i64_ := strconv.parse_int(value.text, 0, 0) or { i64(0) }
return '{ "type": "integer", "value": "$i64_" }'
}
map[string]ast.Value {
mut str := '{ '
for key, val in value {
json_key := json2.Any(key).json_str()
str += ' "$json_key": ${to_burntsushi(val)},'
}
str = str.trim_right(',')
str += ' }'
return str
}
[]ast.Value {
mut str := '[ '
for val in value {
str += ' ${to_burntsushi(val)},'
}
str = str.trim_right(',')
str += ' ]\n'
return str
}
}
return '<error>'
}

View File

@ -9,7 +9,6 @@ import toml.input
import toml.scanner
import toml.parser
import time
import x.json2
import strconv
// 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 {
// panic(@MOD + '.' + @STRUCT + '.' + @FN + ' can\'t convert "$value"')
// return Any('')
}
// to_burntsushi returns a BurntSushi compatible json string of the complete document.
pub fn (d Doc) to_burntsushi() string {
return d.to_burntsushi_(d.ast.table)
}
// to_burntsushi returns a BurntSushi compatible json string of the complete document.
fn (d Doc) to_burntsushi_(value ast.Value) string {
match value {
ast.Quoted {
// txt := .replace(r'\\','\\').replace(r'\"','"')
json_text := json2.Any(value.text).json_str()
return '{ "type": "string", "value": "$json_text" }'
}
ast.DateTime {
// Normalization for json
json_text := json2.Any(value.text).json_str().to_upper().replace(' ', 'T')
typ := if json_text.ends_with('Z') || json_text.all_after('T').contains('-')
|| json_text.all_after('T').contains('+') {
'datetime'
} else {
'datetime-local'
}
return '{ "type": "$typ", "value": "$json_text" }'
}
ast.Date {
json_text := json2.Any(value.text).json_str()
return '{ "type": "date-local", "value": "$json_text" }'
}
ast.Time {
json_text := json2.Any(value.text).json_str()
return '{ "type": "time-local", "value": "$json_text" }'
}
ast.Bool {
json_text := json2.Any(value.text.bool()).json_str()
return '{ "type": "bool", "value": "$json_text" }'
}
ast.Null {
json_text := json2.Any(value.text).json_str()
return '{ "type": "null", "value": "$json_text" }'
}
ast.Number {
if value.text.contains('.') || value.text.to_lower().contains('e') {
json_text := value.text.f64()
return '{ "type": "float", "value": "$json_text" }'
}
i64_ := strconv.parse_int(value.text, 0, 0) or { i64(0) }
return '{ "type": "integer", "value": "$i64_" }'
}
map[string]ast.Value {
mut str := '{ '
for key, val in value {
json_key := json2.Any(key).json_str()
str += ' "$json_key": ${d.to_burntsushi_(val)},'
// str += d.to_burntsushi_(val, indent+1)
}
str = str.trim_right(',')
str += ' }'
return str
}
[]ast.Value {
mut str := '[ '
for val in value {
str += ' ${d.to_burntsushi_(val)},'
}
str = str.trim_right(',')
str += ' ]\n'
return str
}
}
return '<error>'
}