Skip to content

Commit

Permalink
manifest: slight cleanup of init / registration
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
thaJeztah committed Jul 16, 2024
1 parent f22dd61 commit 3d0239a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 77 deletions.
35 changes: 17 additions & 18 deletions manifest/manifestlist/manifestlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,26 @@ 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
}
if err := distribution.RegisterManifestSchema(MediaTypeManifestList, unmarshalManifestList); err != nil {
panic(fmt.Sprintf("Unable to register manifest: %s", err))
}
}

dgst := digest.FromBytes(b)
return m, distribution.Descriptor{Digest: dgst, Size: int64(len(b)), MediaType: MediaTypeManifestList}, err
func unmarshalManifestList(b []byte) (distribution.Manifest, distribution.Descriptor, error) {
m := &DeserializedManifestList{}
if err := m.UnmarshalJSON(b); err != nil {
return nil, distribution.Descriptor{}, err
}
err := distribution.RegisterManifestSchema(MediaTypeManifestList, manifestListFunc)
if err != nil {
panic(fmt.Sprintf("Unable to register manifest: %s", 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
Expand Down
50 changes: 23 additions & 27 deletions manifest/ocischema/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 19 additions & 20 deletions manifest/ocischema/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 14 additions & 12 deletions manifest/schema2/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3d0239a

Please sign in to comment.