Skip to content

add a new type StringArray - #90

Merged
eparis merged 1 commit into
spf13:masterfrom
wu8685:string_array
Sep 6, 2016
Merged

eparis merged 1 commit into
spf13:masterfrom
wu8685:string_array

Conversation

@wu8685

@wu8685 wu8685 commented Aug 29, 2016

Copy link
Copy Markdown

fix #89

@wu8685

wu8685 commented Aug 29, 2016 •

Copy link
Copy Markdown
Author

It is not easy to get rid of CSV. I have to use CSVto combine all items of the array in StringArray into one string and split the string using CVS to get the []string value which will be returned by GetStringArray().

Considering this test case, I also have to use CSV to deal with "

Comment thread string_array_test.go Outdated
f := setUpSAFlagSet(&sa)

in := []string{"one,two", `"three"`, `"four,five",six`, `"seven eight"`}
expected := []string{"one,two", `three`, `"four,five",six`, "seven eight"}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is removing the " around three the correct thing to do with your new 'exact' flag parser?

@wu8685 wu8685 Aug 29, 2016 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess. The same as --value="seven eight". I suppose the quote here is to escape.
If the raw format is expected, it will save some of my work.

@fabianofranz

Copy link
Copy Markdown

I'd expect the StringArray implementation to be much simpler, really raw, just use the values as they come. I may be missing something, but I don't get why you still need CSV. For example, Set should be not more than taking the value and append to the slice

func (s *stringArrayValue) Set(val string) error {
  if !s.changed {
    *s.value = []string{val}
    s.changed = true
  } else {
    *s.value = append(*s.value, val)
  }
  return nil
}

@eparis

eparis commented Aug 30, 2016

Copy link
Copy Markdown
Collaborator

@fabianofranz the problem is that the GetStringArray() function requires one to be able to parse the results of String() which means the output of String() must be encoded in some sane way (CSV). If we don't encode on Set one would get a slightly different result via the variable bound to the flag and the result of GetStringArray()....

@wu8685

wu8685 commented Aug 30, 2016 •

Copy link
Copy Markdown
Author

@fabianofranz If parsing argument as raw data, these input --value=one,two --value="three" --value="seven eight" would be parsed as one,two, "three", "seven eight". It is a little strange. But I am ok, if this is expected.

The last test case is about this.

@fabianofranz

Copy link
Copy Markdown

@wu8685 That will only happen in tests. In the real world when using bash, if you take the raw values (I didn't test, but afaik that's what will happen)

--value=one,two --value="three" --value="seven eight"

will become

one,two, three, and seven eight

Because bash already does some handling of quotes, double-quotes, spaces, etc; before sending the value to the program. If you do echo "foo bar" you get foo bar, not "foo bar".

See, the reason for me arguing about this is that I wouldn't expect any additional magic from the non-CSV version. The program takes the values exactly as provided, I know my bash will do some pre-processing, but other than that I don't expect other surprises. Exactly like every other flag works - the simple String one, for example, doesn't do any CSV, quotes or spaces processing. It just takes values as is. And this one, StringArray, is just a collection of raw Strings.

@wu8685

wu8685 commented Sep 5, 2016

Copy link
Copy Markdown
Author

@fabianofranz Thanks for your explanation. I tested in bash. It proves you are right. I removed the CSV code within Set()

Comment thread string_array.go Outdated
return "[" + str + "]"
}

func writeAsCSV(vals []string) (string, error) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this to string_slice.go and make stringSliceValue.String() to use this. Both writeAsCSV and readAsCSV will be in the same place.

@fabianofranz

Copy link
Copy Markdown

@wu8685 one minor comment then LGTM. @eparis PTAL?

@eparis
eparis merged commit 6fd2ff4 into spf13:master Sep 6, 2016
@wu8685
wu8685 deleted the string_array branch September 13, 2016 00:05
gilbsgilbs added a commit to gilbsgilbs/caddy that referenced this pull request Sep 20, 2025
`pflag.GetStringSlice` treats commas as delimiters, which causes issues
when passing headers whose values contain commas (`X-Robots-Tag:
noindex, nofollow`). These are incorrectly split into multiple headers
and errors out:

- `X-Robots-Tag: noindex`
- ` nofollow`

Switch to `pflag.GetStringArray`, which does not split on commas[1].

Note that this changes behavior for cases where multiple headers were
provided in a single argument with commas (`--header-down "X-Foo:
Bar,X-Bar: Foo"`). Such cases will now be treated as a single header
value. If this breaking change is unacceptable, we will need a smarter
fallback mechanism.

[1] spf13/pflag#90
mholt pushed a commit to caddyserver/caddy that referenced this pull request Sep 23, 2025
`pflag.GetStringSlice` treats commas as delimiters, which causes issues
when passing headers whose values contain commas (`X-Robots-Tag:
noindex, nofollow`). These are incorrectly split into multiple headers
and errors out:

- `X-Robots-Tag: noindex`
- ` nofollow`

Switch to `pflag.GetStringArray`, which does not split on commas[1].

Note that this changes behavior for cases where multiple headers were
provided in a single argument with commas (`--header-down "X-Foo:
Bar,X-Bar: Foo"`). Such cases will now be treated as a single header
value. If this breaking change is unacceptable, we will need a smarter
fallback mechanism.

[1] spf13/pflag#90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a new type StringArray to handle multiple occurances of a flag without comma separation

3 participants