github.com/snyk/vervet/v4@v4.27.2/resource_versions.go (about) 1 package vervet 2 3 import ( 4 "fmt" 5 "sort" 6 "time" 7 8 "github.com/getkin/kin-openapi/openapi3" 9 ) 10 11 type resourceVersionsSlice []*ResourceVersions 12 13 func (s resourceVersionsSlice) validate() error { 14 for _, v := range s.versions() { 15 resourcePaths := map[string]string{} 16 for _, eps := range s { 17 ep, err := eps.At(v.String()) 18 if err == ErrNoMatchingVersion { 19 continue 20 } else if err != nil { 21 return fmt.Errorf("validation failed: %w", err) 22 } 23 for path := range ep.Paths { 24 if conflict, ok := resourcePaths[path]; ok { 25 return fmt.Errorf("conflict: %q %q", conflict, ep.sourcePrefix) 26 } 27 resourcePaths[path] = ep.sourcePrefix 28 } 29 } 30 } 31 return nil 32 } 33 34 func (s resourceVersionsSlice) versions() VersionSlice { 35 vset := map[Version]bool{} 36 for _, eps := range s { 37 for i := range eps.versions { 38 vset[eps.versions[i].Version] = true 39 } 40 } 41 var versions VersionSlice 42 for v := range vset { 43 versions = append(versions, v) 44 } 45 sort.Sort(versions) 46 return versions 47 } 48 49 func (s resourceVersionsSlice) at(v Version) (*openapi3.T, error) { 50 coll := NewCollator() 51 for _, eps := range s { 52 ep, err := eps.At(v.String()) 53 if err == ErrNoMatchingVersion { 54 continue 55 } else if err != nil { 56 return nil, err 57 } 58 err = coll.Collate(ep) 59 if err != nil { 60 return nil, err 61 } 62 } 63 result := coll.Result() 64 if result == nil { 65 return nil, ErrNoMatchingVersion 66 } 67 if result.ExtensionProps.Extensions == nil { 68 result.ExtensionProps.Extensions = map[string]interface{}{} 69 } 70 result.ExtensionProps.Extensions[ExtSnykApiLifecycle] = v.LifecycleAt(time.Time{}).String() 71 return result, nil 72 }