1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-19 01:39:35 +08:00

commands: Changed Marshaler to return a io.Reader instead of a []byte

core/commands: Refactored command marshalers
This commit is contained in:
Matt Bell
2014-12-16 04:31:13 -08:00
parent 0395a7af1e
commit fd40702f73
19 changed files with 136 additions and 85 deletions

View File

@ -3,6 +3,7 @@ package commands
import (
"errors"
"fmt"
"io"
"reflect"
"strings"
@ -15,9 +16,9 @@ var log = u.Logger("command")
// It reads from the Request, and writes results to the Response.
type Function func(Request) (interface{}, error)
// Marshaler is a function that takes in a Response, and returns a marshalled []byte
// Marshaler is a function that takes in a Response, and returns an io.Reader
// (or an error on failure)
type Marshaler func(Response) ([]byte, error)
type Marshaler func(Response) (io.Reader, error)
// MarshalerMap is a map of Marshaler functions, keyed by EncodingType
// (or an error on failure)
@ -113,12 +114,16 @@ 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()
actualType := reflect.ValueOf(output).Type()
if definedType != actualType {
// 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.ValueOf(cmd.Type).Type()
if actualType != expectedType {
res.SetError(ErrIncorrectType, ErrNormal)
return res
}