Skip to content

Commit bc8a3d1

Browse files
committed
Merge branch 'json-prettyprint'
2 parents e8b5eff + d6cc652 commit bc8a3d1

6 files changed

Lines changed: 34 additions & 13 deletions

File tree

cmd/refmt/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func Main(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
103103
Action: func(c *cli.Context) error {
104104
return shared.TokenPump{
105105
cbor.NewDecoder(stdin),
106-
json.NewEncoder(stdout),
106+
json.NewEncoder(stdout, json.EncodeOptions{}),
107107
}.Run()
108108
},
109109
},
@@ -114,7 +114,7 @@ func Main(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
114114
Action: func(c *cli.Context) error {
115115
return shared.TokenPump{
116116
cbor.NewDecoder(hexReader(stdin)),
117-
json.NewEncoder(stdout),
117+
json.NewEncoder(stdout, json.EncodeOptions{}),
118118
}.Run()
119119
},
120120
},
@@ -125,7 +125,7 @@ func Main(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
125125
Action: func(c *cli.Context) error {
126126
return shared.TokenPump{
127127
newYamlTokenSource(stdin),
128-
json.NewEncoder(stdout),
128+
json.NewEncoder(stdout, json.EncodeOptions{}),
129129
}.Run()
130130
},
131131
},

json/jsonEncoder.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
. "github.com/polydawn/refmt/tok"
99
)
1010

11-
func NewEncoder(wr io.Writer) *Encoder {
11+
func NewEncoder(wr io.Writer, cfg EncodeOptions) *Encoder {
1212
return &Encoder{
1313
wr: wr,
14+
cfg: cfg,
1415
stack: make([]phase, 0, 10),
1516
}
1617
}
@@ -25,7 +26,8 @@ func (d *Encoder) Reset() {
2526
A json.Encoder is a TokenSink implementation that emits json bytes.
2627
*/
2728
type Encoder struct {
28-
wr io.Writer
29+
wr io.Writer
30+
cfg EncodeOptions
2931

3032
// Stack, tracking how many array and map opens are outstanding.
3133
// (Values are only 'phase_mapExpectKeyOrEnd' and 'phase_arrExpectValueOrEnd'.)
@@ -74,6 +76,10 @@ func (d *Encoder) Step(tok *Token) (done bool, err error) {
7476
case TArrOpen:
7577
return true, fmt.Errorf("unexpected arrOpen; expected start of key or end of map")
7678
case TMapClose:
79+
d.wr.Write(d.cfg.Line)
80+
for i := 1; i < len(d.stack); i++ {
81+
d.wr.Write(d.cfg.Indent)
82+
}
7783
d.wr.Write(wordMapClose)
7884
return d.popPhase()
7985
case TArrClose:
@@ -126,6 +132,10 @@ func (d *Encoder) Step(tok *Token) (done bool, err error) {
126132
case TMapClose:
127133
return true, fmt.Errorf("unexpected mapClose; expected start of value or end of array")
128134
case TArrClose:
135+
d.wr.Write(d.cfg.Line)
136+
for i := 1; i < len(d.stack); i++ {
137+
d.wr.Write(d.cfg.Indent)
138+
}
129139
d.wr.Write(wordArrClose)
130140
return d.popPhase()
131141
default:
@@ -149,6 +159,7 @@ func (d *Encoder) pushPhase(p phase) {
149159
func (d *Encoder) popPhase() (bool, error) {
150160
n := len(d.stack) - 1
151161
if n == 0 {
162+
d.wr.Write(d.cfg.Line)
152163
return true, nil
153164
}
154165
if n < 0 { // the state machines are supposed to have already errored better
@@ -167,6 +178,10 @@ func (d *Encoder) entrySep() {
167178
d.wr.Write(wordComma)
168179
}
169180
d.some = true
181+
d.wr.Write(d.cfg.Line)
182+
for i := 0; i < len(d.stack); i++ {
183+
d.wr.Write(d.cfg.Indent)
184+
}
170185
}
171186

172187
func (d *Encoder) flushValue(tok *Token) {

json/jsonEncoder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestJsonEncoder(t *testing.T) {
2323
title = strings.Join([]string{tr.sequence.Title, tr.title}, ", ")
2424
}
2525
buf := &bytes.Buffer{}
26-
tokenSink := NewEncoder(buf)
26+
tokenSink := NewEncoder(buf, EncodeOptions{})
2727

2828
// Run steps.
2929
var done bool

json/jsonHelpers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func Marshal(v interface{}) ([]byte, error) {
3636

3737
func MarshalAtlased(v interface{}, atl atlas.Atlas) ([]byte, error) {
3838
var buf bytes.Buffer
39-
if err := NewMarshallerAtlased(&buf, atl).Marshal(v); err != nil {
39+
if err := NewMarshallerAtlased(&buf, EncodeOptions{}, atl).Marshal(v); err != nil {
4040
return nil, err
4141
}
4242
return buf.Bytes(), nil
@@ -55,13 +55,13 @@ func (x *Marshaller) Marshal(v interface{}) error {
5555
}
5656

5757
func NewMarshaller(wr io.Writer) *Marshaller {
58-
return NewMarshallerAtlased(wr, atlas.MustBuild())
58+
return NewMarshallerAtlased(wr, EncodeOptions{}, atlas.MustBuild())
5959
}
6060

61-
func NewMarshallerAtlased(wr io.Writer, atl atlas.Atlas) *Marshaller {
61+
func NewMarshallerAtlased(wr io.Writer, cfg EncodeOptions, atl atlas.Atlas) *Marshaller {
6262
x := &Marshaller{
6363
marshaller: obj.NewMarshaller(atl),
64-
encoder: NewEncoder(wr),
64+
encoder: NewEncoder(wr, cfg),
6565
}
6666
x.pump = shared.TokenPump{
6767
x.marshaller,

json/jsonOptions.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package json
22

33
type EncodeOptions struct {
4-
// future: indentation opts and such
4+
// If set, this will be prefixed to every "line" to pretty-print.
5+
// (Typically you want to just set this to `[]byte{'\n'}`!)
6+
Line []byte
7+
8+
// If set, this will be prefixed $N$ times before each line's content to pretty-print.
9+
// (Likely values are a tab, or a few spaces.)
10+
Indent []byte
511
}
612

713
// marker method -- you may use this type to instruct `refmt.Marshal`

marshalHelpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ func NewMarshaller(opts EncodeOptions, wr io.Writer) Marshaller {
5050
}
5151

5252
func NewMarshallerAtlased(opts EncodeOptions, wr io.Writer, atl atlas.Atlas) Marshaller {
53-
switch opts.(type) {
53+
switch o2 := opts.(type) {
5454
case json.EncodeOptions:
55-
return json.NewMarshallerAtlased(wr, atl)
55+
return json.NewMarshallerAtlased(wr, o2, atl)
5656
case cbor.EncodeOptions:
5757
return cbor.NewMarshallerAtlased(wr, atl)
5858
default:

0 commit comments

Comments
 (0)