From 047d2e2d6287b165f689d43b284336f5726bef51 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet <juan@benet.ai> Date: Thu, 13 Nov 2014 01:05:44 -0800 Subject: [PATCH] cmd2: Marshaller -> Marshaler (see golang/encoding) Also: - map[cmds.EncodingType]cmds.Marshaller -> MarshalMap cc @mappum @maybebtc --- cmd/ipfs2/main.go | 2 +- commands/command.go | 18 +++++++++++------- commands/response.go | 10 +++++----- core/commands2/add.go | 2 +- core/commands2/block.go | 2 +- core/commands2/bootstrap.go | 22 +++++++++++----------- core/commands2/commands.go | 2 +- core/commands2/config.go | 2 +- core/commands2/diag.go | 2 +- core/commands2/log.go | 4 ++-- core/commands2/ls.go | 2 +- core/commands2/mount_unix.go | 2 +- core/commands2/object.go | 2 +- core/commands2/publish.go | 2 +- core/commands2/refs.go | 2 +- core/commands2/resolve.go | 2 +- core/commands2/root.go | 2 +- core/commands2/update.go | 4 ++-- core/commands2/version.go | 2 +- 19 files changed, 45 insertions(+), 41 deletions(-) diff --git a/cmd/ipfs2/main.go b/cmd/ipfs2/main.go index 2c90cb012..782491e56 100644 --- a/cmd/ipfs2/main.go +++ b/cmd/ipfs2/main.go @@ -177,7 +177,7 @@ func (i *cmdInvocation) Parse(args []string) error { // if no encoding was specified by user, default to plaintext encoding // (if command doesn't support plaintext, use JSON instead) if !i.req.Option("encoding").Found() { - if i.req.Command().Marshallers != nil && i.req.Command().Marshallers[cmds.Text] != nil { + if i.req.Command().Marshalers != nil && i.req.Command().Marshalers[cmds.Text] != nil { i.req.SetOption("encoding", cmds.Text) } else { i.req.SetOption("encoding", cmds.JSON) diff --git a/commands/command.go b/commands/command.go index 2ce2cd382..0301e9d1b 100644 --- a/commands/command.go +++ b/commands/command.go @@ -16,9 +16,13 @@ var log = u.Logger("command") // It reads from the Request, and writes results to the Response. type Function func(Request) (interface{}, error) -// Marshaller is a function that takes in a Response, and returns a marshalled []byte +// Marshaler is a function that takes in a Response, and returns a marshalled []byte // (or an error on failure) -type Marshaller func(Response) ([]byte, error) +type Marshaler func(Response) ([]byte, error) + +// MarshalerMap is a map of Marshaler functions, keyed by EncodingType +// (or an error on failure) +type MarshalerMap map[EncodingType]Marshaler // HelpText is a set of strings used to generate command help text. The help // text follows formats similar to man pages, but not exactly the same. @@ -45,11 +49,11 @@ type HelpText struct { // Command is a runnable command, with input arguments and options (flags). // It can also have Subcommands, to group units of work into sets. type Command struct { - Options []Option - Arguments []Argument - Run Function - Marshallers map[EncodingType]Marshaller - Helptext HelpText + Options []Option + Arguments []Argument + Run Function + Marshalers map[EncodingType]Marshaler + Helptext HelpText // Type describes the type of the output of the Command's Run Function. // In precise terms, the value of Type is an instance of the return type of diff --git a/commands/response.go b/commands/response.go index 74b6b5e07..7e12e5922 100644 --- a/commands/response.go +++ b/commands/response.go @@ -40,7 +40,7 @@ const ( // TODO: support more encoding types ) -var marshallers = map[EncodingType]Marshaller{ +var marshallers = map[EncodingType]Marshaler{ JSON: func(res Response) ([]byte, error) { if res.Error() != nil { return json.MarshalIndent(res.Error(), "", " ") @@ -69,7 +69,7 @@ type Response interface { Output() interface{} // Marshal marshals out the response into a buffer. It uses the EncodingType - // on the Request to chose a Marshaller (Codec). + // on the Request to chose a Marshaler (Codec). Marshal() ([]byte, error) // Gets a io.Reader that reads the marshalled output @@ -122,9 +122,9 @@ func (r *response) Marshal() ([]byte, error) { return []byte(r.Error().Error()), nil } - var marshaller Marshaller - if r.req.Command() != nil && r.req.Command().Marshallers != nil { - marshaller = r.req.Command().Marshallers[encType] + var marshaller Marshaler + if r.req.Command() != nil && r.req.Command().Marshalers != nil { + marshaller = r.req.Command().Marshalers[encType] } if marshaller == nil { var ok bool diff --git a/core/commands2/add.go b/core/commands2/add.go index 8dfd26ad2..4af5aeb3a 100644 --- a/core/commands2/add.go +++ b/core/commands2/add.go @@ -171,7 +171,7 @@ remains to be implemented. // // return &AddOutput{added}, nil }, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ + Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { val, ok := res.Output().(*AddOutput) if !ok { diff --git a/core/commands2/block.go b/core/commands2/block.go index 3a5c5b225..84a3c304e 100644 --- a/core/commands2/block.go +++ b/core/commands2/block.go @@ -121,7 +121,7 @@ It reads from stdin, and <key> is a base58 encoded multihash. }, nil }, Type: &Block{}, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ + Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { block := res.Output().(*Block) s := fmt.Sprintf("Block added (%v bytes): %s\n", block.Length, block.Key) diff --git a/core/commands2/bootstrap.go b/core/commands2/bootstrap.go index 738dcb2a4..226b1f169 100644 --- a/core/commands2/bootstrap.go +++ b/core/commands2/bootstrap.go @@ -21,7 +21,7 @@ var peerOptionDesc = "A peer to add to the bootstrap list (in the format '<multi var bootstrapCmd = &cmds.Command{ Helptext: cmds.HelpText{ Tagline: "Show or edit the list of bootstrap peers", - Synopsis: ` + Synopsis: ` ipfs bootstrap list - Show peers in the bootstrap list ipfs bootstrap add <peer>... - Add peers to the bootstrap list ipfs bootstrap remove <peer>... - Removes peers from the bootstrap list @@ -31,9 +31,9 @@ Running 'ipfs bootstrap' with no arguments will run 'ipfs bootstrap list'. ` + bootstrapSecurityWarning, }, - Run: bootstrapListCmd.Run, - Marshallers: bootstrapListCmd.Marshallers, - Type: bootstrapListCmd.Type, + Run: bootstrapListCmd.Run, + Marshalers: bootstrapListCmd.Marshalers, + Type: bootstrapListCmd.Type, Subcommands: map[string]*cmds.Command{ "list": bootstrapListCmd, @@ -77,11 +77,11 @@ in the bootstrap list). return &BootstrapOutput{added}, nil }, Type: &BootstrapOutput{}, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ + Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { v := res.Output().(*BootstrapOutput) s := fmt.Sprintf("Added %v peers to the bootstrap list:\n", len(v.Peers)) - marshalled, err := bootstrapMarshaller(res) + marshalled, err := bootstrapMarshaler(res) if err != nil { return nil, err } @@ -124,11 +124,11 @@ var bootstrapRemoveCmd = &cmds.Command{ return &BootstrapOutput{removed}, nil }, Type: &BootstrapOutput{}, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ + Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { v := res.Output().(*BootstrapOutput) s := fmt.Sprintf("Removed %v peers from the bootstrap list:\n", len(v.Peers)) - marshalled, err := bootstrapMarshaller(res) + marshalled, err := bootstrapMarshaler(res) if err != nil { return nil, err } @@ -153,12 +153,12 @@ var bootstrapListCmd = &cmds.Command{ return &BootstrapOutput{peers}, nil }, Type: &BootstrapOutput{}, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ - cmds.Text: bootstrapMarshaller, + Marshalers: cmds.MarshalerMap{ + cmds.Text: bootstrapMarshaler, }, } -func bootstrapMarshaller(res cmds.Response) ([]byte, error) { +func bootstrapMarshaler(res cmds.Response) ([]byte, error) { v, ok := res.Output().(*BootstrapOutput) if !ok { return nil, u.ErrCast() diff --git a/core/commands2/commands.go b/core/commands2/commands.go index 8ce2b0e06..63124861d 100644 --- a/core/commands2/commands.go +++ b/core/commands2/commands.go @@ -25,7 +25,7 @@ func CommandsCmd(root *cmds.Command) *cmds.Command { root := cmd2outputCmd("ipfs", root) return &root, nil }, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ + Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { v := res.Output().(*Command) var buf bytes.Buffer diff --git a/core/commands2/config.go b/core/commands2/config.go index 5f7cfdd93..8a4b1bdb6 100644 --- a/core/commands2/config.go +++ b/core/commands2/config.go @@ -80,7 +80,7 @@ Set the value of the 'datastore.path' key: return getConfig(filename, key) } }, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ + Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { if len(res.Request().Arguments()) == 2 { return nil, nil // dont output anything diff --git a/core/commands2/diag.go b/core/commands2/diag.go index 8fd3db284..8a68a02ba 100644 --- a/core/commands2/diag.go +++ b/core/commands2/diag.go @@ -85,7 +85,7 @@ connected peers and latencies between them. return &DiagnosticOutput{output}, nil }, Type: &DiagnosticOutput{}, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ + Marshalers: cmds.MarshalerMap{ cmds.Text: func(r cmds.Response) ([]byte, error) { output, ok := r.Output().(*DiagnosticOutput) if !ok { diff --git a/core/commands2/log.go b/core/commands2/log.go index 157bdca27..ace008d9f 100644 --- a/core/commands2/log.go +++ b/core/commands2/log.go @@ -49,8 +49,8 @@ output of a running daemon. log.Info(s) return &MessageOutput{s}, nil }, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ - cmds.Text: MessageTextMarshaller, + Marshalers: cmds.MarshalerMap{ + cmds.Text: MessageTextMarshaler, }, Type: &MessageOutput{}, } diff --git a/core/commands2/ls.go b/core/commands2/ls.go index d948f635e..d07a207e8 100644 --- a/core/commands2/ls.go +++ b/core/commands2/ls.go @@ -73,7 +73,7 @@ it contains, with the following format: return &LsOutput{output}, nil }, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ + Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { s := "" output := res.Output().(*LsOutput).Objects diff --git a/core/commands2/mount_unix.go b/core/commands2/mount_unix.go index 490bce154..bc6e62541 100644 --- a/core/commands2/mount_unix.go +++ b/core/commands2/mount_unix.go @@ -88,7 +88,7 @@ not be listable, as it is virtual. Accessing known paths directly. } }, Type: &config.Mounts{}, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ + Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { v := res.Output().(*config.Mounts) s := fmt.Sprintf("IPFS mounted at: %s\n", v.IPFS) diff --git a/core/commands2/object.go b/core/commands2/object.go index dc5be0a0f..bf4a7cec6 100644 --- a/core/commands2/object.go +++ b/core/commands2/object.go @@ -150,7 +150,7 @@ This command outputs data in the following encodings: return node, nil }, Type: &Node{}, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ + Marshalers: cmds.MarshalerMap{ cmds.EncodingType("protobuf"): func(res cmds.Response) ([]byte, error) { object := res.Output().(*dag.Node) return object.Marshal() diff --git a/core/commands2/publish.go b/core/commands2/publish.go index ab0e81536..f7e76ba0a 100644 --- a/core/commands2/publish.go +++ b/core/commands2/publish.go @@ -78,7 +78,7 @@ Publish a <ref> to another public key: k := n.Identity.PrivKey() return publish(n, k, ref) }, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ + Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { v := res.Output().(*IpnsEntry) s := fmt.Sprintf("Published name %s to %s\n", v.Name, v.Value) diff --git a/core/commands2/refs.go b/core/commands2/refs.go index 00cb5e7b4..d3c243405 100644 --- a/core/commands2/refs.go +++ b/core/commands2/refs.go @@ -65,7 +65,7 @@ Note: list all refs recursively with -r. return getRefs(n, paths, unique, recursive) }, Type: &RefsOutput{}, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ + Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { output := res.Output().(*RefsOutput) s := "" diff --git a/core/commands2/resolve.go b/core/commands2/resolve.go index 0f9287303..ebd47ed96 100644 --- a/core/commands2/resolve.go +++ b/core/commands2/resolve.go @@ -73,7 +73,7 @@ Resolve te value of another name: return output, nil }, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ + Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { output := res.Output().(string) return []byte(output), nil diff --git a/core/commands2/root.go b/core/commands2/root.go index d2372dcc5..798bcd7cf 100644 --- a/core/commands2/root.go +++ b/core/commands2/root.go @@ -79,6 +79,6 @@ type MessageOutput struct { Message string } -func MessageTextMarshaller(res cmds.Response) ([]byte, error) { +func MessageTextMarshaler(res cmds.Response) ([]byte, error) { return []byte(res.Output().(*MessageOutput).Message), nil } diff --git a/core/commands2/update.go b/core/commands2/update.go index 8dbfb3e54..6a3c77a4e 100644 --- a/core/commands2/update.go +++ b/core/commands2/update.go @@ -32,7 +32,7 @@ var updateCmd = &cmds.Command{ "check": updateCheckCmd, "log": updateLogCmd, }, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ + Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { v := res.Output().(*UpdateOutput) s := "" @@ -65,7 +65,7 @@ Nothing will be downloaded or installed. return updateCheck(n) }, Type: &UpdateOutput{}, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ + Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { v := res.Output().(*UpdateOutput) s := "" diff --git a/core/commands2/version.go b/core/commands2/version.go index 575f57f6c..f9f5a33f3 100644 --- a/core/commands2/version.go +++ b/core/commands2/version.go @@ -23,7 +23,7 @@ var versionCmd = &cmds.Command{ Version: config.CurrentVersionNumber, }, nil }, - Marshallers: map[cmds.EncodingType]cmds.Marshaller{ + Marshalers: cmds.MarshalerMap{ cmds.Text: func(res cmds.Response) ([]byte, error) { v := res.Output().(*VersionOutput) s := "" -- GitLab