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:
parent
04dabb5485
commit
a84fddbb91
@ -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 + ' }'
|
||||||
}
|
}
|
||||||
|
16
vlib/toml/tests/inline_test.v
Normal file
16
vlib/toml/tests/inline_test.v
Normal 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" }'
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user