1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-20 02:21:48 +08:00

commands: Replaced 'Formatter' with 'Marshaller'

This commit is contained in:
Matt Bell
2014-11-03 23:02:50 -08:00
committed by Juan Batiz-Benet
parent 068e10cc5d
commit 0149f65c6c
2 changed files with 5 additions and 10 deletions

View File

@ -15,10 +15,9 @@ var log = u.Logger("command")
// It reads from the Request, and writes results to the Response. // It reads from the Request, and writes results to the Response.
type Function func(Response, Request) type Function func(Response, Request)
// Formatter is a function that takes in a Response, and returns a human-readable string // Marshaller is a function that takes in a Response, and returns a marshalled []byte
// (or an error on failure) // (or an error on failure)
// MAYBE_TODO: maybe this should be a io.Reader instead of a string? type Marshaller func(Response) ([]byte, error)
type Formatter func(Response) (string, error)
// TODO: check Argument definitions when creating a Command // TODO: check Argument definitions when creating a Command
// (might need to use a Command constructor) // (might need to use a Command constructor)
@ -33,7 +32,7 @@ type Command struct {
Options []Option Options []Option
Arguments []Argument Arguments []Argument
Run Function Run Function
Format Formatter Format Marshaller
Type interface{} Type interface{}
Subcommands map[string]*Command Subcommands map[string]*Command
} }

View File

@ -40,10 +40,6 @@ const (
// TODO: support more encoding types // TODO: support more encoding types
) )
// Marshaller is a function used by coding types.
// TODO this should just be a `coding.Codec`
type Marshaller func(res Response) ([]byte, error)
var marshallers = map[EncodingType]Marshaller{ var marshallers = map[EncodingType]Marshaller{
JSON: func(res Response) ([]byte, error) { JSON: func(res Response) ([]byte, error) {
if res.Error() != nil { if res.Error() != nil {
@ -63,11 +59,11 @@ var marshallers = map[EncodingType]Marshaller{
return nil, ErrNoFormatter return nil, ErrNoFormatter
} }
s, err := format(res) bytes, err := format(res)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return []byte(s), nil return bytes, nil
}, },
} }