11package model
22
33import (
4+ "encoding/json"
5+
46 "github.com/crazy-max/diun/v4/pkg/registry"
7+ "github.com/pkg/errors"
58)
69
710// Image holds image configuration
@@ -20,6 +23,38 @@ type Image struct {
2023 Metadata map [string ]string `yaml:"metadata,omitempty" json:",omitempty"`
2124}
2225
26+ func (i Image ) hash () (string , error ) {
27+ // Return json serialized image to use as a hashable key
28+ b , err := json .Marshal (i )
29+ if err != nil {
30+ return "" , errors .Errorf ("cannot hash image: %v" , err )
31+ }
32+
33+ return string (b ), nil
34+ }
35+
36+ type ImageList []Image
37+
38+ // Dedupe removes duplicate images from the list and returns a new list
39+ func (il ImageList ) Dedupe () []Image {
40+ keys := make (map [string ]bool )
41+ list := []Image {}
42+ for _ , entry := range il {
43+ hash , err := entry .hash ()
44+ if err != nil {
45+ // If we couldn't hash the entry, we can't dedupe it so we add it anyway
46+ list = append (list , entry )
47+ } else {
48+ if _ , value := keys [hash ]; ! value {
49+ keys [hash ] = true
50+ list = append (list , entry )
51+ }
52+ }
53+ }
54+
55+ return list
56+ }
57+
2358// ImagePlatform holds image platform configuration
2459type ImagePlatform struct {
2560 OS string `yaml:"os,omitempty" json:",omitempty"`
0 commit comments