github.com/docker/cnab-to-oci@v0.3.0-beta4/remotes/typeless.go (about) 1 package remotes 2 3 import ( 4 "encoding/json" 5 6 ocischemav1 "github.com/opencontainers/image-spec/specs-go/v1" 7 ) 8 9 type typelessManifestList struct { 10 Manifests []typelessDescriptor 11 extras map[string]json.RawMessage 12 } 13 14 func (m *typelessManifestList) MarshalJSON() ([]byte, error) { 15 data := map[string]json.RawMessage{} 16 for k, v := range m.extras { 17 data[k] = v 18 } 19 if len(m.Manifests) != 0 { 20 manifestsJSON, err := json.Marshal(m.Manifests) 21 if err != nil { 22 return nil, err 23 } 24 data["manifests"] = json.RawMessage(manifestsJSON) 25 } 26 return json.Marshal(data) 27 } 28 29 func (m *typelessManifestList) UnmarshalJSON(source []byte) error { 30 var data map[string]json.RawMessage 31 if err := json.Unmarshal(source, &data); err != nil { 32 return err 33 } 34 if manifestsJSON, ok := data["manifests"]; ok { 35 if err := json.Unmarshal(manifestsJSON, &m.Manifests); err != nil { 36 return err 37 } 38 delete(data, "manifests") 39 } 40 m.extras = data 41 return nil 42 } 43 44 type typelessDescriptor struct { 45 Platform *ocischemav1.Platform 46 extras map[string]json.RawMessage 47 } 48 49 func (d *typelessDescriptor) MarshalJSON() ([]byte, error) { 50 data := map[string]json.RawMessage{} 51 for k, v := range d.extras { 52 data[k] = v 53 } 54 if d.Platform != nil { 55 platJSON, err := json.Marshal(d.Platform) 56 if err != nil { 57 return nil, err 58 } 59 data["platform"] = json.RawMessage(platJSON) 60 } 61 return json.Marshal(data) 62 } 63 64 func (d *typelessDescriptor) UnmarshalJSON(source []byte) error { 65 var data map[string]json.RawMessage 66 if err := json.Unmarshal(source, &data); err != nil { 67 return err 68 } 69 if platJSON, ok := data["platform"]; ok { 70 var plat ocischemav1.Platform 71 if err := json.Unmarshal(platJSON, &plat); err != nil { 72 return err 73 } 74 d.Platform = &plat 75 delete(data, "platform") 76 } 77 d.extras = data 78 return nil 79 }