Skip to content

Commit 3de51c6

Browse files
authored
Merge pull request #187 from gargdeepak/fix/cmds/req-refactor
#183 refactored the request options conversion code per the ticket requirements
2 parents b060d75 + c9accce commit 3de51c6

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

request.go

+27-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"reflect"
77

8-
"github.com/ipfs/go-ipfs-files"
8+
files "github.com/ipfs/go-ipfs-files"
99
)
1010

1111
// Request represents a call to a command from a consumer
@@ -24,27 +24,32 @@ 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-
func NewRequest(ctx context.Context, path []string, opts OptMap, args []string, file files.Directory, root *Command) (*Request, error) {
28-
if opts == nil {
29-
opts = make(OptMap)
30-
}
27+
func NewRequest(ctx context.Context,
28+
path []string,
29+
opts OptMap,
30+
args []string,
31+
file files.Directory,
32+
root *Command,
33+
) (*Request, error) {
3134

3235
cmd, err := root.Get(path)
3336
if err != nil {
3437
return nil, err
3538
}
3639

40+
options, err := checkAndConvertOptions(root, opts, path)
41+
3742
req := &Request{
3843
Path: path,
39-
Options: opts,
44+
Options: options,
4045
Arguments: args,
4146
Files: file,
4247
Root: root,
4348
Command: cmd,
4449
Context: ctx,
4550
}
4651

47-
return req, req.convertOptions(root)
52+
return req, err
4853
}
4954

5055
// BodyArgs returns a scanner that returns arguments passed in the body as tokens.
@@ -94,13 +99,18 @@ func (req *Request) SetOption(name string, value interface{}) {
9499
return
95100
}
96101

97-
func (req *Request) convertOptions(root *Command) error {
98-
optDefs, err := root.GetOptions(req.Path)
102+
func checkAndConvertOptions(root *Command, opts OptMap, path []string) (OptMap, error) {
103+
optDefs, err := root.GetOptions(path)
104+
options := make(OptMap)
105+
99106
if err != nil {
100-
return err
107+
return options, err
108+
}
109+
for k, v := range opts {
110+
options[k] = v
101111
}
102112

103-
for k, v := range req.Options {
113+
for k, v := range opts {
104114
opt, ok := optDefs[k]
105115
if !ok {
106116
continue
@@ -115,26 +125,26 @@ func (req *Request) convertOptions(root *Command) error {
115125
if len(str) == 0 {
116126
value = "empty value"
117127
}
118-
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)",
119129
value, opt.Type().String(), "-"+k)
120130
}
121-
req.Options[k] = val
131+
options[k] = val
122132

123133
} else {
124-
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",
125135
k, opt.Type().String(), kind.String())
126136
}
127137
}
128138

129139
for _, name := range opt.Names() {
130-
if _, ok := req.Options[name]; name != k && ok {
131-
return fmt.Errorf("Duplicate command options were provided (%q and %q)",
140+
if _, ok := options[name]; name != k && ok {
141+
return options, fmt.Errorf("Duplicate command options were provided (%q and %q)",
132142
k, name)
133143
}
134144
}
135145
}
136146

137-
return nil
147+
return options, nil
138148
}
139149

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

0 commit comments

Comments
 (0)