1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

toml: expand short date time milliseconds in decoder (#12564)

This commit is contained in:
Larpon 2021-11-24 13:49:23 +01:00 committed by GitHub
parent 6f46fc2170
commit ff95cf18d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 6 deletions

View File

@ -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

View File

@ -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
}
}

View File

@ -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 { '' }