Skip to content

Commit

Permalink
Fixes ipfs#183. Per PR comments, not modifying the mutating opts object
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakgarg committed Mar 27, 2020
1 parent b432553 commit 1b028fb
Showing 1 changed file with 17 additions and 26 deletions.
43 changes: 17 additions & 26 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,24 @@ 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
<<<<<<< HEAD
func NewRequest(ctx context.Context,
path []string, opts OptMap,
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) {
>>>>>>> 90843374f694708a997c8e6e4d15142610c50cbf
if opts == nil {
opts = make(OptMap)
}

cmd, err := root.Get(path)
if err != nil {
return nil, err
}

<<<<<<< HEAD
err = checkAndConvertOptions(root, opts, path)
=======
err = convertOptions(root, opts, path)
>>>>>>> 90843374f694708a997c8e6e4d15142610c50cbf
options, err := checkAndConvertOptions(root, opts, path)

req := &Request{
Path: path,
Options: opts,
Options: options,
Arguments: args,
Files: file,
Root: root,
Expand Down Expand Up @@ -109,14 +99,15 @@ func (req *Request) SetOption(name string, value interface{}) {
return
}

<<<<<<< HEAD
func checkAndConvertOptions(root *Command, opts OptMap, path []string) error {
=======
func convertOptions(root *Command, opts OptMap, path []string) error {
>>>>>>> 90843374f694708a997c8e6e4d15142610c50cbf
func checkAndConvertOptions(root *Command, opts OptMap, path []string) (OptMap, error) {
optDefs, err := root.GetOptions(path)
options := make(OptMap)

if err != nil {
return err
return options, err
}
for k, v := range opts {
options[k] = v
}

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

} else {
return fmt.Errorf("Option %q should be type %q, but got type %q",
return options, fmt.Errorf("Option %q should be type %q, but got type %q",
k, opt.Type().String(), kind.String())
}
}

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

return nil
return options, nil
}

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

0 comments on commit 1b028fb

Please sign in to comment.