manifest: slight cleanup of init / registration

Change the marshal-funcs to a regular function instead of definining
as part of an init and remove some intermediate variables.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2024-07-16 12:02:28 +02:00
parent f22dd61860
commit 3d0239ac6f
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
4 changed files with 74 additions and 78 deletions

View file

@ -24,27 +24,26 @@ var SchemaVersion = manifest.Versioned{
} }
func init() { func init() {
manifestListFunc := func(b []byte) (distribution.Manifest, distribution.Descriptor, error) { if err := distribution.RegisterManifestSchema(MediaTypeManifestList, unmarshalManifestList); err != nil {
m := new(DeserializedManifestList) panic(fmt.Sprintf("Unable to register manifest: %s", err))
err := m.UnmarshalJSON(b) }
if err != nil { }
func unmarshalManifestList(b []byte) (distribution.Manifest, distribution.Descriptor, error) {
m := &DeserializedManifestList{}
if err := m.UnmarshalJSON(b); err != nil {
return nil, distribution.Descriptor{}, err return nil, distribution.Descriptor{}, err
} }
if m.MediaType != MediaTypeManifestList { if m.MediaType != MediaTypeManifestList {
err = fmt.Errorf("mediaType in manifest list should be '%s' not '%s'", return nil, distribution.Descriptor{}, fmt.Errorf("mediaType in manifest list should be '%s' not '%s'", MediaTypeManifestList, m.MediaType)
MediaTypeManifestList, m.MediaType)
return nil, distribution.Descriptor{}, err
} }
dgst := digest.FromBytes(b) return m, distribution.Descriptor{
return m, distribution.Descriptor{Digest: dgst, Size: int64(len(b)), MediaType: MediaTypeManifestList}, err Digest: digest.FromBytes(b),
} Size: int64(len(b)),
err := distribution.RegisterManifestSchema(MediaTypeManifestList, manifestListFunc) MediaType: MediaTypeManifestList,
if err != nil { }, nil
panic(fmt.Sprintf("Unable to register manifest: %s", err))
}
} }
// PlatformSpec specifies a platform where a particular image manifest is // PlatformSpec specifies a platform where a particular image manifest is

View file

@ -19,35 +19,31 @@ var IndexSchemaVersion = manifest.Versioned{
} }
func init() { func init() {
imageIndexFunc := func(b []byte) (distribution.Manifest, distribution.Descriptor, error) { if err := distribution.RegisterManifestSchema(v1.MediaTypeImageIndex, unmarshalImageIndex); err != nil {
panic(fmt.Sprintf("Unable to register OCI Image Index: %s", err))
}
}
func unmarshalImageIndex(b []byte) (distribution.Manifest, distribution.Descriptor, error) {
if err := validateIndex(b); err != nil { if err := validateIndex(b); err != nil {
return nil, distribution.Descriptor{}, err return nil, distribution.Descriptor{}, err
} }
m := new(DeserializedImageIndex)
err := m.UnmarshalJSON(b) m := &DeserializedImageIndex{}
if err != nil { if err := m.UnmarshalJSON(b); err != nil {
return nil, distribution.Descriptor{}, err return nil, distribution.Descriptor{}, err
} }
if m.MediaType != "" && m.MediaType != v1.MediaTypeImageIndex { if m.MediaType != "" && m.MediaType != v1.MediaTypeImageIndex {
err = fmt.Errorf("if present, mediaType in image index should be '%s' not '%s'", return nil, distribution.Descriptor{}, fmt.Errorf("if present, mediaType in image index should be '%s' not '%s'", v1.MediaTypeImageIndex, m.MediaType)
v1.MediaTypeImageIndex, m.MediaType)
return nil, distribution.Descriptor{}, err
} }
dgst := digest.FromBytes(b)
return m, distribution.Descriptor{ return m, distribution.Descriptor{
MediaType: v1.MediaTypeImageIndex, MediaType: v1.MediaTypeImageIndex,
Digest: dgst, Digest: digest.FromBytes(b),
Size: int64(len(b)), Size: int64(len(b)),
Annotations: m.Annotations, Annotations: m.Annotations,
}, err }, nil
}
err := distribution.RegisterManifestSchema(v1.MediaTypeImageIndex, imageIndexFunc)
if err != nil {
panic(fmt.Sprintf("Unable to register OCI Image Index: %s", err))
}
} }
// ImageIndex references manifests for various platforms. // ImageIndex references manifests for various platforms.

View file

@ -19,28 +19,27 @@ var SchemaVersion = manifest.Versioned{
} }
func init() { func init() {
ocischemaFunc := func(b []byte) (distribution.Manifest, distribution.Descriptor, error) { if err := distribution.RegisterManifestSchema(v1.MediaTypeImageManifest, unmarshalOCISchema); err != nil {
panic(fmt.Sprintf("Unable to register manifest: %s", err))
}
}
func unmarshalOCISchema(b []byte) (distribution.Manifest, distribution.Descriptor, error) {
if err := validateManifest(b); err != nil { if err := validateManifest(b); err != nil {
return nil, distribution.Descriptor{}, err return nil, distribution.Descriptor{}, err
} }
m := new(DeserializedManifest)
err := m.UnmarshalJSON(b) m := &DeserializedManifest{}
if err != nil { if err := m.UnmarshalJSON(b); err != nil {
return nil, distribution.Descriptor{}, err return nil, distribution.Descriptor{}, err
} }
dgst := digest.FromBytes(b)
return m, distribution.Descriptor{ return m, distribution.Descriptor{
MediaType: v1.MediaTypeImageManifest, MediaType: v1.MediaTypeImageManifest,
Digest: dgst, Digest: digest.FromBytes(b),
Size: int64(len(b)), Size: int64(len(b)),
Annotations: m.Annotations, Annotations: m.Annotations,
}, err }, nil
}
err := distribution.RegisterManifestSchema(v1.MediaTypeImageManifest, ocischemaFunc)
if err != nil {
panic(fmt.Sprintf("Unable to register manifest: %s", err))
}
} }
// Manifest defines a ocischema manifest. // Manifest defines a ocischema manifest.

View file

@ -41,20 +41,22 @@ var SchemaVersion = manifest.Versioned{
} }
func init() { func init() {
schema2Func := func(b []byte) (distribution.Manifest, distribution.Descriptor, error) { if err := distribution.RegisterManifestSchema(MediaTypeManifest, unmarshalSchema2); err != nil {
m := new(DeserializedManifest) panic(fmt.Sprintf("Unable to register manifest: %s", err))
err := m.UnmarshalJSON(b) }
if err != nil { }
func unmarshalSchema2(b []byte) (distribution.Manifest, distribution.Descriptor, error) {
m := &DeserializedManifest{}
if err := m.UnmarshalJSON(b); err != nil {
return nil, distribution.Descriptor{}, err return nil, distribution.Descriptor{}, err
} }
dgst := digest.FromBytes(b) return m, distribution.Descriptor{
return m, distribution.Descriptor{Digest: dgst, Size: int64(len(b)), MediaType: MediaTypeManifest}, err Digest: digest.FromBytes(b),
} Size: int64(len(b)),
err := distribution.RegisterManifestSchema(MediaTypeManifest, schema2Func) MediaType: MediaTypeManifest,
if err != nil { }, nil
panic(fmt.Sprintf("Unable to register manifest: %s", err))
}
} }
// Manifest defines a schema2 manifest. // Manifest defines a schema2 manifest.