From 629d276409b5bb1e5f4e96c14335d52e7ad32662 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sun, 29 Jan 2017 14:56:31 -0800 Subject: [PATCH] cmd/lncli: use jsonpb to son pretty-printing to disable omitempty MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit uses protobuf’s jsonpb library rather than the built-in json/encoding library to print the JSOn representation of the responses from gRPC. By using this library, we are now able to properly display any values from the response which are “non-truthy” (0, false, etc). --- cmd/lncli/commands.go | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/cmd/lncli/commands.go b/cmd/lncli/commands.go index fdb4926b..44d87a40 100644 --- a/cmd/lncli/commands.go +++ b/cmd/lncli/commands.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/hex" "encoding/json" + "errors" "fmt" "io" "io/ioutil" @@ -14,6 +15,8 @@ import ( "strings" "github.com/awalterschulze/gographviz" + "github.com/golang/protobuf/jsonpb" + "github.com/golang/protobuf/proto" "github.com/lightningnetwork/lnd/lnrpc" "github.com/roasbeef/btcd/chaincfg/chainhash" "github.com/roasbeef/btcutil" @@ -24,19 +27,32 @@ import ( // TODO(roasbeef): cli logic for supporting both positional and unix style // arguments. -func printRespJson(resp interface{}) { +func printJson(resp interface{}) { b, err := json.Marshal(resp) if err != nil { fatal(err) } - // TODO(roasbeef): disable 'omitempty' like behavior - var out bytes.Buffer json.Indent(&out, b, "", "\t") out.WriteTo(os.Stdout) } +func printRespJson(resp proto.Message) { + jsonMarshaler := &jsonpb.Marshaler{ + EmitDefaults: true, + Indent: " ", + } + + jsonStr, err := jsonMarshaler.MarshalToString(resp) + if err != nil { + fmt.Println("unable to decode response: ", err) + return + } + + fmt.Println(jsonStr) +} + var NewAddressCommand = cli.Command{ Name: "newaddress", Usage: "generates a new address. Three address types are supported: p2wkh, np2wkh, p2pkh", @@ -279,7 +295,7 @@ func openChannel(ctx *cli.Context) error { } index := channelPoint.OutputIndex - printRespJson(struct { + printJson(struct { ChannelPoint string `json:"channel_point"` }{ ChannelPoint: fmt.Sprintf("%v:%v", txid, index), @@ -369,7 +385,7 @@ func closeChannel(ctx *cli.Context) error { return err } - printRespJson(struct { + printJson(struct { ClosingTXID string `json:"closing_txid"` }{ ClosingTXID: txid.String(), @@ -695,7 +711,7 @@ func addInvoice(ctx *cli.Context) error { return err } - printRespJson(struct { + printJson(struct { RHash string `json:"r_hash"` PayReq string `json:"pay_req"` }{ @@ -1165,11 +1181,13 @@ func decodePayReq(ctx *cli.Context) error { client, cleanUp := getClient(ctx) defer cleanUp() - req := &lnrpc.PayReqString{ - PayReq: ctx.String("pay_req"), + if ctx.String("pay_req") == "" { + return errors.New("the --pay_req argument cannot be empty") } - resp, err := client.DecodePayReq(ctxb, req) + resp, err := client.DecodePayReq(ctxb, &lnrpc.PayReqString{ + PayReq: ctx.String("pay_req"), + }) if err != nil { return err }