1
0
mirror of https://github.com/distribution/distribution synced 2024-11-12 05:45:51 +01:00

Fix Go Idioms

- DRY out SchemaVersion literals
- Better name the predefined Versioned struct for the Image Index
- Var names, declarations, else cases.

Co-authored-by: Milos Gajdos <milosthegajdos@gmail.com>
Signed-off-by: Bracken Dawson <abdawson@gmail.com>
This commit is contained in:
Bracken Dawson 2023-04-21 14:43:21 +01:00
parent 88646f54da
commit 973bfbb676
No known key found for this signature in database
GPG Key ID: 7C6C7FA182101826
4 changed files with 19 additions and 20 deletions

@ -129,7 +129,7 @@ func FromDescriptors(descriptors []ManifestDescriptor) (*DeserializedManifestLis
func fromDescriptorsWithMediaType(descriptors []ManifestDescriptor, mediaType string) (*DeserializedManifestList, error) { func fromDescriptorsWithMediaType(descriptors []ManifestDescriptor, mediaType string) (*DeserializedManifestList, error) {
m := ManifestList{ m := ManifestList{
Versioned: manifest.Versioned{ Versioned: manifest.Versioned{
SchemaVersion: 2, SchemaVersion: SchemaVersion.SchemaVersion,
MediaType: mediaType, MediaType: mediaType,
}, },
} }

@ -11,9 +11,9 @@ import (
v1 "github.com/opencontainers/image-spec/specs-go/v1" v1 "github.com/opencontainers/image-spec/specs-go/v1"
) )
// OCISchemaVersion provides a pre-initialized version structure for this // IndexSchemaVersion provides a pre-initialized version structure for OCI Image
// packages OCIschema version of the manifest. // Indices.
var OCISchemaVersion = manifest.Versioned{ var IndexSchemaVersion = manifest.Versioned{
SchemaVersion: 2, SchemaVersion: 2,
MediaType: v1.MediaTypeImageIndex, MediaType: v1.MediaTypeImageIndex,
} }
@ -69,13 +69,13 @@ type ImageIndex struct {
// References returns the distribution descriptors for the referenced image // References returns the distribution descriptors for the referenced image
// manifests. // manifests.
func (ii ImageIndex) References() []distribution.Descriptor { func (ii ImageIndex) References() []distribution.Descriptor {
dependencies := make([]distribution.Descriptor, len(ii.Manifests)) references := make([]distribution.Descriptor, len(ii.Manifests))
for i := range ii.Manifests { for i := range ii.Manifests {
dependencies[i] = ii.Manifests[i].Descriptor references[i] = ii.Manifests[i].Descriptor
dependencies[i].Platform = ii.Manifests[i].Platform references[i].Platform = ii.Manifests[i].Platform
} }
return dependencies return references
} }
// DeserializedImageIndex wraps ManifestList with a copy of the original // DeserializedImageIndex wraps ManifestList with a copy of the original
@ -96,10 +96,10 @@ func FromDescriptors(descriptors []ManifestDescriptor, annotations map[string]st
} }
// fromDescriptorsWithMediaType is for testing purposes, it's useful to be able to specify the media type explicitly // fromDescriptorsWithMediaType is for testing purposes, it's useful to be able to specify the media type explicitly
func fromDescriptorsWithMediaType(descriptors []ManifestDescriptor, annotations map[string]string, mediaType string) (*DeserializedImageIndex, error) { func fromDescriptorsWithMediaType(descriptors []ManifestDescriptor, annotations map[string]string, mediaType string) (_ *DeserializedImageIndex, err error) {
m := ImageIndex{ m := ImageIndex{
Versioned: manifest.Versioned{ Versioned: manifest.Versioned{
SchemaVersion: 2, SchemaVersion: IndexSchemaVersion.SchemaVersion,
MediaType: mediaType, MediaType: mediaType,
}, },
Annotations: annotations, Annotations: annotations,
@ -112,7 +112,6 @@ func fromDescriptorsWithMediaType(descriptors []ManifestDescriptor, annotations
ImageIndex: m, ImageIndex: m,
} }
var err error
deserialized.canonical, err = json.MarshalIndent(&m, "", " ") deserialized.canonical, err = json.MarshalIndent(&m, "", " ")
return &deserialized, err return &deserialized, err
} }
@ -147,11 +146,9 @@ func (m *DeserializedImageIndex) MarshalJSON() ([]byte, error) {
// Payload returns the raw content of the manifest list. The contents can be // Payload returns the raw content of the manifest list. The contents can be
// used to calculate the content identifier. // used to calculate the content identifier.
func (m DeserializedImageIndex) Payload() (string, []byte, error) { func (m DeserializedImageIndex) Payload() (string, []byte, error) {
var mediaType string mediaType := m.MediaType
if m.MediaType == "" { if m.MediaType == "" {
mediaType = v1.MediaTypeImageIndex mediaType = v1.MediaTypeImageIndex
} else {
mediaType = m.MediaType
} }
return mediaType, m.canonical, nil return mediaType, m.canonical, nil

@ -11,10 +11,10 @@ import (
v1 "github.com/opencontainers/image-spec/specs-go/v1" v1 "github.com/opencontainers/image-spec/specs-go/v1"
) )
// SchemaVersion provides a pre-initialized version structure for this // SchemaVersion provides a pre-initialized version structure for OCI Image
// packages version of the manifest. // Manifests
var SchemaVersion = manifest.Versioned{ var SchemaVersion = manifest.Versioned{
SchemaVersion: 2, // historical value here.. does not pertain to OCI or docker version SchemaVersion: 2,
MediaType: v1.MediaTypeImageManifest, MediaType: v1.MediaTypeImageManifest,
} }

@ -34,17 +34,19 @@ func (ms *manifestListHandler) Unmarshal(ctx context.Context, dgst digest.Digest
func (ms *manifestListHandler) Put(ctx context.Context, manifestList distribution.Manifest, skipDependencyVerification bool) (digest.Digest, error) { func (ms *manifestListHandler) Put(ctx context.Context, manifestList distribution.Manifest, skipDependencyVerification bool) (digest.Digest, error) {
dcontext.GetLogger(ms.ctx).Debug("(*manifestListHandler).Put") dcontext.GetLogger(ms.ctx).Debug("(*manifestListHandler).Put")
var schemaVersion int var schemaVersion, expectedSchemaVersion int
switch m := manifestList.(type) { switch m := manifestList.(type) {
case *manifestlist.DeserializedManifestList: case *manifestlist.DeserializedManifestList:
expectedSchemaVersion = manifestlist.SchemaVersion.SchemaVersion
schemaVersion = m.SchemaVersion schemaVersion = m.SchemaVersion
case *ocischema.DeserializedImageIndex: case *ocischema.DeserializedImageIndex:
expectedSchemaVersion = ocischema.IndexSchemaVersion.SchemaVersion
schemaVersion = m.SchemaVersion schemaVersion = m.SchemaVersion
default: default:
return "", fmt.Errorf("wrong type put to manifestListHandler: %T", manifestList) return "", fmt.Errorf("wrong type put to manifestListHandler: %T", manifestList)
} }
if schemaVersion != 2 { if schemaVersion != expectedSchemaVersion {
return "", fmt.Errorf("unrecognized manifest list schema version %d", schemaVersion) return "", fmt.Errorf("unrecognized manifest list schema version %d, expected %d", schemaVersion, expectedSchemaVersion)
} }
if err := ms.verifyManifest(ms.ctx, manifestList, skipDependencyVerification); err != nil { if err := ms.verifyManifest(ms.ctx, manifestList, skipDependencyVerification); err != nil {