Skip to content

Commit 5ca533c

Browse files
committed
Add JSON marshalling funcs for Digest.
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 5ca533c

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-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

+55
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,56 @@ 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+
b, err := json.Marshal(digest)
166+
if err != nil {
167+
t.Fatalf("Marshal() failed: %v", err)
168+
}
169+
170+
if want := `"` + digestNameStr + `"`; want != string(b) {
171+
t.Errorf("Marshal() was incorrect. Wanted: `%s` Got: `%s`", want, string(b))
172+
}
173+
174+
var out Digest
175+
if err := json.Unmarshal(b, &out); err != nil {
176+
t.Fatalf("Unmarshal() failed: %v", err)
177+
}
178+
179+
if out.String() != digest.String() {
180+
t.Errorf("Unmarshaled Digest should be the same as the original. Wanted: `%s` Got: `%s`", digest, out)
181+
}
182+
})
183+
184+
t.Run("map", func(t *testing.T) {
185+
in := map[string]Digest{
186+
"a": digest,
187+
}
188+
b, err := json.Marshal(in)
189+
if err != nil {
190+
t.Fatalf("MarshalJSON() failed: %v", err)
191+
}
192+
193+
want := `{"a":"` + digestNameStr + `"}`
194+
if want != string(b) {
195+
t.Errorf("Marshal() was incorrect. Wanted: `%s` Got: `%s`", want, string(b))
196+
}
197+
198+
var out map[string]Digest
199+
if err := json.Unmarshal(b, &out); err != nil {
200+
t.Fatalf("Unmarshal() failed: %v", err)
201+
}
202+
203+
if !reflect.DeepEqual(in, out) {
204+
t.Errorf("Unmarshaled map should be the same as the original. Wanted: `%v` Got: `%v`", in, out)
205+
}
206+
})
207+
}

0 commit comments

Comments
 (0)