Skip to content

Commit aefb943

Browse files
committed
implement TextMarshaler/JSONMarshaler more consistently
Signed-off-by: Jason Hall <[email protected]>
1 parent 098045d commit aefb943

File tree

5 files changed

+113
-12
lines changed

5 files changed

+113
-12
lines changed

pkg/name/digest.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,21 @@ func (d *Digest) UnmarshalJSON(data []byte) error {
7979
return nil
8080
}
8181

82+
// MarshalText formats the digest into a string for text serialization.
83+
func (d Digest) MarshalText() ([]byte, error) {
84+
return []byte(d.String()), nil
85+
}
86+
87+
// UnmarshalText parses a text string into a Digest.
88+
func (d *Digest) UnmarshalText(data []byte) error {
89+
n, err := NewDigest(string(data))
90+
if err != nil {
91+
return err
92+
}
93+
*d = n
94+
return nil
95+
}
96+
8297
// NewDigest returns a new Digest representing the given name.
8398
func NewDigest(name string, opts ...Option) (Digest, error) {
8499
// Split on "@"

pkg/name/registry.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package name
1616

1717
import (
18+
"encoding/json"
1819
"net"
1920
"net/url"
2021
"path"
@@ -140,3 +141,33 @@ func NewInsecureRegistry(name string, opts ...Option) (Registry, error) {
140141
opts = append(opts, Insecure)
141142
return NewRegistry(name, opts...)
142143
}
144+
145+
// MarshalJSON formats the digest into a string for JSON serialization.
146+
func (r Registry) MarshalJSON() ([]byte, error) { return json.Marshal(r.String()) }
147+
148+
// UnmarshalJSON parses a JSON string into a Repository.
149+
func (r *Registry) UnmarshalJSON(data []byte) error {
150+
var s string
151+
if err := json.Unmarshal(data, &s); err != nil {
152+
return err
153+
}
154+
n, err := NewRegistry(s)
155+
if err != nil {
156+
return err
157+
}
158+
*r = n
159+
return nil
160+
}
161+
162+
// MarshalText formats the digest into a string for text serialization.
163+
func (r Registry) MarshalText() ([]byte, error) { return []byte(r.String()), nil }
164+
165+
// UnmarshalText parses a text string into a Repository.
166+
func (r *Registry) UnmarshalText(data []byte) error {
167+
n, err := NewRegistry(string(data))
168+
if err != nil {
169+
return err
170+
}
171+
*r = n
172+
return nil
173+
}

pkg/name/repository.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package name
1616

1717
import (
18+
"encoding/json"
1819
"fmt"
1920
"strings"
2021
)
@@ -119,3 +120,33 @@ func (r Repository) Digest(identifier string) Digest {
119120
d.original = d.Name()
120121
return d
121122
}
123+
124+
// MarshalJSON formats the digest into a string for JSON serialization.
125+
func (r Repository) MarshalJSON() ([]byte, error) { return json.Marshal(r.String()) }
126+
127+
// UnmarshalJSON parses a JSON string into a Repository.
128+
func (r *Repository) UnmarshalJSON(data []byte) error {
129+
var s string
130+
if err := json.Unmarshal(data, &s); err != nil {
131+
return err
132+
}
133+
n, err := NewRepository(s)
134+
if err != nil {
135+
return err
136+
}
137+
*r = n
138+
return nil
139+
}
140+
141+
// MarshalText formats the digest into a string for text serialization.
142+
func (r Repository) MarshalText() ([]byte, error) { return []byte(r.String()), nil }
143+
144+
// UnmarshalText parses a text string into a Repository.
145+
func (r *Repository) UnmarshalText(data []byte) error {
146+
n, err := NewRepository(string(data))
147+
if err != nil {
148+
return err
149+
}
150+
*r = n
151+
return nil
152+
}

pkg/name/tag.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package name
1616

1717
import (
18+
"encoding/json"
1819
"strings"
1920
)
2021

@@ -106,3 +107,33 @@ func NewTag(name string, opts ...Option) (Tag, error) {
106107
original: name,
107108
}, nil
108109
}
110+
111+
// MarshalJSON formats the digest into a string for JSON serialization.
112+
func (t Tag) MarshalJSON() ([]byte, error) { return json.Marshal(t.String()) }
113+
114+
// UnmarshalJSON parses a JSON string into a Tag.
115+
func (t *Tag) UnmarshalJSON(data []byte) error {
116+
var s string
117+
if err := json.Unmarshal(data, &s); err != nil {
118+
return err
119+
}
120+
n, err := NewTag(s)
121+
if err != nil {
122+
return err
123+
}
124+
*t = n
125+
return nil
126+
}
127+
128+
// MarshalText formats the digest into a string for text serialization.
129+
func (t Tag) MarshalText() ([]byte, error) { return []byte(t.String()), nil }
130+
131+
// UnmarshalText parses a text string into a Tag.
132+
func (t *Tag) UnmarshalText(data []byte) error {
133+
n, err := NewTag(string(data))
134+
if err != nil {
135+
return err
136+
}
137+
*t = n
138+
return nil
139+
}

pkg/v1/hash.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"hash"
2323
"io"
24-
"strconv"
2524
"strings"
2625
)
2726

@@ -49,30 +48,24 @@ func NewHash(s string) (Hash, error) {
4948
}
5049

5150
// MarshalJSON implements json.Marshaler
52-
func (h Hash) MarshalJSON() ([]byte, error) {
53-
return json.Marshal(h.String())
54-
}
51+
func (h Hash) MarshalJSON() ([]byte, error) { return json.Marshal(h.String()) }
5552

5653
// UnmarshalJSON implements json.Unmarshaler
5754
func (h *Hash) UnmarshalJSON(data []byte) error {
58-
s, err := strconv.Unquote(string(data))
59-
if err != nil {
55+
var s string
56+
if err := json.Unmarshal(data, &s); err != nil {
6057
return err
6158
}
6259
return h.parse(s)
6360
}
6461

6562
// MarshalText implements encoding.TextMarshaler. This is required to use
6663
// v1.Hash as a key in a map when marshalling JSON.
67-
func (h Hash) MarshalText() (text []byte, err error) {
68-
return []byte(h.String()), nil
69-
}
64+
func (h Hash) MarshalText() ([]byte, error) { return []byte(h.String()), nil }
7065

7166
// UnmarshalText implements encoding.TextUnmarshaler. This is required to use
7267
// v1.Hash as a key in a map when unmarshalling JSON.
73-
func (h *Hash) UnmarshalText(text []byte) error {
74-
return h.parse(string(text))
75-
}
68+
func (h *Hash) UnmarshalText(text []byte) error { return h.parse(string(text)) }
7669

7770
// Hasher returns a hash.Hash for the named algorithm (e.g. "sha256")
7871
func Hasher(name string) (hash.Hash, error) {

0 commit comments

Comments
 (0)