Skip to content

Commit

Permalink
ipfs#183 refactored the request options conversion code
Browse files Browse the repository at this point in the history
Now options are converted and saved before the request object is generated
  • Loading branch information
deepakgarg committed Mar 26, 2020
1 parent 9b025d0 commit 9084337
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"reflect"

"github.com/ipfs/go-ipfs-files"
files "github.com/ipfs/go-ipfs-files"
)

// Request represents a call to a command from a consumer
Expand All @@ -24,7 +24,8 @@ type Request struct {

// NewRequest returns a request initialized with given arguments
// An non-nil error will be returned if the provided option values are invalid
func NewRequest(ctx context.Context, path []string, opts OptMap, args []string, file files.Directory, root *Command) (*Request, error) {
func NewRequest(ctx context.Context, path []string, opts OptMap, args []string,
file files.Directory, root *Command) (*Request, error) {
if opts == nil {
opts = make(OptMap)
}
Expand All @@ -34,6 +35,7 @@ func NewRequest(ctx context.Context, path []string, opts OptMap, args []string,
return nil, err
}

err = convertOptions(root, opts, path)
req := &Request{
Path: path,
Options: opts,
Expand All @@ -44,7 +46,7 @@ func NewRequest(ctx context.Context, path []string, opts OptMap, args []string,
Context: ctx,
}

return req, req.convertOptions(root)
return req, err
}

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

func (req *Request) convertOptions(root *Command) error {
optDefs, err := root.GetOptions(req.Path)
func convertOptions(root *Command, opts OptMap, path []string) error {
optDefs, err := root.GetOptions(path)
if err != nil {
return err
}

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

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

for _, name := range opt.Names() {
if _, ok := req.Options[name]; name != k && ok {
if _, ok := opts[name]; name != k && ok {
return fmt.Errorf("Duplicate command options were provided (%q and %q)",
k, name)
}
Expand Down

0 comments on commit 9084337

Please sign in to comment.