Skip to content

Commit

Permalink
Add JSON marshalling funcs for Digest. (#1915)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wlynch authored Apr 9, 2024
1 parent 8b3c303 commit 0309184
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/name/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package name
import (
// nolint: depguard
_ "crypto/sha256" // Recommended by go-digest.
"encoding/json"
"strings"

"github.com/opencontainers/go-digest"
Expand Down Expand Up @@ -59,6 +60,25 @@ func (d Digest) String() string {
return d.original
}

// MarshalJSON formats the digest into a string for JSON serialization.
func (d Digest) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}

// UnmarshalJSON parses a JSON string into a Digest.
func (d *Digest) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
n, err := NewDigest(s)
if err != nil {
return err
}
*d = n
return nil
}

// NewDigest returns a new Digest representing the given name.
func NewDigest(name string, opts ...Option) (Digest, error) {
// Split on "@"
Expand Down
57 changes: 57 additions & 0 deletions pkg/name/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package name

import (
"encoding/json"
"path"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -150,3 +152,58 @@ func TestDigestScopes(t *testing.T) {
t.Errorf("scope was incorrect for %v. Wanted: `%s` Got: `%s`", digest, expectedScope, actualScope)
}
}

func TestJSON(t *testing.T) {
t.Parallel()
digestNameStr := "gcr.io/project-id/image@" + validDigest
digest, err := NewDigest(digestNameStr, StrictValidation)
if err != nil {
t.Fatalf("`%s` should be a valid Digest name, got error: %v", digestNameStr, err)
}

t.Run("string", func(t *testing.T) {
t.Parallel()
b, err := json.Marshal(digest)
if err != nil {
t.Fatalf("Marshal() failed: %v", err)
}

if want := `"` + digestNameStr + `"`; want != string(b) {
t.Errorf("Marshal() was incorrect. Wanted: `%s` Got: `%s`", want, string(b))
}

var out Digest
if err := json.Unmarshal(b, &out); err != nil {
t.Fatalf("Unmarshal() failed: %v", err)
}

if out.String() != digest.String() {
t.Errorf("Unmarshaled Digest should be the same as the original. Wanted: `%s` Got: `%s`", digest, out)
}
})

t.Run("map", func(t *testing.T) {
t.Parallel()
in := map[string]Digest{
"a": digest,
}
b, err := json.Marshal(in)
if err != nil {
t.Fatalf("MarshalJSON() failed: %v", err)
}

want := `{"a":"` + digestNameStr + `"}`
if want != string(b) {
t.Errorf("Marshal() was incorrect. Wanted: `%s` Got: `%s`", want, string(b))
}

var out map[string]Digest
if err := json.Unmarshal(b, &out); err != nil {
t.Fatalf("Unmarshal() failed: %v", err)
}

if !reflect.DeepEqual(in, out) {
t.Errorf("Unmarshaled map should be the same as the original. Wanted: `%v` Got: `%v`", in, out)
}
})
}

0 comments on commit 0309184

Please sign in to comment.