Skip to content

Commit

Permalink
toml: fix trailing comma in inline toml, add test (#17977)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Apr 18, 2023
1 parent 04dabb5 commit a84fddb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
17 changes: 7 additions & 10 deletions vlib/toml/any.v
Expand Up @@ -221,11 +221,8 @@ pub fn (m map[string]Any) as_strings() map[string]string {
pub fn (m map[string]Any) to_toml() string {
mut toml_text := ''
for k, v in m {
mut key := k
if key.contains(' ') {
key = '"${key}"'
}
toml_text += '${key} = ' + v.to_toml() + '\n'
key := if k.contains(' ') { '"${k}"' } else { k }
toml_text += '${key} = ${v.to_toml()}\n'
}
toml_text = toml_text.trim_right('\n')
return toml_text
Expand All @@ -235,12 +232,12 @@ pub fn (m map[string]Any) to_toml() string {
// as an inline table encoded TOML `string`.
pub fn (m map[string]Any) to_inline_toml() string {
mut toml_text := '{'
mut i := 1
for k, v in m {
mut key := k
if key.contains(' ') {
key = '"${key}"'
}
toml_text += ' ${key} = ' + v.to_toml() + ','
key := if k.contains(' ') { '"${k}"' } else { k }
delimeter := if i < m.len { ',' } else { '' }
toml_text += ' ${key} = ${v.to_toml()}${delimeter}'
i++
}
return toml_text + ' }'
}
Expand Down
16 changes: 16 additions & 0 deletions vlib/toml/tests/inline_test.v
@@ -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" }'
}

0 comments on commit a84fddb

Please sign in to comment.