Skip to content

Commit 9084337

Browse files
committed
#183 refactored the request options conversion code
Now options are converted and saved before the request object is generated
1 parent 9b025d0 commit 9084337

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

request.go

Lines changed: 10 additions & 8 deletions
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,8 @@ 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, path []string, opts OptMap, args []string,
28+
file files.Directory, root *Command) (*Request, error) {
2829
if opts == nil {
2930
opts = make(OptMap)
3031
}
@@ -34,6 +35,7 @@ func NewRequest(ctx context.Context, path []string, opts OptMap, args []string,
3435
return nil, err
3536
}
3637

38+
err = convertOptions(root, opts, path)
3739
req := &Request{
3840
Path: path,
3941
Options: opts,
@@ -44,7 +46,7 @@ func NewRequest(ctx context.Context, path []string, opts OptMap, args []string,
4446
Context: ctx,
4547
}
4648

47-
return req, req.convertOptions(root)
49+
return req, err
4850
}
4951

5052
// BodyArgs returns a scanner that returns arguments passed in the body as tokens.
@@ -94,13 +96,13 @@ func (req *Request) SetOption(name string, value interface{}) {
9496
return
9597
}
9698

97-
func (req *Request) convertOptions(root *Command) error {
98-
optDefs, err := root.GetOptions(req.Path)
99+
func convertOptions(root *Command, opts OptMap, path []string) error {
100+
optDefs, err := root.GetOptions(path)
99101
if err != nil {
100102
return err
101103
}
102104

103-
for k, v := range req.Options {
105+
for k, v := range opts {
104106
opt, ok := optDefs[k]
105107
if !ok {
106108
continue
@@ -118,7 +120,7 @@ func (req *Request) convertOptions(root *Command) error {
118120
return fmt.Errorf("Could not convert %s to type %q (for option %q)",
119121
value, opt.Type().String(), "-"+k)
120122
}
121-
req.Options[k] = val
123+
opts[k] = val
122124

123125
} else {
124126
return fmt.Errorf("Option %q should be type %q, but got type %q",
@@ -127,7 +129,7 @@ func (req *Request) convertOptions(root *Command) error {
127129
}
128130

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

0 commit comments

Comments
 (0)