manifest: slight cleanup of init / registration (#4403)

This commit is contained in:
Wang Yan 2024-07-18 19:53:48 +08:00 committed by GitHub
commit e0503319b2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 74 additions and 78 deletions

View file

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

View file

@ -19,37 +19,33 @@ var IndexSchemaVersion = manifest.Versioned{
}
func init() {
imageIndexFunc := func(b []byte) (distribution.Manifest, distribution.Descriptor, error) {
if err := validateIndex(b); err != nil {
return nil, distribution.Descriptor{}, err
}
m := new(DeserializedImageIndex)
err := m.UnmarshalJSON(b)
if err != nil {
return nil, distribution.Descriptor{}, err
}
if m.MediaType != "" && m.MediaType != v1.MediaTypeImageIndex {
err = fmt.Errorf("if present, mediaType in image index should be '%s' not '%s'",
v1.MediaTypeImageIndex, m.MediaType)
return nil, distribution.Descriptor{}, err
}
dgst := digest.FromBytes(b)
return m, distribution.Descriptor{
MediaType: v1.MediaTypeImageIndex,
Digest: dgst,
Size: int64(len(b)),
Annotations: m.Annotations,
}, err
}
err := distribution.RegisterManifestSchema(v1.MediaTypeImageIndex, imageIndexFunc)
if err != nil {
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 {
return nil, distribution.Descriptor{}, err
}
m := &DeserializedImageIndex{}
if err := m.UnmarshalJSON(b); err != nil {
return nil, distribution.Descriptor{}, err
}
if m.MediaType != "" && m.MediaType != v1.MediaTypeImageIndex {
return nil, distribution.Descriptor{}, fmt.Errorf("if present, mediaType in image index should be '%s' not '%s'", v1.MediaTypeImageIndex, m.MediaType)
}
return m, distribution.Descriptor{
MediaType: v1.MediaTypeImageIndex,
Digest: digest.FromBytes(b),
Size: int64(len(b)),
Annotations: m.Annotations,
}, nil
}
// ImageIndex references manifests for various platforms.
type ImageIndex struct {
manifest.Versioned

View file

@ -19,30 +19,29 @@ var SchemaVersion = manifest.Versioned{
}
func init() {
ocischemaFunc := func(b []byte) (distribution.Manifest, distribution.Descriptor, error) {
if err := validateManifest(b); err != nil {
return nil, distribution.Descriptor{}, err
}
m := new(DeserializedManifest)
err := m.UnmarshalJSON(b)
if err != nil {
return nil, distribution.Descriptor{}, err
}
dgst := digest.FromBytes(b)
return m, distribution.Descriptor{
MediaType: v1.MediaTypeImageManifest,
Digest: dgst,
Size: int64(len(b)),
Annotations: m.Annotations,
}, err
}
err := distribution.RegisterManifestSchema(v1.MediaTypeImageManifest, ocischemaFunc)
if err != nil {
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 {
return nil, distribution.Descriptor{}, err
}
m := &DeserializedManifest{}
if err := m.UnmarshalJSON(b); err != nil {
return nil, distribution.Descriptor{}, err
}
return m, distribution.Descriptor{
MediaType: v1.MediaTypeImageManifest,
Digest: digest.FromBytes(b),
Size: int64(len(b)),
Annotations: m.Annotations,
}, nil
}
// Manifest defines a ocischema manifest.
type Manifest struct {
manifest.Versioned

View file

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