Skip to content

Commit d6cc652

Browse files
committed
Configure prettyprint in json.EncodeOptions.
Add this config paramater to all of the relevant methods. Signed-off-by: Eric Myhre <hash@exultant.us>
1 parent 0ffbc52 commit d6cc652

6 files changed

Lines changed: 28 additions & 22 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: 11 additions & 11 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,9 +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-
indent []byte
30-
line []byte
29+
wr io.Writer
30+
cfg EncodeOptions
3131

3232
// Stack, tracking how many array and map opens are outstanding.
3333
// (Values are only 'phase_mapExpectKeyOrEnd' and 'phase_arrExpectValueOrEnd'.)
@@ -76,9 +76,9 @@ func (d *Encoder) Step(tok *Token) (done bool, err error) {
7676
case TArrOpen:
7777
return true, fmt.Errorf("unexpected arrOpen; expected start of key or end of map")
7878
case TMapClose:
79-
d.wr.Write(d.line)
79+
d.wr.Write(d.cfg.Line)
8080
for i := 1; i < len(d.stack); i++ {
81-
d.wr.Write(d.indent)
81+
d.wr.Write(d.cfg.Indent)
8282
}
8383
d.wr.Write(wordMapClose)
8484
return d.popPhase()
@@ -132,9 +132,9 @@ func (d *Encoder) Step(tok *Token) (done bool, err error) {
132132
case TMapClose:
133133
return true, fmt.Errorf("unexpected mapClose; expected start of value or end of array")
134134
case TArrClose:
135-
d.wr.Write(d.line)
135+
d.wr.Write(d.cfg.Line)
136136
for i := 1; i < len(d.stack); i++ {
137-
d.wr.Write(d.indent)
137+
d.wr.Write(d.cfg.Indent)
138138
}
139139
d.wr.Write(wordArrClose)
140140
return d.popPhase()
@@ -159,7 +159,7 @@ func (d *Encoder) pushPhase(p phase) {
159159
func (d *Encoder) popPhase() (bool, error) {
160160
n := len(d.stack) - 1
161161
if n == 0 {
162-
d.wr.Write(d.line)
162+
d.wr.Write(d.cfg.Line)
163163
return true, nil
164164
}
165165
if n < 0 { // the state machines are supposed to have already errored better
@@ -178,9 +178,9 @@ func (d *Encoder) entrySep() {
178178
d.wr.Write(wordComma)
179179
}
180180
d.some = true
181-
d.wr.Write(d.line)
181+
d.wr.Write(d.cfg.Line)
182182
for i := 0; i < len(d.stack); i++ {
183-
d.wr.Write(d.indent)
183+
d.wr.Write(d.cfg.Indent)
184184
}
185185
}
186186

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)