提交 3b407c70 作者: Matt Bell 提交者: Juan Batiz-Benet

commands: Ensure command output is correct type (if cmd.Type is set), resolves #321

上级 c9737760
......@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"reflect"
"strings"
u "github.com/jbenet/go-ipfs/util"
......@@ -64,6 +65,8 @@ var ErrNotCallable = errors.New("This command can't be called directly. Try one
var ErrNoFormatter = errors.New("This command cannot be formatted to plain text")
var ErrIncorrectType = errors.New("The command returned a value with a different type than expected")
// Call invokes the command for the given Request
func (c *Command) Call(req Request) Response {
res := NewResponse(req)
......@@ -106,6 +109,17 @@ func (c *Command) Call(req Request) Response {
return res
}
// If the command specified an output type, ensure the actual value returned is of that type
if cmd.Type != nil {
definedType := reflect.ValueOf(cmd.Type).Type()
actualType := reflect.ValueOf(output).Type()
if definedType != actualType {
res.SetError(ErrIncorrectType, ErrNormal)
return res
}
}
// clean up the request (close the readers, e.g. fileargs)
// NOTE: this means commands can't expect to keep reading after cmd.Run returns (in a goroutine)
err = req.Cleanup()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论