From ff1f2cbff6b0685f2f46e48856157675166070e2 Mon Sep 17 00:00:00 2001 From: amarkdotdev Date: Tue, 7 Jul 2026 07:23:59 +0300 Subject: [PATCH] imagetools: unmarshal descriptor JSON once in parseSource Parse schemaVersion and descriptor fields in a single pass instead of calling json.Unmarshal twice. Signed-off-by: amarkdotdev --- commands/imagetools/create.go | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/commands/imagetools/create.go b/commands/imagetools/create.go index 7d1af673f..037c07489 100644 --- a/commands/imagetools/create.go +++ b/commands/imagetools/create.go @@ -353,25 +353,22 @@ func parseSource(in string) (*imagetools.Source, error) { return nil, err } - var s imagetools.Source - if err := json.Unmarshal([]byte(in), &s.Desc); err != nil { + var parsed struct { + SchemaVersion int `json:"schemaVersion"` + ocispecs.Descriptor + } + if err := json.Unmarshal([]byte(in), &parsed); err != nil { return nil, errors.WithStack(err) } - if err := validateDescriptorJSON(in, s.Desc); err != nil { + if err := validateDescriptor(parsed.Descriptor, parsed.SchemaVersion); err != nil { return nil, err } - return &s, nil + return &imagetools.Source{Desc: parsed.Descriptor}, nil } -func validateDescriptorJSON(raw string, desc ocispecs.Descriptor) error { - var meta struct { - SchemaVersion int `json:"schemaVersion"` - } - if err := json.Unmarshal([]byte(raw), &meta); err != nil { - return errors.WithStack(err) - } - if meta.SchemaVersion != 0 { - return errors.Errorf("expected an OCI content descriptor, got a manifest or index (schemaVersion %d)", meta.SchemaVersion) +func validateDescriptor(desc ocispecs.Descriptor, schemaVersion int) error { + if schemaVersion != 0 { + return errors.Errorf("expected an OCI content descriptor, got a manifest or index (schemaVersion %d)", schemaVersion) } if desc.Digest == "" { return errors.Errorf("invalid descriptor: digest is required")