Skip to content

Commit 1b028fb

Browse files
committed
Fixes ipfs#183. Per PR comments, not modifying the mutating opts object
1 parent b432553 commit 1b028fb

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

request.go

+17-26
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,24 @@ type Request struct {
2424

2525
// NewRequest returns a request initialized with given arguments
2626
// An non-nil error will be returned if the provided option values are invalid
27-
<<<<<<< HEAD
2827
func NewRequest(ctx context.Context,
29-
path []string, opts OptMap,
28+
path []string,
29+
opts OptMap,
3030
args []string,
3131
file files.Directory,
3232
root *Command,
3333
) (*Request, error) {
34-
=======
35-
func NewRequest(ctx context.Context, path []string, opts OptMap, args []string,
36-
file files.Directory, root *Command) (*Request, error) {
37-
>>>>>>> 90843374f694708a997c8e6e4d15142610c50cbf
38-
if opts == nil {
39-
opts = make(OptMap)
40-
}
4134

4235
cmd, err := root.Get(path)
4336
if err != nil {
4437
return nil, err
4538
}
4639

47-
<<<<<<< HEAD
48-
err = checkAndConvertOptions(root, opts, path)
49-
=======
50-
err = convertOptions(root, opts, path)
51-
>>>>>>> 90843374f694708a997c8e6e4d15142610c50cbf
40+
options, err := checkAndConvertOptions(root, opts, path)
41+
5242
req := &Request{
5343
Path: path,
54-
Options: opts,
44+
Options: options,
5545
Arguments: args,
5646
Files: file,
5747
Root: root,
@@ -109,14 +99,15 @@ func (req *Request) SetOption(name string, value interface{}) {
10999
return
110100
}
111101

112-
<<<<<<< HEAD
113-
func checkAndConvertOptions(root *Command, opts OptMap, path []string) error {
114-
=======
115-
func convertOptions(root *Command, opts OptMap, path []string) error {
116-
>>>>>>> 90843374f694708a997c8e6e4d15142610c50cbf
102+
func checkAndConvertOptions(root *Command, opts OptMap, path []string) (OptMap, error) {
117103
optDefs, err := root.GetOptions(path)
104+
options := make(OptMap)
105+
118106
if err != nil {
119-
return err
107+
return options, err
108+
}
109+
for k, v := range opts {
110+
options[k] = v
120111
}
121112

122113
for k, v := range opts {
@@ -134,26 +125,26 @@ func convertOptions(root *Command, opts OptMap, path []string) error {
134125
if len(str) == 0 {
135126
value = "empty value"
136127
}
137-
return fmt.Errorf("Could not convert %s to type %q (for option %q)",
128+
return options, fmt.Errorf("Could not convert %s to type %q (for option %q)",
138129
value, opt.Type().String(), "-"+k)
139130
}
140-
opts[k] = val
131+
options[k] = val
141132

142133
} else {
143-
return fmt.Errorf("Option %q should be type %q, but got type %q",
134+
return options, fmt.Errorf("Option %q should be type %q, but got type %q",
144135
k, opt.Type().String(), kind.String())
145136
}
146137
}
147138

148139
for _, name := range opt.Names() {
149140
if _, ok := opts[name]; name != k && ok {
150-
return fmt.Errorf("Duplicate command options were provided (%q and %q)",
141+
return options, fmt.Errorf("Duplicate command options were provided (%q and %q)",
151142
k, name)
152143
}
153144
}
154145
}
155146

156-
return nil
147+
return options, nil
157148
}
158149

159150
// GetEncoding returns the EncodingType set in a request, falling back to JSON

0 commit comments

Comments
 (0)