Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#183 refactored the request options conversion code per the ticket requirements #187

Merged
merged 5 commits into from
Mar 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 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,27 +24,32 @@ 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) {
if opts == nil {
opts = make(OptMap)
}
func NewRequest(ctx context.Context,
path []string,
opts OptMap,
args []string,
file files.Directory,
root *Command,
) (*Request, error) {

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

options, err := checkAndConvertOptions(root, opts, path)

req := &Request{
Path: path,
Options: opts,
Options: options,
Arguments: args,
Files: file,
Root: root,
Command: cmd,
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 +99,18 @@ func (req *Request) SetOption(name string, value interface{}) {
return
}

func (req *Request) convertOptions(root *Command) error {
optDefs, err := root.GetOptions(req.Path)
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 req.Options {
for k, v := range opts {
opt, ok := optDefs[k]
if !ok {
continue
Expand All @@ -115,26 +125,26 @@ func (req *Request) convertOptions(root *Command) 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)
}
req.Options[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 := req.Options[name]; name != k && ok {
return fmt.Errorf("Duplicate command options were provided (%q and %q)",
if _, ok := options[name]; name != k && ok {
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