|
1 | 1 | // Package reference provides a general type to represent any way of referencing images within the registry.
|
2 | 2 | // Its main purpose is to abstract tags and digests (content-addressable hash).
|
3 | 3 | //
|
4 |
| -// Grammar |
| 4 | +// # Grammar |
5 | 5 | //
|
6 | 6 | // reference := name [ ":" tag ] [ "@" digest ]
|
7 | 7 | // name := [domain '/'] remote-name
|
|
24 | 24 | // digest-hex := /[0-9a-fA-F]{32,}/ ; At least 128 bit digest value
|
25 | 25 | //
|
26 | 26 | // identifier := /[a-f0-9]{64}/
|
| 27 | +// |
| 28 | +// # Supported algorithms |
| 29 | +// |
| 30 | +// Implementations must register the algorithms they want to support for digests |
| 31 | +// by importing the appropriate implementation in a non-test file. |
| 32 | +// |
| 33 | +// For example, to register the sha256 algorithm implementation from Go's stdlib: |
| 34 | +// |
| 35 | +// import ( |
| 36 | +// _ "crypto/sha256" |
| 37 | +// ) |
| 38 | +// |
| 39 | +// Even though digests may be assemblable as a string, always verify your input |
| 40 | +// with [digest.Parse] or use [digest.Digest.Validate] when accepting untrusted |
| 41 | +// input. While there are measures to avoid common problems, this ensures you |
| 42 | +// have valid digests in the rest of your application. |
| 43 | +// |
| 44 | +// While alternative encodings of hash values (digests) are possible (for example, |
| 45 | +// base64), this package deals exclusively with hex-encoded digests. Refer to |
| 46 | +// the [OCI image specification] for algorithms that are defined as part of the |
| 47 | +// specification. |
| 48 | +// |
| 49 | +// [OCI image specification]: https://github.com/opencontainers/image-spec/blob/v1.0.2/descriptor.md#registered-algorithms |
27 | 50 | package reference
|
28 | 51 |
|
29 | 52 | import (
|
@@ -102,6 +125,16 @@ func (f Field) MarshalText() (p []byte, err error) {
|
102 | 125 | // UnmarshalText parses text bytes by invoking the
|
103 | 126 | // reference parser to ensure the appropriately
|
104 | 127 | // typed reference object is wrapped by field.
|
| 128 | +// |
| 129 | +// An error is returned when unmarshaling a reference that contains a digest with an |
| 130 | +// algorithm that is not registered. Implementations must register the algorithm |
| 131 | +// by importing the appropriate implementation. |
| 132 | +// |
| 133 | +// For example, to register the sha256 algorithm implementation from Go's stdlib: |
| 134 | +// |
| 135 | +// import ( |
| 136 | +// _ "crypto/sha256" |
| 137 | +// ) |
105 | 138 | func (f *Field) UnmarshalText(p []byte) error {
|
106 | 139 | r, err := Parse(string(p))
|
107 | 140 | if err != nil {
|
@@ -181,8 +214,19 @@ func splitDomain(name string) (string, string) {
|
181 | 214 | return match[1], match[2]
|
182 | 215 | }
|
183 | 216 |
|
184 |
| -// Parse parses s and returns a syntactically valid Reference. |
185 |
| -// If an error was encountered it is returned, along with a nil Reference. |
| 217 | +// Parse parses s and returns a syntactically valid [Reference]. It returns a |
| 218 | +// nil Reference if an error occurs. When parsing a reference that contains a |
| 219 | +// digest, an error is returned if the digest's algorithm that is not registered. |
| 220 | +// |
| 221 | +// An error is returned when parsing a reference that contains a digest with an |
| 222 | +// algorithm that is not registered. Implementations must register the algorithm |
| 223 | +// by importing the appropriate implementation. |
| 224 | +// |
| 225 | +// For example, to register the sha256 algorithm implementation from Go's stdlib: |
| 226 | +// |
| 227 | +// import ( |
| 228 | +// _ "crypto/sha256" |
| 229 | +// ) |
186 | 230 | func Parse(s string) (Reference, error) {
|
187 | 231 | matches := ReferenceRegexp.FindStringSubmatch(s)
|
188 | 232 | if matches == nil {
|
|
0 commit comments