Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 20bf102

Browse files
committed
dep: internalize and improve JSON handling
manifest and lock's MarshalJSON methods were previously returning HTML-escaped, unindented byte streams. Now they do that work internally, and more gracefully via a json.Encoder.
1 parent 9b21ed5 commit 20bf102

File tree

4 files changed

+52
-59
lines changed

4 files changed

+52
-59
lines changed

lock.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package main
66

77
import (
8+
"bytes"
89
"encoding/hex"
910
"encoding/json"
1011
"fmt"
@@ -120,5 +121,12 @@ func (l *lock) MarshalJSON() ([]byte, error) {
120121
}
121122

122123
// TODO sort output - #15
123-
return json.Marshal(raw)
124+
125+
var buf bytes.Buffer
126+
enc := json.NewEncoder(&buf)
127+
enc.SetIndent("", " ")
128+
enc.SetEscapeHTML(false)
129+
err := enc.Encode(raw)
130+
131+
return buf.Bytes(), err
124132
}

lock_test.go

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
package main
66

77
import (
8-
"bytes"
98
"encoding/hex"
10-
"encoding/json"
119
"reflect"
1210
"strings"
1311
"testing"
@@ -16,33 +14,35 @@ import (
1614
)
1715

1816
const le = `{
19-
"memo": "2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e",
20-
"projects": [
21-
{
22-
"name": "github.com/sdboyer/gps",
23-
"branch": "master",
24-
"version": "v0.12.0",
25-
"revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb",
26-
"packages": [
27-
"."
28-
]
29-
}
30-
]
31-
}`
17+
"memo": "2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e",
18+
"projects": [
19+
{
20+
"name": "github.com/sdboyer/gps",
21+
"branch": "master",
22+
"version": "v0.12.0",
23+
"revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb",
24+
"packages": [
25+
"."
26+
]
27+
}
28+
]
29+
}
30+
`
3231

3332
const lg = `{
34-
"memo": "2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e",
35-
"projects": [
36-
{
37-
"name": "github.com/sdboyer/gps",
38-
"branch": "master",
39-
"revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb",
40-
"packages": [
41-
"."
42-
]
43-
}
44-
]
45-
}`
33+
"memo": "2252a285ab27944a4d7adcba8dbd03980f59ba652f12db39fa93b927c345593e",
34+
"projects": [
35+
{
36+
"name": "github.com/sdboyer/gps",
37+
"branch": "master",
38+
"revision": "d05d5aca9f895d19e9265839bffeadd74a2d2ecb",
39+
"packages": [
40+
"."
41+
]
42+
}
43+
]
44+
}
45+
`
4646

4747
func TestReadLock(t *testing.T) {
4848
_, err := readLock(strings.NewReader(le))
@@ -87,16 +87,12 @@ func TestWriteLock(t *testing.T) {
8787
},
8888
}
8989

90-
b, err := json.Marshal(l)
90+
b, err := l.MarshalJSON()
9191
if err != nil {
9292
t.Fatalf("Error while marshaling valid lock to JSON: %q", err)
9393
}
9494

95-
var out bytes.Buffer
96-
json.Indent(&out, b, "", "\t")
97-
98-
s := out.String()
99-
if s != lg {
100-
t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", s, lg)
95+
if string(b) != lg {
96+
t.Errorf("Valid lock did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", string(b), lg)
10197
}
10298
}

manifest.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,13 @@ func (m *manifest) MarshalJSON() ([]byte, error) {
118118
raw.Overrides[string(n)] = toPossible(pp)
119119
}
120120

121-
b, err := json.Marshal(raw)
122-
if err != nil {
123-
return nil, err
124-
}
121+
var buf bytes.Buffer
122+
enc := json.NewEncoder(&buf)
123+
enc.SetIndent("", " ")
124+
enc.SetEscapeHTML(false)
125+
err := enc.Encode(raw)
125126

126-
// Semver range ops, > and <, get turned into unicode code points. This is a
127-
// nice example of why using JSON for files like this is not the best
128-
b = bytes.Replace(b, []byte("\\u003c"), []byte("<"), -1)
129-
b = bytes.Replace(b, []byte("\\u003e"), []byte(">"), -1)
130-
return b, nil
127+
return buf.Bytes(), err
131128
}
132129

133130
func toPossible(pp gps.ProjectProperties) (p possibleProps) {

manifest_test.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
package main
66

77
import (
8-
"bytes"
9-
"encoding/json"
108
"reflect"
119
"strings"
1210
"testing"
@@ -34,7 +32,8 @@ const je = `{
3432
"ignores": [
3533
"github.com/foo/bar"
3634
]
37-
}`
35+
}
36+
`
3837

3938
const jg = `{
4039
"dependencies": {
@@ -54,7 +53,8 @@ const jg = `{
5453
"ignores": [
5554
"github.com/foo/bar"
5655
]
57-
}`
56+
}
57+
`
5858

5959
func TestReadManifest(t *testing.T) {
6060
_, err := readManifest(strings.NewReader(je))
@@ -119,20 +119,12 @@ func TestWriteManifest(t *testing.T) {
119119
Ignores: []string{"github.com/foo/bar"},
120120
}
121121

122-
b, err := json.Marshal(m)
122+
b, err := m.MarshalJSON()
123123
if err != nil {
124124
t.Fatalf("Error while marshaling valid manifest to JSON: %q", err)
125125
}
126126

127-
var out bytes.Buffer
128-
json.Indent(&out, b, "", " ")
129-
b = out.Bytes()
130-
// uuuuuughhhhh
131-
b = bytes.Replace(b, []byte("\\u003c"), []byte("<"), -1)
132-
b = bytes.Replace(b, []byte("\\u003e"), []byte(">"), -1)
133-
134-
s := string(b)
135-
if s != jg {
136-
t.Errorf("Valid manifest did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", s, jg)
127+
if string(b) != jg {
128+
t.Errorf("Valid manifest did not marshal to JSON as expected:\n\t(GOT): %s\n\t(WNT): %s", string(b), jg)
137129
}
138130
}

0 commit comments

Comments
 (0)