Skip to content

Commit 0309184

Browse files
authored
Add JSON marshalling funcs for Digest. (#1915)
Currently when you marshal a digest in a struct, you get `{}` because all of the fields are unmarshalled. This changes the behavior to marshal digests as their string format.
1 parent 8b3c303 commit 0309184

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

pkg/name/digest.go

+20
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package name
1717
import (
1818
// nolint: depguard
1919
_ "crypto/sha256" // Recommended by go-digest.
20+
"encoding/json"
2021
"strings"
2122

2223
"github.com/opencontainers/go-digest"
@@ -59,6 +60,25 @@ func (d Digest) String() string {
5960
return d.original
6061
}
6162

63+
// MarshalJSON formats the digest into a string for JSON serialization.
64+
func (d Digest) MarshalJSON() ([]byte, error) {
65+
return json.Marshal(d.String())
66+
}
67+
68+
// UnmarshalJSON parses a JSON string into a Digest.
69+
func (d *Digest) UnmarshalJSON(data []byte) error {
70+
var s string
71+
if err := json.Unmarshal(data, &s); err != nil {
72+
return err
73+
}
74+
n, err := NewDigest(s)
75+
if err != nil {
76+
return err
77+
}
78+
*d = n
79+
return nil
80+
}
81+
6282
// NewDigest returns a new Digest representing the given name.
6383
func NewDigest(name string, opts ...Option) (Digest, error) {
6484
// Split on "@"

pkg/name/digest_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
package name
1616

1717
import (
18+
"encoding/json"
1819
"path"
20+
"reflect"
1921
"strings"
2022
"testing"
2123
)
@@ -150,3 +152,58 @@ func TestDigestScopes(t *testing.T) {
150152
t.Errorf("scope was incorrect for %v. Wanted: `%s` Got: `%s`", digest, expectedScope, actualScope)
151153
}
152154
}
155+
156+
func TestJSON(t *testing.T) {
157+
t.Parallel()
158+
digestNameStr := "gcr.io/project-id/image@" + validDigest
159+
digest, err := NewDigest(digestNameStr, StrictValidation)
160+
if err != nil {
161+
t.Fatalf("`%s` should be a valid Digest name, got error: %v", digestNameStr, err)
162+
}
163+
164+
t.Run("string", func(t *testing.T) {
165+
t.Parallel()
166+
b, err := json.Marshal(digest)
167+
if err != nil {
168+
t.Fatalf("Marshal() failed: %v", err)
169+
}
170+
171+
if want := `"` + digestNameStr + `"`; want != string(b) {
172+
t.Errorf("Marshal() was incorrect. Wanted: `%s` Got: `%s`", want, string(b))
173+
}
174+
175+
var out Digest
176+
if err := json.Unmarshal(b, &out); err != nil {
177+
t.Fatalf("Unmarshal() failed: %v", err)
178+
}
179+
180+
if out.String() != digest.String() {
181+
t.Errorf("Unmarshaled Digest should be the same as the original. Wanted: `%s` Got: `%s`", digest, out)
182+
}
183+
})
184+
185+
t.Run("map", func(t *testing.T) {
186+
t.Parallel()
187+
in := map[string]Digest{
188+
"a": digest,
189+
}
190+
b, err := json.Marshal(in)
191+
if err != nil {
192+
t.Fatalf("MarshalJSON() failed: %v", err)
193+
}
194+
195+
want := `{"a":"` + digestNameStr + `"}`
196+
if want != string(b) {
197+
t.Errorf("Marshal() was incorrect. Wanted: `%s` Got: `%s`", want, string(b))
198+
}
199+
200+
var out map[string]Digest
201+
if err := json.Unmarshal(b, &out); err != nil {
202+
t.Fatalf("Unmarshal() failed: %v", err)
203+
}
204+
205+
if !reflect.DeepEqual(in, out) {
206+
t.Errorf("Unmarshaled map should be the same as the original. Wanted: `%v` Got: `%v`", in, out)
207+
}
208+
})
209+
}

0 commit comments

Comments
 (0)