Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
e621aebc
提交
e621aebc
authored
11月 17, 2014
作者:
Juan Batiz-Benet
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #356 from jbenet/fix/option-cast
Fix Option Validation
上级
4af3e0ff
abb8374d
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
31 行增加
和
17 行删除
+31
-17
parse.go
commands/cli/parse.go
+4
-1
command_test.go
commands/command_test.go
+8
-8
parse.go
commands/http/parse.go
+4
-1
request.go
commands/request.go
+14
-6
response_test.go
commands/response_test.go
+1
-1
没有找到文件。
commands/cli/parse.go
浏览文件 @
e621aebc
...
...
@@ -46,7 +46,10 @@ func Parse(input []string, stdin *os.File, root *cmds.Command) (cmds.Request, *c
}
}
req
:=
cmds
.
NewRequest
(
path
,
opts
,
args
,
cmd
,
optDefs
)
req
,
err
:=
cmds
.
NewRequest
(
path
,
opts
,
args
,
cmd
,
optDefs
)
if
err
!=
nil
{
return
nil
,
cmd
,
path
,
err
}
err
=
cmd
.
CheckArguments
(
req
)
if
err
!=
nil
{
...
...
commands/command_test.go
浏览文件 @
e621aebc
...
...
@@ -17,21 +17,21 @@ func TestOptionValidation(t *testing.T) {
opts
,
_
:=
cmd
.
GetOptions
(
nil
)
req
:=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
,
_
:=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
.
SetOption
(
"beep"
,
true
)
res
:=
cmd
.
Call
(
req
)
if
res
.
Error
()
==
nil
{
t
.
Error
(
"Should have failed (incorrect type)"
)
}
req
=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
,
_
=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
.
SetOption
(
"beep"
,
5
)
res
=
cmd
.
Call
(
req
)
if
res
.
Error
()
!=
nil
{
t
.
Error
(
res
.
Error
(),
"Should have passed"
)
}
req
=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
,
_
=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
.
SetOption
(
"beep"
,
5
)
req
.
SetOption
(
"boop"
,
"test"
)
res
=
cmd
.
Call
(
req
)
...
...
@@ -39,7 +39,7 @@ func TestOptionValidation(t *testing.T) {
t
.
Error
(
"Should have passed"
)
}
req
=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
,
_
=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
.
SetOption
(
"b"
,
5
)
req
.
SetOption
(
"B"
,
"test"
)
res
=
cmd
.
Call
(
req
)
...
...
@@ -47,28 +47,28 @@ func TestOptionValidation(t *testing.T) {
t
.
Error
(
"Should have passed"
)
}
req
=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
,
_
=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
.
SetOption
(
"foo"
,
5
)
res
=
cmd
.
Call
(
req
)
if
res
.
Error
()
!=
nil
{
t
.
Error
(
"Should have passed"
)
}
req
=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
,
_
=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
.
SetOption
(
EncShort
,
"json"
)
res
=
cmd
.
Call
(
req
)
if
res
.
Error
()
!=
nil
{
t
.
Error
(
"Should have passed"
)
}
req
=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
,
_
=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
.
SetOption
(
"b"
,
"100"
)
res
=
cmd
.
Call
(
req
)
if
res
.
Error
()
!=
nil
{
t
.
Error
(
"Should have passed"
)
}
req
=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
,
_
=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
.
SetOption
(
"b"
,
":)"
)
res
=
cmd
.
Call
(
req
)
if
res
.
Error
()
==
nil
{
...
...
commands/http/parse.go
浏览文件 @
e621aebc
...
...
@@ -94,7 +94,10 @@ func Parse(r *http.Request, root *cmds.Command) (cmds.Request, error) {
return
nil
,
err
}
req
:=
cmds
.
NewRequest
(
path
,
opts
,
args
,
cmd
,
optDefs
)
req
,
err
:=
cmds
.
NewRequest
(
path
,
opts
,
args
,
cmd
,
optDefs
)
if
err
!=
nil
{
return
nil
,
err
}
err
=
cmd
.
CheckArguments
(
req
)
if
err
!=
nil
{
...
...
commands/request.go
浏览文件 @
e621aebc
...
...
@@ -223,8 +223,12 @@ func (r *request) ConvertOptions() error {
}
val
,
err
:=
convert
(
str
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Could not convert string value '%s' to type '%s'"
,
v
,
opt
.
Type
.
String
())
value
:=
fmt
.
Sprintf
(
"value '%v'"
,
v
)
if
len
(
str
)
==
0
{
value
=
"empty value"
}
return
fmt
.
Errorf
(
"Could not convert %s to type '%s' (for option '-%s')"
,
value
,
opt
.
Type
.
String
(),
k
)
}
r
.
options
[
k
]
=
val
...
...
@@ -248,12 +252,13 @@ func (r *request) ConvertOptions() error {
}
// NewEmptyRequest initializes an empty request
func
NewEmptyRequest
()
Request
{
func
NewEmptyRequest
()
(
Request
,
error
)
{
return
NewRequest
(
nil
,
nil
,
nil
,
nil
,
nil
)
}
// NewRequest returns a request initialized with given arguments
func
NewRequest
(
path
[]
string
,
opts
optMap
,
args
[]
interface
{},
cmd
*
Command
,
optDefs
map
[
string
]
Option
)
Request
{
// An non-nil error will be returned if the provided option values are invalid
func
NewRequest
(
path
[]
string
,
opts
optMap
,
args
[]
interface
{},
cmd
*
Command
,
optDefs
map
[
string
]
Option
)
(
Request
,
error
)
{
if
path
==
nil
{
path
=
make
([]
string
,
0
)
}
...
...
@@ -268,7 +273,10 @@ func NewRequest(path []string, opts optMap, args []interface{}, cmd *Command, op
}
req
:=
&
request
{
path
,
opts
,
args
,
cmd
,
Context
{},
optDefs
}
req
.
ConvertOptions
()
err
:=
req
.
ConvertOptions
()
if
err
!=
nil
{
return
nil
,
err
}
return
req
return
req
,
nil
}
commands/response_test.go
浏览文件 @
e621aebc
...
...
@@ -15,7 +15,7 @@ func TestMarshalling(t *testing.T) {
cmd
:=
&
Command
{}
opts
,
_
:=
cmd
.
GetOptions
(
nil
)
req
:=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
req
,
_
:=
NewRequest
(
nil
,
nil
,
nil
,
nil
,
opts
)
res
:=
NewResponse
(
req
)
res
.
SetOutput
(
TestOutput
{
"beep"
,
"boop"
,
1337
})
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论