Repository navigation
Conversation
…form A slice flag splits its value on commas, and the map value source formatted whatever it found with %+v. A list therefore arrived as "[80 443]": the IntSliceFlag from the issue failed to parse it, and a StringSliceFlag kept the whole string as one element without an error. The value source now returns a slice or array in the serialized form SliceBase already parses, so the flag stops splitting it: an element that contains a comma, a custom SliceFlagSeparator and a disabled separator all arrive intact.
yxshee
left a comment
There was a problem hiding this comment.
I opened #2516, so I ran this against a few more config shapes. Keeping commas inside elements and honoring a custom or disabled separator is a real improvement over just joining the items with commas. The catch is that the serialized path skips the per-element parsing, and SliceBase.Set drops the json.Unmarshal error, so when the JSON types don't match the flag's element type the flag ends up with zero values and no error:
| map value | flag | this PR | joining with , |
|---|---|---|---|
[]any{80, 443} |
StringSliceFlag |
["" ""] |
["80" "443"] |
[]string{"80", "443"} |
IntSliceFlag |
[0 0] |
[80 443] |
[]any{"a", 2, true} |
StringSliceFlag |
["a" "" ""] |
["a" "2" "true"] |
[]any{-1} |
UintSliceFlag |
[0] |
parse error |
[]string{"x", "y"} |
StringFlag |
sl:::1793718754028232588:::["x","y"] |
x,y |
cmd.Run returned nil for all of these on this branch. The first row is probably the common one, since YAML like ports: [80, 443] decodes to []any of ints, and a string slice is a reasonable flag for it. The last row is the internal prefix leaking into a non-slice flag.
A couple of ways to keep the separator fix without this: in the serialized branch, unmarshal into []string (formatting each element with %+v on the value source side) and pass each item through the flag's value parser, the same way the split path does. At a minimum, returning the json.Unmarshal error there would turn the silent zeros into an error.
The tests in the PR pass for me, as do go test ./..., gofmt and go vet.
|
Both shapes are fixed in |
e0a3164 to
91614ff
Compare
|
Replaced Decoding into any concrete type ( The five rows behave as described above, everything is green, and the binary is back at its 2.0 MB. |
A list from a map source arrives serialized, and Set decoded it straight into the flag's slice with json.Unmarshal, dropping the error. A JSON number in a string slice became "", a JSON string in an int slice became 0, and a negative in an unsigned slice silently became 0 instead of failing. The serialized branch now splits the array and hands each element to the same value parser the split path uses, so mismatched types parse or report instead of turning into zero values. A scalar flag no longer sees the internal prefix: its elements are comma-joined before Set. The split scans the array rather than decoding it: a decode into any concrete type links the whole encoding/json decoder, which adds 300 KB to the example binary and fails `make check-binary-size`. A serialized slice is a flat array of scalars, so the scan covers every element Serialize can write.
91614ff to
458cde6
Compare
|
The branch head is |
What type of PR is this?
What this PR does / why we need it:
A slice flag splits its value on commas, and
mapValueSource.Lookupformatted whatever it found with%+v. A list in the map therefore reached the flag as"[80 443]": theIntSliceFlagfrom the issue fails to parse it, and aStringSliceFlagkeeps the whole string as one element without reporting an error.A comma-joined value would fix the reported case and break the next ones: an element that contains a comma is silently split, and
SliceFlagSeparator/DisableSliceFlagSeparatorchange the separator the flag expects. SoLookupreturns slices and arrays in the serialized formSliceBase.Setalready parses, which bypasses splitting entirely. Scalars format as before.Which issue(s) this PR fixes:
Fixes #2516
Tests
TestMapValueSourceFeedsSliceFlagsdrives a command with anIntSliceFlagand aStringSliceFlagfed from a map source, which is the reproduction in the issue, and checks the parsed values.TestMapValueSourceListSeparatorscovers an element containing a comma, a command withSliceFlagSeparator: ";"and one with the separator disabled. Reverting theLookupchange fails them with the error from the issue.Release Notes