Added RegisterCustomCmdGenerator to btcjson package

Allows for addition of custom Cmd classes that implement UnmarshalJSON
directly as opposed to via RawCmd object and RawCmdParser
This commit is contained in:
Francis Lam 2014-02-08 16:41:36 -05:00
parent 7623d13c37
commit 773efd633d

View file

@ -53,6 +53,14 @@ func RegisterCustomCmd(method string, parser RawCmdParser, helpString string) {
customCmds[method] = cmd{parser: parser, helpString: helpString} customCmds[method] = cmd{parser: parser, helpString: helpString}
} }
type CmdGenerator func() Cmd
var customCmdGenerators = make(map[string]CmdGenerator)
func RegisterCustomCmdGenerator(method string, generator CmdGenerator) {
customCmdGenerators[method] = generator
}
// ParseMarshaledCmd parses a raw command and unmarshals as a Cmd. // ParseMarshaledCmd parses a raw command and unmarshals as a Cmd.
// Code that reads and handles commands should switch on the type and // Code that reads and handles commands should switch on the type and
// type assert as the particular commands supported by the program. // type assert as the particular commands supported by the program.
@ -294,6 +302,10 @@ func ParseMarshaledCmd(b []byte) (Cmd, error) {
if c, ok := customCmds[r.Method]; ok { if c, ok := customCmds[r.Method]; ok {
return c.parser(&r) return c.parser(&r)
} }
if g, ok := customCmdGenerators[r.Method]; ok {
cmd = g()
}
} }
if cmd == nil { if cmd == nil {