From ff95cf18d49e9294182e472c316b288b690b1cb1 Mon Sep 17 00:00:00 2001 From: Larpon Date: Wed, 24 Nov 2021 13:49:23 +0100 Subject: [PATCH] toml: expand short date time milliseconds in decoder (#12564) --- vlib/toml/ast/types.v | 3 +- vlib/toml/decoder/decoder.v | 38 +++++++++++++++++++-- vlib/toml/tests/burntsushi.toml-test_test.v | 2 -- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/vlib/toml/ast/types.v b/vlib/toml/ast/types.v index 515a7c6d2a..fd7ee5e0fe 100644 --- a/vlib/toml/ast/types.v +++ b/vlib/toml/ast/types.v @@ -231,8 +231,9 @@ pub fn (t Time) str() string { // DateTime is the data representation of a TOML date-time type (`YYYY-MM-DDTHH:MM:SS.milli`). // DateTime types can appear only as values in TOML documents. pub struct DateTime { -pub: +pub mut: text string +pub: pos token.Position date Date time Time diff --git a/vlib/toml/decoder/decoder.v b/vlib/toml/decoder/decoder.v index c3543c1dc4..042163c612 100644 --- a/vlib/toml/decoder/decoder.v +++ b/vlib/toml/decoder/decoder.v @@ -34,6 +34,10 @@ fn (d Decoder) modify(mut value ast.Value) ? { mut v := &(value as ast.Number) d.decode_number(mut v) ? } + ast.DateTime { + mut v := &(value as ast.DateTime) + d.decode_date_time(mut v) ? + } else {} } } @@ -205,9 +209,10 @@ pub fn decode_quoted_escapes(mut q ast.Quoted) ? { q.text = decoded_s } -// decode_unicode_escape returns an error if `esc_unicode` is not -// a valid Unicode escape sequence. `esc_unicode` is expected to be -// prefixed with either `u` or `U`. +// decode_unicode_escape decodes the Unicode escape sequence `esc_unicode`. +// The sequence is expected to be prefixed with either `u` or `U`. +// decode_unicode_escape returns the decoded rune as +// a string, it's integer value and it's length. fn decode_unicode_escape(esc_unicode string) ?(string, int, int) { is_long_esc_type := esc_unicode.starts_with('U') mut sequence := esc_unicode[1..] @@ -224,3 +229,30 @@ fn decode_unicode_escape(esc_unicode string) ?(string, int, int) { rn := rune(i64_val) return '$rn', int(i64_val), sequence_len } + +// decode_date_time decodes the `dt ast.DateTime`. +fn (d Decoder) decode_date_time(mut dt ast.DateTime) ? { + // Expand milliseconds that are only 1 char + if dt.text.contains('.') { + yymmddhhmmss := dt.text.all_before('.') + rest := dt.text.all_after('.') + z := if rest.contains('Z') { 'Z' } else { '' } + mut ms := rest + mut offset := '' + if rest.contains('+') { + offset = '+' + rest.all_after('+') + ms = rest.all_before('+') + } else if rest.contains('-') { + offset = '-' + rest.all_after('-') + ms = rest.all_before('-') + } + if z != '' { + ms = ms.replace('Z', '') + } + if ms.len > 1 { + return + } + ms = ms + '0'.repeat(6 - ms.len) + z + dt.text = yymmddhhmmss + '.' + ms + offset + } +} diff --git a/vlib/toml/tests/burntsushi.toml-test_test.v b/vlib/toml/tests/burntsushi.toml-test_test.v index f7cc234142..0eed4d5631 100644 --- a/vlib/toml/tests/burntsushi.toml-test_test.v +++ b/vlib/toml/tests/burntsushi.toml-test_test.v @@ -17,8 +17,6 @@ const ( valid_value_exceptions = [ // Integer 'integer/long.toml', // TODO awaits BUG fix with strconv.parse_int('-9223372036854775808') - // Date-time - 'datetime/milliseconds.toml', ] jq = os.find_abs_path_of_executable('jq') or { '' }