Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3-cmds
Commits
ee80c8ee
Commit
ee80c8ee
authored
Mar 25, 2020
by
deepakgarg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #183 Now options are converted and saved before the request object is generated
parent
9b025d03
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
8 deletions
+14
-8
request.go
request.go
+14
-8
No files found.
request.go
View file @
ee80c8ee
...
...
@@ -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
...
...
@@ -24,7 +24,12 @@ 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
)
}
...
...
@@ -34,6 +39,7 @@ func NewRequest(ctx context.Context, path []string, opts OptMap, args []string,
return
nil
,
err
}
err
=
checkAndConvertOptions
(
root
,
opts
,
path
)
req
:=
&
Request
{
Path
:
path
,
Options
:
opts
,
...
...
@@ -44,7 +50,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.
...
...
@@ -94,13 +100,13 @@ func (req *Request) SetOption(name string, value interface{}) {
return
}
func
(
req
*
Request
)
c
onvertOptions
(
root
*
Command
)
error
{
optDefs
,
err
:=
root
.
GetOptions
(
req
.
P
ath
)
func
checkAndC
onvertOptions
(
root
*
Command
,
opts
OptMap
,
path
[]
string
)
error
{
optDefs
,
err
:=
root
.
GetOptions
(
p
ath
)
if
err
!=
nil
{
return
err
}
for
k
,
v
:=
range
req
.
Option
s
{
for
k
,
v
:=
range
opt
s
{
opt
,
ok
:=
optDefs
[
k
]
if
!
ok
{
continue
...
...
@@ -118,7 +124,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
.
Option
s
[
k
]
=
val
opt
s
[
k
]
=
val
}
else
{
return
fmt
.
Errorf
(
"Option %q should be type %q, but got type %q"
,
...
...
@@ -127,7 +133,7 @@ func (req *Request) convertOptions(root *Command) error {
}
for
_
,
name
:=
range
opt
.
Names
()
{
if
_
,
ok
:=
req
.
Option
s
[
name
];
name
!=
k
&&
ok
{
if
_
,
ok
:=
opt
s
[
name
];
name
!=
k
&&
ok
{
return
fmt
.
Errorf
(
"Duplicate command options were provided (%q and %q)"
,
k
,
name
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment