From dbf469e0001ef3641a5fb67bc644c58513d2a45f Mon Sep 17 00:00:00 2001 From: Larpon Date: Wed, 17 Nov 2021 16:25:50 +0100 Subject: [PATCH] toml: move to_burntsushi functionality (#12492) --- vlib/toml/tests/burntsushi.toml-test_test.v | 72 +++++++++++++++++++- vlib/toml/toml.v | 73 --------------------- 2 files changed, 71 insertions(+), 74 deletions(-) diff --git a/vlib/toml/tests/burntsushi.toml-test_test.v b/vlib/toml/tests/burntsushi.toml-test_test.v index a3242ebddd..ff77933e2d 100644 --- a/vlib/toml/tests/burntsushi.toml-test_test.v +++ b/vlib/toml/tests/burntsushi.toml-test_test.v @@ -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 '' +} diff --git a/vlib/toml/toml.v b/vlib/toml/toml.v index 79fc75e9cd..8989116f0a 100644 --- a/vlib/toml/toml.v +++ b/vlib/toml/toml.v @@ -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 '' -}