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

toml: fix trailing comma in inline toml, add test (#17977)

This commit is contained in:
Turiiya 2023-04-18 11:35:55 +02:00 committed by GitHub
parent 04dabb5485
commit a84fddbb91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 10 deletions

View File

@ -221,11 +221,8 @@ pub fn (m map[string]Any) as_strings() map[string]string {
pub fn (m map[string]Any) to_toml() string { pub fn (m map[string]Any) to_toml() string {
mut toml_text := '' mut toml_text := ''
for k, v in m { for k, v in m {
mut key := k key := if k.contains(' ') { '"${k}"' } else { k }
if key.contains(' ') { toml_text += '${key} = ${v.to_toml()}\n'
key = '"${key}"'
}
toml_text += '${key} = ' + v.to_toml() + '\n'
} }
toml_text = toml_text.trim_right('\n') toml_text = toml_text.trim_right('\n')
return toml_text return toml_text
@ -235,12 +232,12 @@ pub fn (m map[string]Any) to_toml() string {
// as an inline table encoded TOML `string`. // as an inline table encoded TOML `string`.
pub fn (m map[string]Any) to_inline_toml() string { pub fn (m map[string]Any) to_inline_toml() string {
mut toml_text := '{' mut toml_text := '{'
mut i := 1
for k, v in m { for k, v in m {
mut key := k key := if k.contains(' ') { '"${k}"' } else { k }
if key.contains(' ') { delimeter := if i < m.len { ',' } else { '' }
key = '"${key}"' toml_text += ' ${key} = ${v.to_toml()}${delimeter}'
} i++
toml_text += ' ${key} = ' + v.to_toml() + ','
} }
return toml_text + ' }' return toml_text + ' }'
} }

View File

@ -0,0 +1,16 @@
import toml
struct Address {
street string
city string
}
fn test_inline() {
a := Address{'Elm Street', 'Springwood'}
mut mp := map[string]toml.Any{}
mp['street'] = toml.Any(a.street)
mp['city'] = toml.Any(a.city)
assert mp.to_inline_toml() == '{ street = "Elm Street", city = "Springwood" }'
}