Skip to content

Commit 3e64e17

Browse files
Merge pull request #96 from danielgtaylor/fix-send-defaults
fix: simplify option handling, send explicitly passed defaults
2 parents c094c86 + 94acf9e commit 3e64e17

File tree

3 files changed

+49
-35
lines changed

3 files changed

+49
-35
lines changed

cli/operation.go

+7-27
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"log"
77
"net/http"
88
"net/url"
9-
"reflect"
109
"strings"
1110

1211
"github.com/gosimple/slug"
@@ -74,18 +73,13 @@ func (o Operation) command() *cobra.Command {
7473

7574
query := url.Values{}
7675
for _, param := range o.QueryParams {
77-
flag := flags[param.Name]
78-
if reflect.ValueOf(flag).Elem().Interface() == param.Default {
79-
// No need to send the default value. Just skip it.
80-
continue
81-
}
82-
83-
if param.Default == nil && reflect.ValueOf(flag).Elem().IsZero() {
84-
// No explicit default, so the implied default is the zero value.
85-
// Again no need to send that default, so we skip.
76+
if !cmd.Flags().Changed(param.OptionName()) {
77+
// This option was not passed from the shell, so there is no need to
78+
// send it, even if it is the default or zero value.
8679
continue
8780
}
8881

82+
flag := flags[param.Name]
8983
for _, v := range param.Serialize(flag) {
9084
query.Add(param.Name, v)
9185
}
@@ -118,26 +112,12 @@ func (o Operation) command() *cobra.Command {
118112

119113
headers := http.Header{}
120114
for _, param := range o.HeaderParams {
121-
rv := reflect.ValueOf(flags[param.Name]).Elem()
122-
if rv.Interface() == param.Default {
123-
// No need to send the default value. Just skip it.
115+
if !cmd.Flags().Changed(param.OptionName()) {
116+
// This option was not passed from the shell, so there is no need to
117+
// send it, even if it is the default or zero value.
124118
continue
125119
}
126120

127-
if param.Default == nil {
128-
if rv.IsZero() {
129-
// No explicit default, so the implied default is the zero value.
130-
// Again no need to send that default, so we skip.
131-
continue
132-
}
133-
134-
if rv.Kind() == reflect.Slice && rv.Len() == 0 {
135-
// IsZero() above fails for empty arrays, so if it's empty let's
136-
// ignore it.
137-
continue
138-
}
139-
}
140-
141121
for _, v := range param.Serialize(flags[param.Name]) {
142122
headers.Add(param.Name, v)
143123
}

cli/operation_test.go

+35-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ import (
1313
func TestOperation(t *testing.T) {
1414
defer gock.Off()
1515

16-
gock.New("http://example2.com").Get("/prefix/test/id1").MatchParam("search", "foo").Reply(200).JSON(map[string]interface{}{
17-
"hello": "world",
18-
})
16+
gock.
17+
New("http://example2.com").
18+
Get("/prefix/test/id1").
19+
MatchParam("search", "foo").
20+
MatchParam("def3", "abc").
21+
MatchHeader("Accept", "application/json").
22+
Reply(200).
23+
JSON(map[string]interface{}{
24+
"hello": "world",
25+
})
1926

2027
op := Operation{
2128
Name: "test",
@@ -51,6 +58,29 @@ func TestOperation(t *testing.T) {
5158
Description: "desc",
5259
Default: "",
5360
},
61+
{
62+
Type: "string",
63+
Name: "def3",
64+
DisplayName: "def3",
65+
Description: "desc",
66+
Default: "abc",
67+
},
68+
},
69+
HeaderParams: []*Param{
70+
{
71+
Type: "string",
72+
Name: "Accept",
73+
DisplayName: "Accept",
74+
Description: "desc",
75+
Default: "application/json",
76+
},
77+
{
78+
Type: "string",
79+
Name: "Accept-Encoding",
80+
DisplayName: "Accept-Encoding",
81+
Description: "desc",
82+
Default: "gz",
83+
},
5484
},
5585
}
5686

@@ -63,9 +93,9 @@ func TestOperation(t *testing.T) {
6393
capture := &strings.Builder{}
6494
Stdout = capture
6595
Stderr = capture
66-
cmd.SetOut(Stdout)
96+
cmd.SetOutput(Stdout)
6797
viper.Set("rsh-server", "http://example2.com/prefix")
68-
cmd.Flags().Parse([]string{"--search=foo"})
98+
cmd.Flags().Parse([]string{"--search=foo", "--def-3=abc", "--accept=application/json"})
6999
cmd.Run(cmd, []string{"id1"})
70100

71101
assert.Equal(t, "HTTP/1.1 200 OK\nContent-Type: application/json\n\n{\n hello: \"world\"\n}\n", capture.String())

cli/param.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,18 @@ func (p Param) Serialize(value interface{}) []string {
9595
return nil
9696
}
9797

98-
// AddFlag adds a new option flag to a command's flag set for this parameter.
99-
func (p Param) AddFlag(flags *pflag.FlagSet) interface{} {
98+
// OptionName returns the commandline option name for this parameter.
99+
func (p Param) OptionName() string {
100100
name := p.Name
101101
if p.DisplayName != "" {
102102
name = p.DisplayName
103103
}
104-
name = strcase.ToDelimited(name, '-')
104+
return strcase.ToDelimited(name, '-')
105+
}
105106

107+
// AddFlag adds a new option flag to a command's flag set for this parameter.
108+
func (p Param) AddFlag(flags *pflag.FlagSet) interface{} {
109+
name := p.OptionName()
106110
def := p.Default
107111

108112
switch p.Type {

0 commit comments

Comments
 (0)