cmd2: Marshaller -> Marshaler (see golang/encoding)
Also: - map[cmds.EncodingType]cmds.Marshaller -> MarshalMap cc @mappum @maybebtc
Juan Batiz-Benet committed
Nov 13, 2014 at 01:05 UTC
047d2e2d6287b165f689d43b284336f5726bef51
19 files changed
+45
-41
cmd/ipfs2/main.go
+1
-1
@@ -177,7 +177,7 @@ func (i *cmdInvocation) Parse(args []string) error {
177
// if no encoding was specified by user, default to plaintext encoding
178
// (if command doesn't support plaintext, use JSON instead)
179
if !i.req.Option("encoding").Found() {
180
- if i.req.Command().Marshallers != nil && i.req.Command().Marshallers[cmds.Text] != nil {
180
+ if i.req.Command().Marshalers != nil && i.req.Command().Marshalers[cmds.Text] != nil {
181
i.req.SetOption("encoding", cmds.Text)
182
} else {
183
i.req.SetOption("encoding", cmds.JSON)
commands/command.go
+11
-7
@@ -16,9 +16,13 @@ var log = u.Logger("command")
16
// It reads from the Request, and writes results to the Response.
17
type Function func(Request) (interface{}, error)
18
19
-// Marshaller is a function that takes in a Response, and returns a marshalled []byte
19
+// Marshaler is a function that takes in a Response, and returns a marshalled []byte
20
// (or an error on failure)
21
-type Marshaller func(Response) ([]byte, error)
21
+type Marshaler func(Response) ([]byte, error)
22
+
23
+// MarshalerMap is a map of Marshaler functions, keyed by EncodingType
24
+// (or an error on failure)
25
+type MarshalerMap map[EncodingType]Marshaler
26
27
// HelpText is a set of strings used to generate command help text. The help
28
// text follows formats similar to man pages, but not exactly the same.
@@ -45,11 +49,11 @@ type HelpText struct {
49
// Command is a runnable command, with input arguments and options (flags).
50
// It can also have Subcommands, to group units of work into sets.
51
type Command struct {
48
- Options []Option
49
- Arguments []Argument
50
- Run Function
51
- Marshallers map[EncodingType]Marshaller
52
- Helptext HelpText
52
+ Options []Option
53
+ Arguments []Argument
54
+ Run Function
55
+ Marshalers map[EncodingType]Marshaler
56
+ Helptext HelpText
57
58
// Type describes the type of the output of the Command's Run Function.
59
// In precise terms, the value of Type is an instance of the return type of
commands/response.go
+5
-5
@@ -40,7 +40,7 @@ const (
40
// TODO: support more encoding types
41
)
42
43
-var marshallers = map[EncodingType]Marshaller{
43
+var marshallers = map[EncodingType]Marshaler{
44
JSON: func(res Response) ([]byte, error) {
45
if res.Error() != nil {
46
return json.MarshalIndent(res.Error(), "", " ")
@@ -69,7 +69,7 @@ type Response interface {
69
Output() interface{}
70
71
// Marshal marshals out the response into a buffer. It uses the EncodingType
72
- // on the Request to chose a Marshaller (Codec).
72
+ // on the Request to chose a Marshaler (Codec).
73
Marshal() ([]byte, error)
74
75
// Gets a io.Reader that reads the marshalled output
@@ -122,9 +122,9 @@ func (r *response) Marshal() ([]byte, error) {
122
return []byte(r.Error().Error()), nil
123
}
124
125
- var marshaller Marshaller
126
- if r.req.Command() != nil && r.req.Command().Marshallers != nil {
127
- marshaller = r.req.Command().Marshallers[encType]
125
+ var marshaller Marshaler
126
+ if r.req.Command() != nil && r.req.Command().Marshalers != nil {
127
+ marshaller = r.req.Command().Marshalers[encType]
128
}
129
if marshaller == nil {
130
var ok bool
core/commands2/add.go
+1
-1
@@ -171,7 +171,7 @@ remains to be implemented.
171
//
172
// return &AddOutput{added}, nil
173
},
174
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
174
+ Marshalers: cmds.MarshalerMap{
175
cmds.Text: func(res cmds.Response) ([]byte, error) {
176
val, ok := res.Output().(*AddOutput)
177
if !ok {
core/commands2/block.go
+1
-1
@@ -121,7 +121,7 @@ It reads from stdin, and <key> is a base58 encoded multihash.
121
}, nil
122
},
123
Type: &Block{},
124
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
124
+ Marshalers: cmds.MarshalerMap{
125
cmds.Text: func(res cmds.Response) ([]byte, error) {
126
block := res.Output().(*Block)
127
s := fmt.Sprintf("Block added (%v bytes): %s\n", block.Length, block.Key)
core/commands2/bootstrap.go
+11
-11
@@ -21,7 +21,7 @@ var peerOptionDesc = "A peer to add to the bootstrap list (in the format '<multi
21
var bootstrapCmd = &cmds.Command{
22
Helptext: cmds.HelpText{
23
Tagline: "Show or edit the list of bootstrap peers",
24
- Synopsis: `
24
+ Synopsis: `
25
ipfs bootstrap list - Show peers in the bootstrap list
26
ipfs bootstrap add <peer>... - Add peers to the bootstrap list
27
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'.
31
` + bootstrapSecurityWarning,
32
},
33
34
- Run: bootstrapListCmd.Run,
35
- Marshallers: bootstrapListCmd.Marshallers,
36
- Type: bootstrapListCmd.Type,
34
+ Run: bootstrapListCmd.Run,
35
+ Marshalers: bootstrapListCmd.Marshalers,
36
+ Type: bootstrapListCmd.Type,
37
38
Subcommands: map[string]*cmds.Command{
39
"list": bootstrapListCmd,
@@ -77,11 +77,11 @@ in the bootstrap list).
77
return &BootstrapOutput{added}, nil
78
},
79
Type: &BootstrapOutput{},
80
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
80
+ Marshalers: cmds.MarshalerMap{
81
cmds.Text: func(res cmds.Response) ([]byte, error) {
82
v := res.Output().(*BootstrapOutput)
83
s := fmt.Sprintf("Added %v peers to the bootstrap list:\n", len(v.Peers))
84
- marshalled, err := bootstrapMarshaller(res)
84
+ marshalled, err := bootstrapMarshaler(res)
85
if err != nil {
86
return nil, err
87
}
@@ -124,11 +124,11 @@ var bootstrapRemoveCmd = &cmds.Command{
124
return &BootstrapOutput{removed}, nil
125
},
126
Type: &BootstrapOutput{},
127
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
127
+ Marshalers: cmds.MarshalerMap{
128
cmds.Text: func(res cmds.Response) ([]byte, error) {
129
v := res.Output().(*BootstrapOutput)
130
s := fmt.Sprintf("Removed %v peers from the bootstrap list:\n", len(v.Peers))
131
- marshalled, err := bootstrapMarshaller(res)
131
+ marshalled, err := bootstrapMarshaler(res)
132
if err != nil {
133
return nil, err
134
}
@@ -153,12 +153,12 @@ var bootstrapListCmd = &cmds.Command{
153
return &BootstrapOutput{peers}, nil
154
},
155
Type: &BootstrapOutput{},
156
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
157
- cmds.Text: bootstrapMarshaller,
156
+ Marshalers: cmds.MarshalerMap{
157
+ cmds.Text: bootstrapMarshaler,
158
},
159
}
160
161
-func bootstrapMarshaller(res cmds.Response) ([]byte, error) {
161
+func bootstrapMarshaler(res cmds.Response) ([]byte, error) {
162
v, ok := res.Output().(*BootstrapOutput)
163
if !ok {
164
return nil, u.ErrCast()
core/commands2/commands.go
+1
-1
@@ -25,7 +25,7 @@ func CommandsCmd(root *cmds.Command) *cmds.Command {
25
root := cmd2outputCmd("ipfs", root)
26
return &root, nil
27
},
28
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
28
+ Marshalers: cmds.MarshalerMap{
29
cmds.Text: func(res cmds.Response) ([]byte, error) {
30
v := res.Output().(*Command)
31
var buf bytes.Buffer
core/commands2/config.go
+1
-1
@@ -80,7 +80,7 @@ Set the value of the 'datastore.path' key:
80
return getConfig(filename, key)
81
}
82
},
83
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
83
+ Marshalers: cmds.MarshalerMap{
84
cmds.Text: func(res cmds.Response) ([]byte, error) {
85
if len(res.Request().Arguments()) == 2 {
86
return nil, nil // dont output anything
core/commands2/diag.go
+1
-1
@@ -85,7 +85,7 @@ connected peers and latencies between them.
85
return &DiagnosticOutput{output}, nil
86
},
87
Type: &DiagnosticOutput{},
88
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
88
+ Marshalers: cmds.MarshalerMap{
89
cmds.Text: func(r cmds.Response) ([]byte, error) {
90
output, ok := r.Output().(*DiagnosticOutput)
91
if !ok {
core/commands2/log.go
+2
-2
@@ -49,8 +49,8 @@ output of a running daemon.
49
log.Info(s)
50
return &MessageOutput{s}, nil
51
},
52
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
53
- cmds.Text: MessageTextMarshaller,
52
+ Marshalers: cmds.MarshalerMap{
53
+ cmds.Text: MessageTextMarshaler,
54
},
55
Type: &MessageOutput{},
56
}
core/commands2/ls.go
+1
-1
@@ -73,7 +73,7 @@ it contains, with the following format:
73
74
return &LsOutput{output}, nil
75
},
76
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
76
+ Marshalers: cmds.MarshalerMap{
77
cmds.Text: func(res cmds.Response) ([]byte, error) {
78
s := ""
79
output := res.Output().(*LsOutput).Objects
core/commands2/mount_unix.go
+1
-1
@@ -88,7 +88,7 @@ not be listable, as it is virtual. Accessing known paths directly.
88
}
89
},
90
Type: &config.Mounts{},
91
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
91
+ Marshalers: cmds.MarshalerMap{
92
cmds.Text: func(res cmds.Response) ([]byte, error) {
93
v := res.Output().(*config.Mounts)
94
s := fmt.Sprintf("IPFS mounted at: %s\n", v.IPFS)
core/commands2/object.go
+1
-1
@@ -150,7 +150,7 @@ This command outputs data in the following encodings:
150
return node, nil
151
},
152
Type: &Node{},
153
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
153
+ Marshalers: cmds.MarshalerMap{
154
cmds.EncodingType("protobuf"): func(res cmds.Response) ([]byte, error) {
155
object := res.Output().(*dag.Node)
156
return object.Marshal()
core/commands2/publish.go
+1
-1
@@ -78,7 +78,7 @@ Publish a <ref> to another public key:
78
k := n.Identity.PrivKey()
79
return publish(n, k, ref)
80
},
81
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
81
+ Marshalers: cmds.MarshalerMap{
82
cmds.Text: func(res cmds.Response) ([]byte, error) {
83
v := res.Output().(*IpnsEntry)
84
s := fmt.Sprintf("Published name %s to %s\n", v.Name, v.Value)
core/commands2/refs.go
+1
-1
@@ -65,7 +65,7 @@ Note: list all refs recursively with -r.
65
return getRefs(n, paths, unique, recursive)
66
},
67
Type: &RefsOutput{},
68
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
68
+ Marshalers: cmds.MarshalerMap{
69
cmds.Text: func(res cmds.Response) ([]byte, error) {
70
output := res.Output().(*RefsOutput)
71
s := ""
core/commands2/resolve.go
+1
-1
@@ -73,7 +73,7 @@ Resolve te value of another name:
73
74
return output, nil
75
},
76
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
76
+ Marshalers: cmds.MarshalerMap{
77
cmds.Text: func(res cmds.Response) ([]byte, error) {
78
output := res.Output().(string)
79
return []byte(output), nil
core/commands2/root.go
+1
-1
@@ -79,6 +79,6 @@ type MessageOutput struct {
79
Message string
80
}
81
82
-func MessageTextMarshaller(res cmds.Response) ([]byte, error) {
82
+func MessageTextMarshaler(res cmds.Response) ([]byte, error) {
83
return []byte(res.Output().(*MessageOutput).Message), nil
84
}
core/commands2/update.go
+2
-2
@@ -32,7 +32,7 @@ var updateCmd = &cmds.Command{
32
"check": updateCheckCmd,
33
"log": updateLogCmd,
34
},
35
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
35
+ Marshalers: cmds.MarshalerMap{
36
cmds.Text: func(res cmds.Response) ([]byte, error) {
37
v := res.Output().(*UpdateOutput)
38
s := ""
@@ -65,7 +65,7 @@ Nothing will be downloaded or installed.
65
return updateCheck(n)
66
},
67
Type: &UpdateOutput{},
68
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
68
+ Marshalers: cmds.MarshalerMap{
69
cmds.Text: func(res cmds.Response) ([]byte, error) {
70
v := res.Output().(*UpdateOutput)
71
s := ""
core/commands2/version.go
+1
-1
@@ -23,7 +23,7 @@ var versionCmd = &cmds.Command{
23
Version: config.CurrentVersionNumber,
24
}, nil
25
},
26
- Marshallers: map[cmds.EncodingType]cmds.Marshaller{
26
+ Marshalers: cmds.MarshalerMap{
27
cmds.Text: func(res cmds.Response) ([]byte, error) {
28
v := res.Output().(*VersionOutput)
29
s := ""