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

Assert output types #514

Merged
merged 3 commits into from
Jan 7, 2015
Merged
Show file tree
Hide file tree
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
59 changes: 40 additions & 19 deletions commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,10 @@ func (c *Command) Call(req Request) Response {
return res
}

isChan := false
actualType := reflect.TypeOf(output)
if actualType != nil {
if actualType.Kind() == reflect.Ptr {
actualType = actualType.Elem()
}

// test if output is a channel
isChan = actualType.Kind() == reflect.Chan
}

// If the command specified an output type, ensure the actual value returned is of that type
if cmd.Type != nil && !isChan {
expectedType := reflect.TypeOf(cmd.Type)

if actualType != expectedType {
res.SetError(ErrIncorrectType, ErrNormal)
return res
}
err = checkType(output, cmd.Type)
if err != nil {
res.SetOutput(err)
return res
}

res.SetOutput(output)
Expand Down Expand Up @@ -256,6 +241,42 @@ func checkArgValue(v string, found bool, def Argument) error {
return nil
}

// checkType returns an error if the command returned a value different from
// what was specified in its Type field. It panics if the command returns a
// value that is not a pointer, io.Reader, or channel (as a message to the
// author of the command).
func checkType(output interface{}, t interface{}) error {
isChan := false
actualType := reflect.TypeOf(output)
if actualType != nil {
isChan = actualType.Kind() == reflect.Chan
isPointer := actualType.Kind() == reflect.Ptr
_, isReader := output.(io.Reader)

if !isReader {
if isChan || isPointer {
// use the dereferenced type, not the pointer type
actualType = actualType.Elem()
} else {
// ensures the developer of the command is returning a valid type
panic("Commands must return a pointer, io.Reader, or channel")
}
}
}

// If the command specified an output type, ensure the actual value returned is of that type
if t != nil && !isChan {
expectedType := reflect.TypeOf(t)

if actualType != expectedType {
// TODO: should this be a panic, too?
return ErrIncorrectType
}
}

return nil
}

func ClientError(msg string) error {
return &Error{Code: ErrClient, Message: msg}
}
2 changes: 1 addition & 1 deletion core/commands/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ var objectStatCmd = &cmds.Command{
return nil, err
}

return ns, nil
return &ns, nil
},
Type: dag.NodeStat{},
Marshalers: cmds.MarshalerMap{
Expand Down