Skip to content

Commit 0ddb086

Browse files
authored
Merge pull request #1821 from lightninglabs/wip/cmd-json-emits-unset-fields
cli: include unset and zero-valued proto fields in JSON output
2 parents cd914b3 + 25c66fd commit 0ddb086

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

cmd/commands/commands.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package commands
22

33
import (
4-
"bytes"
54
"context"
65
"encoding/json"
76
"fmt"
@@ -12,6 +11,7 @@ import (
1211
lfn "github.com/lightningnetwork/lnd/fn/v2"
1312
"github.com/lightningnetwork/lnd/signal"
1413
"github.com/urfave/cli"
14+
"google.golang.org/protobuf/encoding/protojson"
1515
"google.golang.org/protobuf/proto"
1616
)
1717

@@ -193,16 +193,36 @@ func getContext() context.Context {
193193
return ctxc
194194
}
195195

196+
// printJSON prints any JSON serializable object to stdout in a pretty format.
196197
func printJSON(resp interface{}) {
197-
b, err := json.Marshal(resp)
198+
var (
199+
b []byte
200+
err error
201+
)
202+
203+
switch m := resp.(type) {
204+
case proto.Message:
205+
mo := protojson.MarshalOptions{
206+
Multiline: true,
207+
Indent: "\t",
208+
EmitUnpopulated: true,
209+
UseProtoNames: true,
210+
}
211+
b, err = mo.Marshal(m)
212+
213+
default:
214+
// This does not emit unpopulated fields.
215+
b, err = json.MarshalIndent(resp, "", "\t")
216+
}
217+
198218
if err != nil {
199219
Fatal(err)
200220
}
201221

202-
var out bytes.Buffer
203-
_ = json.Indent(&out, b, "", "\t")
204-
out.WriteString("\n")
205-
_, _ = out.WriteTo(os.Stdout)
222+
_, err = os.Stdout.Write(append(b, '\n'))
223+
if err != nil {
224+
Fatal(err)
225+
}
206226
}
207227

208228
func printRespJSON(resp proto.Message) {

docs/release-notes/release-notes-0.7.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@
182182
allows users to specify the amount to send to a V2 address that allows custom
183183
amounts (which is the case when a V2 address is created with an amount of 0).
184184

185+
- CLI JSON output [now](https://github.com/lightninglabs/taproot-assets/pull/1821)
186+
includes unset and zero-valued proto fields (e.g. transaction output indexes).
187+
This ensures consistent output shape across all proto messages.
188+
185189
# Improvements
186190

187191
## Functional Updates

0 commit comments

Comments
 (0)