Skip to content

Commit ff22a37

Browse files
committed
fix: address review feedback and update doc
1 parent 33d1db5 commit ff22a37

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

flag_text.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import (
55
"strings"
66
)
77

8-
type TextMarshalUnmarshaller interface {
8+
type TextMarshalUnmarshaler interface {
99
encoding.TextMarshaler
1010
encoding.TextUnmarshaler
1111
}
1212

13-
// TextFlag enables you to set types that satisfies [TextMarshalUnmarshaller] using flags such as log levels.
14-
type TextFlag = FlagBase[TextMarshalUnmarshaller, StringConfig, TextValue]
13+
// TextFlag enables you to set types that satisfies [TextMarshalUnmarshaler] using flags such as log levels.
14+
type TextFlag = FlagBase[TextMarshalUnmarshaler, StringConfig, TextValue]
1515

1616
type TextValue struct {
17-
Value *TextMarshalUnmarshaller
17+
Value *TextMarshalUnmarshaler
1818
Config StringConfig
1919
}
2020

@@ -39,9 +39,11 @@ func (f TextValue) Get() any {
3939
return *f.Value
4040
}
4141

42-
func (f TextValue) Create(v TextMarshalUnmarshaller, p *TextMarshalUnmarshaller, c StringConfig) Value {
42+
func (f TextValue) Create(v TextMarshalUnmarshaler, p *TextMarshalUnmarshaler, c StringConfig) Value {
4343
if v != nil {
44-
*p = v
44+
b, _ := v.MarshalText()
45+
46+
_ = (*p).UnmarshalText(b)
4547
}
4648

4749
return &TextValue{
@@ -50,7 +52,7 @@ func (f TextValue) Create(v TextMarshalUnmarshaller, p *TextMarshalUnmarshaller,
5052
}
5153
}
5254

53-
func (f TextValue) ToString(v TextMarshalUnmarshaller) string {
55+
func (f TextValue) ToString(v TextMarshalUnmarshaler) string {
5456
text, err := v.MarshalText()
5557
if err != nil {
5658
return ""

flag_text_test.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@ func TestTextFlag(t *testing.T) {
3838
name: "empty",
3939
flag: TextFlag{
4040
Name: "log-level",
41-
Destination: ptr[TextMarshalUnmarshaller](&slog.LevelVar{}),
41+
Value: &slog.LevelVar{},
42+
Destination: ptr[TextMarshalUnmarshaler](&slog.LevelVar{}),
4243
},
4344
want: "INFO",
4445
},
4546
{
4647
name: "info",
4748
flag: TextFlag{
4849
Name: "log-level",
49-
Destination: ptr[TextMarshalUnmarshaller](&slog.LevelVar{}),
50-
Validator: func(v TextMarshalUnmarshaller) error {
50+
Value: &slog.LevelVar{},
51+
Destination: ptr[TextMarshalUnmarshaler](&slog.LevelVar{}),
52+
Validator: func(v TextMarshalUnmarshaler) error {
5153
text, err := v.MarshalText()
5254
if err != nil {
5355
return err
@@ -67,7 +69,8 @@ func TestTextFlag(t *testing.T) {
6769
name: "debug",
6870
flag: TextFlag{
6971
Name: "log-level",
70-
Destination: ptr[TextMarshalUnmarshaller](&slog.LevelVar{}),
72+
Value: &slog.LevelVar{},
73+
Destination: ptr[TextMarshalUnmarshaler](&slog.LevelVar{}),
7174
},
7275
args: []string{"--log-level", "debug"},
7376
want: "DEBUG",
@@ -76,7 +79,8 @@ func TestTextFlag(t *testing.T) {
7679
name: "debug_with_trim",
7780
flag: TextFlag{
7881
Name: "log-level",
79-
Destination: ptr[TextMarshalUnmarshaller](&slog.LevelVar{}),
82+
Value: &slog.LevelVar{},
83+
Destination: ptr[TextMarshalUnmarshaler](&slog.LevelVar{}),
8084
Config: StringConfig{TrimSpace: true},
8185
},
8286
args: []string{"--log-level", " debug "},
@@ -86,7 +90,8 @@ func TestTextFlag(t *testing.T) {
8690
name: "invalid",
8791
flag: TextFlag{
8892
Name: "log-level",
89-
Destination: ptr[TextMarshalUnmarshaller](&slog.LevelVar{}),
93+
Value: &slog.LevelVar{},
94+
Destination: ptr[TextMarshalUnmarshaler](&slog.LevelVar{}),
9095
},
9196
args: []string{"--log-level", "invalid"},
9297
want: "INFO",
@@ -97,7 +102,7 @@ func TestTextFlag(t *testing.T) {
97102
flag: TextFlag{
98103
Name: "text",
99104
Value: &badMarshaller{},
100-
Destination: ptr[TextMarshalUnmarshaller](&badMarshaller{}),
105+
Destination: ptr[TextMarshalUnmarshaler](&badMarshaller{}),
101106
},
102107
args: []string{"--text", "foo"},
103108
wantErr: true,
@@ -113,7 +118,7 @@ func TestTextFlag(t *testing.T) {
113118

114119
return &l
115120
}(),
116-
Destination: ptr[TextMarshalUnmarshaller](&slog.LevelVar{}),
121+
Destination: ptr[TextMarshalUnmarshaler](&slog.LevelVar{}),
117122
},
118123
args: []string{},
119124
want: "WARN",
@@ -129,7 +134,7 @@ func TestTextFlag(t *testing.T) {
129134

130135
return &l
131136
}(),
132-
Destination: ptr[TextMarshalUnmarshaller](&slog.LevelVar{}),
137+
Destination: ptr[TextMarshalUnmarshaler](&slog.LevelVar{}),
133138
},
134139
args: []string{"--log-level", "error"},
135140
want: "ERROR",
@@ -141,6 +146,9 @@ func TestTextFlag(t *testing.T) {
141146
for _, tt := range tests {
142147
t.Run(tt.name, func(t *testing.T) {
143148
set := flag.NewFlagSet(tt.name, flag.ContinueOnError)
149+
150+
// If the test is expected to result in an error then the test needs to make sure that the test output is
151+
// not littered with output that's expected but not relevant.
144152
if tt.wantErr {
145153
set.SetOutput(io.Discard)
146154
}
@@ -149,12 +157,13 @@ func TestTextFlag(t *testing.T) {
149157

150158
err := set.Parse(tt.args)
151159

152-
require.False(t, (err != nil) && !tt.wantErr, tt.name)
160+
// Ensure that there's only an error if we wanted an error.
161+
require.False(t, (err != nil) && !tt.wantErr)
153162

154-
if tt.flag.Value != nil {
155-
assert.Equal(t, tt.want, tt.flag.GetDefaultText())
156-
}
163+
assert.Equal(t, tt.flag.GetDefaultText(), set.Lookup(tt.flag.Name).DefValue)
157164

165+
// If the test is expected to fail and the code reaches this point then the test is successful and the test
166+
// must therefore conclude.
158167
if tt.wantErr {
159168
return
160169
}

godoc-current.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -977,8 +977,8 @@ type SuggestCommandFunc func(commands []*Command, provided string) string
977977

978978
type SuggestFlagFunc func(flags []Flag, provided string, hideHelp bool) string
979979

980-
type TextFlag = FlagBase[TextMarshalUnMarshaller, StringConfig, TextValue]
981-
TextFlag enables you to set types that satisfies TextMarshalUnMarshaller
980+
type TextFlag = FlagBase[TextMarshalUnmarshaler, StringConfig, TextValue]
981+
TextFlag enables you to set types that satisfies TextMarshalUnmarshaler
982982
using flags such as log levels.
983983

984984
type TextMarshalUnMarshaller interface {
@@ -987,19 +987,19 @@ type TextMarshalUnMarshaller interface {
987987
}
988988

989989
type TextValue struct {
990-
Value TextMarshalUnMarshaller
990+
Value *TextMarshalUnmarshaler
991991
Config StringConfig
992992
}
993993

994-
func (v TextValue) Create(t TextMarshalUnMarshaller, _ *TextMarshalUnMarshaller, c StringConfig) Value
994+
func (v TextValue) Create(t TextMarshalUnmarshaler, p *TextMarshalUnmarshaler, c StringConfig) Value
995995

996996
func (v TextValue) Get() any
997997

998998
func (v TextValue) Set(s string) error
999999

10001000
func (v TextValue) String() string
10011001

1002-
func (v TextValue) ToString(t TextMarshalUnMarshaller) string
1002+
func (v TextValue) ToString(t TextMarshalUnmarshaler) string
10031003

10041004
type TimestampArg = ArgumentBase[time.Time, TimestampConfig, timestampValue]
10051005

0 commit comments

Comments
 (0)