-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add custom
stringToString
parser
Fixes issues with the --values (-v) flag: 1. pflag's default parser doesn't support multi-line values. Conversely, the custom parser handles multiple values and single values spanning multiple lines based on the presence of `=`. 2. The default parser strips trailing `]` from values. I'll open a PR to fix this, but it may not be merged soon due to pflag's inactivity.
- Loading branch information
Showing
8 changed files
with
223 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package flag | ||
|
||
import ( | ||
"bytes" | ||
"encoding/csv" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"maps" | ||
"strings" | ||
) | ||
|
||
var ErrStringToStringFormat = errors.New("must be formatted as key=value") | ||
|
||
type StringToString struct { | ||
value map[string]string | ||
changed bool | ||
} | ||
|
||
// Set Format: a=1,b=2 | ||
func (s *StringToString) Set(val string) error { | ||
val = strings.TrimSpace(val) | ||
count := strings.Count(val, "=") | ||
records := make([]string, 0, count) | ||
switch count { | ||
case 0: | ||
return fmt.Errorf("%s %w", val, ErrStringToStringFormat) | ||
case 1: | ||
records = append(records, val) | ||
default: | ||
r := csv.NewReader(strings.NewReader(val)) | ||
r.TrimLeadingSpace = true | ||
for { | ||
line, err := r.Read() | ||
if err != nil { | ||
if errors.Is(err, io.EOF) { | ||
break | ||
} | ||
return err | ||
} | ||
|
||
r.FieldsPerRecord = 0 // Prevent wrong number of fields error | ||
|
||
for _, v := range line { | ||
switch { | ||
case strings.Contains(v, "="): | ||
records = append(records, v) | ||
case len(records) != 0: | ||
records[len(records)-1] += "\n" + v | ||
default: | ||
return fmt.Errorf("%s %w", v, ErrStringToStringFormat) | ||
} | ||
} | ||
} | ||
} | ||
|
||
result := make(map[string]string, len(records)) | ||
for _, pair := range records { | ||
kv := strings.SplitN(pair, "=", 2) | ||
if len(kv) != 2 { | ||
return fmt.Errorf("%s %w", pair, ErrStringToStringFormat) | ||
} | ||
result[kv[0]] = kv[1] | ||
} | ||
|
||
if s.changed { | ||
for k, v := range result { | ||
s.value[k] = v | ||
} | ||
} else { | ||
s.changed = true | ||
s.value = result | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *StringToString) Type() string { | ||
return "stringToString" | ||
} | ||
|
||
func (s *StringToString) String() string { | ||
records := make([]string, 0, len(s.value)) | ||
for k, v := range s.value { | ||
records = append(records, k+"="+v) | ||
} | ||
|
||
var buf bytes.Buffer | ||
w := csv.NewWriter(&buf) | ||
if err := w.Write(records); err != nil { | ||
panic(err) | ||
} | ||
w.Flush() | ||
return "[" + strings.TrimSpace(buf.String()) + "]" | ||
} | ||
|
||
func (s *StringToString) Values() map[string]string { | ||
return maps.Clone(s.value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package flag | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStringToString_Set(t *testing.T) { | ||
type args struct { | ||
val string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *StringToString | ||
wantErr require.ErrorAssertionFunc | ||
}{ | ||
{ | ||
"one value", | ||
args{"a=b"}, | ||
&StringToString{value: map[string]string{"a": "b"}, changed: true}, | ||
require.NoError, | ||
}, | ||
{ | ||
"two values", | ||
args{"a=b,c=d"}, | ||
&StringToString{value: map[string]string{"a": "b", "c": "d"}, changed: true}, | ||
require.NoError, | ||
}, | ||
{ | ||
"multiline value", | ||
args{"a=b\nc,d=e"}, | ||
&StringToString{value: map[string]string{"a": "b\nc", "d": "e"}, changed: true}, | ||
require.NoError, | ||
}, | ||
{ | ||
"multiline values", | ||
args{"a=b\nc=d"}, | ||
&StringToString{value: map[string]string{"a": "b", "c": "d"}, changed: true}, | ||
require.NoError, | ||
}, | ||
{ | ||
"multiple newlines", | ||
args{"a=b\n\nc=d"}, | ||
&StringToString{value: map[string]string{"a": "b", "c": "d"}, changed: true}, | ||
require.NoError, | ||
}, | ||
{ | ||
"trim spaces", | ||
args{"a=b\n c=d"}, | ||
&StringToString{value: map[string]string{"a": "b", "c": "d"}, changed: true}, | ||
require.NoError, | ||
}, | ||
{ | ||
"newline around values", | ||
args{"\na=b\nc=d\n"}, | ||
&StringToString{value: map[string]string{"a": "b", "c": "d"}, changed: true}, | ||
require.NoError, | ||
}, | ||
{ | ||
"json value", | ||
args{"a=[1]"}, | ||
&StringToString{value: map[string]string{"a": "[1]"}, changed: true}, | ||
require.NoError, | ||
}, | ||
{ | ||
"json values", | ||
args{"a=[1, 2, 3]"}, | ||
&StringToString{value: map[string]string{"a": "[1, 2, 3]"}, changed: true}, | ||
require.NoError, | ||
}, | ||
{"error empty", args{""}, &StringToString{}, require.Error}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
s := &StringToString{} | ||
tt.wantErr(t, s.Set(tt.args.val)) | ||
assert.Equal(t, tt.want, s) | ||
}) | ||
} | ||
|
||
t.Run("consecutive", func(t *testing.T) { | ||
s := &StringToString{} | ||
require.NoError(t, s.Set("a=b")) | ||
assert.True(t, s.changed) | ||
assert.Equal(t, map[string]string{"a": "b"}, s.value) | ||
require.NoError(t, s.Set("c=d")) | ||
assert.True(t, s.changed) | ||
assert.Equal(t, map[string]string{"a": "b", "c": "d"}, s.value) | ||
}) | ||
} | ||
|
||
func TestStringToString_String(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value *StringToString | ||
want string | ||
}{ | ||
{"empty", &StringToString{}, "[]"}, | ||
{"simple value", &StringToString{value: map[string]string{"a": "b"}, changed: true}, "[a=b]"}, | ||
{"value with comma", &StringToString{value: map[string]string{"a": "b,c"}, changed: true}, `["a=b,c"]`}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := tt.value.String() | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.