1
0
mirror of https://github.com/distribution/distribution synced 2024-11-06 19:35:52 +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) {
m := ManifestList{
Versioned: manifest.Versioned{
SchemaVersion: 2,
SchemaVersion: SchemaVersion.SchemaVersion,
MediaType: mediaType,
},
}

@ -11,9 +11,9 @@ import (
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)
// OCISchemaVersion provides a pre-initialized version structure for this
// packages OCIschema version of the manifest.
var OCISchemaVersion = manifest.Versioned{
// IndexSchemaVersion provides a pre-initialized version structure for OCI Image
// Indices.
var IndexSchemaVersion = manifest.Versioned{
SchemaVersion: 2,
MediaType: v1.MediaTypeImageIndex,
}
@ -69,13 +69,13 @@ type ImageIndex struct {
// References returns the distribution descriptors for the referenced image
// manifests.
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 {
dependencies[i] = ii.Manifests[i].Descriptor
dependencies[i].Platform = ii.Manifests[i].Platform
references[i] = ii.Manifests[i].Descriptor
references[i].Platform = ii.Manifests[i].Platform
}
return dependencies
return references
}
// 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
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{
Versioned: manifest.Versioned{
SchemaVersion: 2,
SchemaVersion: IndexSchemaVersion.SchemaVersion,
MediaType: mediaType,
},
Annotations: annotations,
@ -112,7 +112,6 @@ func fromDescriptorsWithMediaType(descriptors []ManifestDescriptor, annotations
ImageIndex: m,
}
var err error
deserialized.canonical, err = json.MarshalIndent(&m, "", " ")
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
// used to calculate the content identifier.
func (m DeserializedImageIndex) Payload() (string, []byte, error) {
var mediaType string
mediaType := m.MediaType
if m.MediaType == "" {
mediaType = v1.MediaTypeImageIndex
} else {
mediaType = m.MediaType
}
return mediaType, m.canonical, nil

@ -11,10 +11,10 @@ import (
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)
// SchemaVersion provides a pre-initialized version structure for this
// packages version of the manifest.
// SchemaVersion provides a pre-initialized version structure for OCI Image
// Manifests
var SchemaVersion = manifest.Versioned{
SchemaVersion: 2, // historical value here.. does not pertain to OCI or docker version
SchemaVersion: 2,
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) {
dcontext.GetLogger(ms.ctx).Debug("(*manifestListHandler).Put")
var schemaVersion int
var schemaVersion, expectedSchemaVersion int
switch m := manifestList.(type) {
case *manifestlist.DeserializedManifestList:
expectedSchemaVersion = manifestlist.SchemaVersion.SchemaVersion
schemaVersion = m.SchemaVersion
case *ocischema.DeserializedImageIndex:
expectedSchemaVersion = ocischema.IndexSchemaVersion.SchemaVersion
schemaVersion = m.SchemaVersion
default:
return "", fmt.Errorf("wrong type put to manifestListHandler: %T", manifestList)
}
if schemaVersion != 2 {
return "", fmt.Errorf("unrecognized manifest list schema version %d", schemaVersion)
if schemaVersion != expectedSchemaVersion {
return "", fmt.Errorf("unrecognized manifest list schema version %d, expected %d", schemaVersion, expectedSchemaVersion)
}
if err := ms.verifyManifest(ms.ctx, manifestList, skipDependencyVerification); err != nil {