mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
go2idl: remove comma-processing for comment-tags
It's simpler and clearer without them, and it leaves room for per-use-case parsing of the tag lines. If anything we should be splitting on whitespace.
This commit is contained in:
parent
d88fc84bae
commit
ec79eee289
@ -25,16 +25,18 @@ import (
|
|||||||
|
|
||||||
// ExtractCommentTags parses comments for lines of the form:
|
// ExtractCommentTags parses comments for lines of the form:
|
||||||
//
|
//
|
||||||
// 'marker' + "key1=value1,key2=value2".
|
// 'marker' + "key=value".
|
||||||
//
|
//
|
||||||
// Values are optional; "" is the default. A tag can be specified more than
|
// Values are optional; "" is the default. A tag can be specified more than
|
||||||
// one time and all values are returned. If the resulting map has an entry for
|
// one time and all values are returned. If the resulting map has an entry for
|
||||||
// a key, the value (a slice) is guaranteed to have at least 1 element.
|
// a key, the value (a slice) is guaranteed to have at least 1 element.
|
||||||
//
|
//
|
||||||
// Example: if you pass "+" for 'marker', and the following two lines are in
|
// Example: if you pass "+" for 'marker', and the following lines are in
|
||||||
// the comments:
|
// the comments:
|
||||||
// +foo=value1,bar
|
// +foo=value1
|
||||||
// +foo=value2,baz="qux"
|
// +bar
|
||||||
|
// +foo=value2
|
||||||
|
// +baz="qux"
|
||||||
// Then this function will return:
|
// Then this function will return:
|
||||||
// map[string][]string{"foo":{"value1, "value2"}, "bar": {""}, "baz": {"qux"}}
|
// map[string][]string{"foo":{"value1, "value2"}, "bar": {""}, "baz": {"qux"}}
|
||||||
func ExtractCommentTags(marker string, lines []string) map[string][]string {
|
func ExtractCommentTags(marker string, lines []string) map[string][]string {
|
||||||
@ -47,14 +49,12 @@ func ExtractCommentTags(marker string, lines []string) map[string][]string {
|
|||||||
if !strings.HasPrefix(line, marker) {
|
if !strings.HasPrefix(line, marker) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
pairs := strings.Split(line[len(marker):], ",")
|
// TODO: we could support multiple values per key if we split on spaces
|
||||||
for _, p := range pairs {
|
kv := strings.SplitN(line[len(marker):], "=", 2)
|
||||||
kv := strings.Split(p, "=")
|
if len(kv) == 2 {
|
||||||
if len(kv) == 2 {
|
out[kv[0]] = append(out[kv[0]], kv[1])
|
||||||
out[kv[0]] = append(out[kv[0]], kv[1])
|
} else if len(kv) == 1 {
|
||||||
} else if len(kv) == 1 {
|
out[kv[0]] = append(out[kv[0]], "")
|
||||||
out[kv[0]] = append(out[kv[0]], "")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
|
@ -25,12 +25,18 @@ import (
|
|||||||
func TestExtractCommentTags(t *testing.T) {
|
func TestExtractCommentTags(t *testing.T) {
|
||||||
commentLines := []string{
|
commentLines := []string{
|
||||||
"Human comment that is ignored.",
|
"Human comment that is ignored.",
|
||||||
"+foo=value1,bar",
|
"+foo=value1",
|
||||||
"+foo=value2,baz=qux",
|
"+bar",
|
||||||
|
"+foo=value2",
|
||||||
|
"+baz=qux,zrb=true",
|
||||||
}
|
}
|
||||||
|
|
||||||
a := ExtractCommentTags("+", commentLines)
|
a := ExtractCommentTags("+", commentLines)
|
||||||
e := map[string][]string{"foo": {"value1", "value2"}, "bar": {""}, "baz": {"qux"}}
|
e := map[string][]string{
|
||||||
|
"foo": {"value1", "value2"},
|
||||||
|
"bar": {""},
|
||||||
|
"baz": {"qux,zrb=true"},
|
||||||
|
}
|
||||||
if !reflect.DeepEqual(e, a) {
|
if !reflect.DeepEqual(e, a) {
|
||||||
t.Errorf("Wanted %q, got %q", e, a)
|
t.Errorf("Wanted %q, got %q", e, a)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user