Skip to content

Commit ee80c8e

Browse files
committed
Fixes #183 Now options are converted and saved before the request object is generated
1 parent 9b025d0 commit ee80c8e

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

request.go

+14-8
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,7 +24,12 @@ 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) {
27+
func NewRequest(ctx context.Context,
28+
path []string, opts OptMap,
29+
args []string,
30+
file files.Directory,
31+
root *Command,
32+
) (*Request, error) {
2833
if opts == nil {
2934
opts = make(OptMap)
3035
}
@@ -34,6 +39,7 @@ func NewRequest(ctx context.Context, path []string, opts OptMap, args []string,
3439
return nil, err
3540
}
3641

42+
err = checkAndConvertOptions(root, opts, path)
3743
req := &Request{
3844
Path: path,
3945
Options: opts,
@@ -44,7 +50,7 @@ func NewRequest(ctx context.Context, path []string, opts OptMap, args []string,
4450
Context: ctx,
4551
}
4652

47-
return req, req.convertOptions(root)
53+
return req, err
4854
}
4955

5056
// BodyArgs returns a scanner that returns arguments passed in the body as tokens.
@@ -94,13 +100,13 @@ func (req *Request) SetOption(name string, value interface{}) {
94100
return
95101
}
96102

97-
func (req *Request) convertOptions(root *Command) error {
98-
optDefs, err := root.GetOptions(req.Path)
103+
func checkAndConvertOptions(root *Command, opts OptMap, path []string) error {
104+
optDefs, err := root.GetOptions(path)
99105
if err != nil {
100106
return err
101107
}
102108

103-
for k, v := range req.Options {
109+
for k, v := range opts {
104110
opt, ok := optDefs[k]
105111
if !ok {
106112
continue
@@ -118,7 +124,7 @@ func (req *Request) convertOptions(root *Command) error {
118124
return fmt.Errorf("Could not convert %s to type %q (for option %q)",
119125
value, opt.Type().String(), "-"+k)
120126
}
121-
req.Options[k] = val
127+
opts[k] = val
122128

123129
} else {
124130
return fmt.Errorf("Option %q should be type %q, but got type %q",
@@ -127,7 +133,7 @@ func (req *Request) convertOptions(root *Command) error {
127133
}
128134

129135
for _, name := range opt.Names() {
130-
if _, ok := req.Options[name]; name != k && ok {
136+
if _, ok := opts[name]; name != k && ok {
131137
return fmt.Errorf("Duplicate command options were provided (%q and %q)",
132138
k, name)
133139
}

0 commit comments

Comments
 (0)