Skip to content

Commit 765f894

Browse files
committed
fix: Disambiguate Parse
Specifically, either return (<something>, nil), or (<nothing>, err), but not (<something>, err) which is difficult to reason about. Add two abstraction helper methods: `IsIdenticalTo`, and `IsEmpty` Signed-off-by: Nima Talebi <[email protected]>
1 parent bde1400 commit 765f894

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

digest.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ var (
8080
// be returned if the format is invalid.
8181
func Parse(s string) (Digest, error) {
8282
d := Digest(s)
83-
return d, d.Validate()
83+
if err := d.Validate(); err != nil {
84+
return "", err
85+
}
86+
return d, nil
8487
}
8588

8689
// FromReader consumes the content of rd until io.EOF, returning canonical digest.
@@ -137,11 +140,21 @@ func (d Digest) Encoded() string {
137140
return string(d[d.sepIndex()+1:])
138141
}
139142

140-
// Hex is deprecated. Please use Digest.Encoded.
143+
// IsIdenticalTo compares this digest to any other, inclusive of the hashing algorithm used
144+
func (d Digest) IsIdenticalTo(o Digest) bool {
145+
return string(d) == string(o)
146+
}
147+
148+
// Deprecated: Hex has been deprecated in favor of Digest.Encoded
141149
func (d Digest) Hex() string {
142150
return d.Encoded()
143151
}
144152

153+
// IsEmpty does what it says (an empty digest is an empty string)
154+
func (d Digest) IsEmpty() bool {
155+
return len(d.String()) == 0
156+
}
157+
145158
func (d Digest) String() string {
146159
return string(d)
147160
}

0 commit comments

Comments
 (0)