mirror of https://github.com/BTCPrivate/lnd.git
cmd/lncli: use jsonpb to son pretty-printing to disable omitempty
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).
This commit is contained in:
parent
f4f92bb8e7
commit
629d276409
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -14,6 +15,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/awalterschulze/gographviz"
|
"github.com/awalterschulze/gographviz"
|
||||||
|
"github.com/golang/protobuf/jsonpb"
|
||||||
|
"github.com/golang/protobuf/proto"
|
||||||
"github.com/lightningnetwork/lnd/lnrpc"
|
"github.com/lightningnetwork/lnd/lnrpc"
|
||||||
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
||||||
"github.com/roasbeef/btcutil"
|
"github.com/roasbeef/btcutil"
|
||||||
|
@ -24,19 +27,32 @@ import (
|
||||||
// TODO(roasbeef): cli logic for supporting both positional and unix style
|
// TODO(roasbeef): cli logic for supporting both positional and unix style
|
||||||
// arguments.
|
// arguments.
|
||||||
|
|
||||||
func printRespJson(resp interface{}) {
|
func printJson(resp interface{}) {
|
||||||
b, err := json.Marshal(resp)
|
b, err := json.Marshal(resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fatal(err)
|
fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(roasbeef): disable 'omitempty' like behavior
|
|
||||||
|
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
json.Indent(&out, b, "", "\t")
|
json.Indent(&out, b, "", "\t")
|
||||||
out.WriteTo(os.Stdout)
|
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{
|
var NewAddressCommand = cli.Command{
|
||||||
Name: "newaddress",
|
Name: "newaddress",
|
||||||
Usage: "generates a new address. Three address types are supported: p2wkh, np2wkh, p2pkh",
|
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
|
index := channelPoint.OutputIndex
|
||||||
printRespJson(struct {
|
printJson(struct {
|
||||||
ChannelPoint string `json:"channel_point"`
|
ChannelPoint string `json:"channel_point"`
|
||||||
}{
|
}{
|
||||||
ChannelPoint: fmt.Sprintf("%v:%v", txid, index),
|
ChannelPoint: fmt.Sprintf("%v:%v", txid, index),
|
||||||
|
@ -369,7 +385,7 @@ func closeChannel(ctx *cli.Context) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
printRespJson(struct {
|
printJson(struct {
|
||||||
ClosingTXID string `json:"closing_txid"`
|
ClosingTXID string `json:"closing_txid"`
|
||||||
}{
|
}{
|
||||||
ClosingTXID: txid.String(),
|
ClosingTXID: txid.String(),
|
||||||
|
@ -695,7 +711,7 @@ func addInvoice(ctx *cli.Context) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
printRespJson(struct {
|
printJson(struct {
|
||||||
RHash string `json:"r_hash"`
|
RHash string `json:"r_hash"`
|
||||||
PayReq string `json:"pay_req"`
|
PayReq string `json:"pay_req"`
|
||||||
}{
|
}{
|
||||||
|
@ -1165,11 +1181,13 @@ func decodePayReq(ctx *cli.Context) error {
|
||||||
client, cleanUp := getClient(ctx)
|
client, cleanUp := getClient(ctx)
|
||||||
defer cleanUp()
|
defer cleanUp()
|
||||||
|
|
||||||
req := &lnrpc.PayReqString{
|
if ctx.String("pay_req") == "" {
|
||||||
PayReq: 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue