From 773efd633d23e1f96984a202e1cc17937d4014e7 Mon Sep 17 00:00:00 2001 From: Francis Lam Date: Sat, 8 Feb 2014 16:41:36 -0500 Subject: [PATCH] Added RegisterCustomCmdGenerator to btcjson package Allows for addition of custom Cmd classes that implement UnmarshalJSON directly as opposed to via RawCmd object and RawCmdParser --- jsoncmd.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/jsoncmd.go b/jsoncmd.go index 27154f0a..a1b97e96 100644 --- a/jsoncmd.go +++ b/jsoncmd.go @@ -53,6 +53,14 @@ func RegisterCustomCmd(method string, parser RawCmdParser, helpString string) { 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. // Code that reads and handles commands should switch on the type and // 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 { return c.parser(&r) } + + if g, ok := customCmdGenerators[r.Method]; ok { + cmd = g() + } } if cmd == nil {