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
4373bad3
Commit
4373bad3
authored
Nov 24, 2020
by
Adin Schmahmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: support strings option over HTTP API
parent
bb36028e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
22 deletions
+43
-22
http/client.go
http/client.go
+9
-0
http/parse.go
http/parse.go
+14
-9
request.go
request.go
+20
-13
No files found.
http/client.go
View file @
4373bad3
...
...
@@ -230,6 +230,15 @@ func getQuery(req *cmds.Request) (string, error) {
if
OptionSkipMap
[
k
]
{
continue
}
optArr
,
ok
:=
v
.
([]
string
)
if
ok
{
for
_
,
o
:=
range
optArr
{
query
.
Add
(
k
,
o
)
}
continue
}
str
:=
fmt
.
Sprintf
(
"%v"
,
v
)
query
.
Set
(
k
,
str
)
}
...
...
http/parse.go
View file @
4373bad3
...
...
@@ -61,22 +61,28 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
}
opts
,
stringArgs2
:=
parseOptions
(
r
)
iopts
:=
make
(
map
[
string
]
interface
{},
len
(
opts
))
optDefs
,
err
:=
root
.
GetOptions
(
pth
)
if
err
!=
nil
{
return
nil
,
err
}
for
k
,
v
:=
range
opts
{
iopts
[
k
]
=
v
if
optDef
,
ok
:=
optDefs
[
k
];
ok
{
name
:=
optDef
.
Names
()[
0
]
if
k
!=
name
{
opts
[
name
]
=
v
delete
(
opts
,
k
)
iopts
[
name
]
=
v
delete
(
iopts
,
k
)
}
if
optDef
.
Type
()
!=
cmds
.
Strings
&&
len
(
v
)
>
0
{
iopts
[
name
]
=
v
[
0
]
}
}
}
// default to setting encoding to JSON
if
_
,
ok
:=
opts
[
cmds
.
EncLong
];
!
ok
{
opts
[
cmds
.
EncLong
]
=
cmds
.
JSON
if
_
,
ok
:=
i
opts
[
cmds
.
EncLong
];
!
ok
{
i
opts
[
cmds
.
EncLong
]
=
cmds
.
JSON
}
stringArgs
=
append
(
stringArgs
,
stringArgs2
...
)
...
...
@@ -148,7 +154,7 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
}
ctx
:=
logging
.
ContextWithLoggable
(
r
.
Context
(),
uuidLoggable
())
req
,
err
:=
cmds
.
NewRequest
(
ctx
,
pth
,
opts
,
args
,
f
,
root
)
req
,
err
:=
cmds
.
NewRequest
(
ctx
,
pth
,
i
opts
,
args
,
f
,
root
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -162,8 +168,8 @@ func parseRequest(r *http.Request, root *cmds.Command) (*cmds.Request, error) {
return
req
,
err
}
func
parseOptions
(
r
*
http
.
Request
)
(
map
[
string
]
interface
{}
,
[]
string
)
{
opts
:=
make
(
map
[
string
]
interface
{}
)
func
parseOptions
(
r
*
http
.
Request
)
(
map
[
string
]
[]
string
,
[]
string
)
{
opts
:=
make
(
map
[
string
]
[]
string
)
var
args
[]
string
query
:=
r
.
URL
.
Query
()
...
...
@@ -171,8 +177,7 @@ func parseOptions(r *http.Request) (map[string]interface{}, []string) {
if
k
==
"arg"
{
args
=
v
}
else
{
opts
[
k
]
=
v
[
0
]
opts
[
k
]
=
v
}
}
...
...
request.go
View file @
4373bad3
...
...
@@ -118,21 +118,28 @@ func checkAndConvertOptions(root *Command, opts OptMap, path []string) (OptMap,
kind
:=
reflect
.
TypeOf
(
v
)
.
Kind
()
if
kind
!=
opt
.
Type
()
{
if
str
,
ok
:=
v
.
(
string
);
ok
{
val
,
err
:=
opt
.
Parse
(
str
)
if
err
!=
nil
{
value
:=
fmt
.
Sprintf
(
"value %q"
,
v
)
if
len
(
str
)
==
0
{
value
=
"empty value"
}
return
options
,
fmt
.
Errorf
(
"could not convert %s to type %q (for option %q)"
,
value
,
opt
.
Type
()
.
String
(),
"-"
+
k
)
if
opt
.
Type
()
==
Strings
{
if
_
,
ok
:=
v
.
([]
string
);
!
ok
{
return
options
,
fmt
.
Errorf
(
"option %q should be type %q, but got type %q"
,
k
,
opt
.
Type
()
.
String
(),
kind
.
String
())
}
options
[
k
]
=
val
}
else
{
return
options
,
fmt
.
Errorf
(
"option %q should be type %q, but got type %q"
,
k
,
opt
.
Type
()
.
String
(),
kind
.
String
())
if
str
,
ok
:=
v
.
(
string
);
ok
{
val
,
err
:=
opt
.
Parse
(
str
)
if
err
!=
nil
{
value
:=
fmt
.
Sprintf
(
"value %q"
,
v
)
if
len
(
str
)
==
0
{
value
=
"empty value"
}
return
options
,
fmt
.
Errorf
(
"could not convert %s to type %q (for option %q)"
,
value
,
opt
.
Type
()
.
String
(),
"-"
+
k
)
}
options
[
k
]
=
val
}
else
{
return
options
,
fmt
.
Errorf
(
"option %q should be type %q, but got type %q"
,
k
,
opt
.
Type
()
.
String
(),
kind
.
String
())
}
}
}
...
...
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