@@ -3,12 +3,24 @@ package publisher
33import (
44 "errors"
55 "fmt"
6- "strings"
76
87 "github.com/MontFerret/specs/pkg/api"
98 apicatalog "github.com/MontFerret/specs/pkg/api/catalog"
109)
1110
11+ type functionIdentity struct {
12+ Namespace string
13+ Name string
14+ }
15+
16+ func (identity functionIdentity ) String () string {
17+ if identity .Namespace == "" {
18+ return identity .Name
19+ }
20+
21+ return identity .Namespace + "::" + identity .Name
22+ }
23+
1224func validatePair (reference * api.Reference , catalog * apicatalog.Catalog ) error {
1325 problems := make ([]error , 0 )
1426 if catalog .ID != reference .ID {
@@ -19,53 +31,39 @@ func validatePair(reference *api.Reference, catalog *apicatalog.Catalog) error {
1931 problems = append (problems , fmt .Errorf ("catalog version %q does not match API version %q" , catalog .Version , reference .Version ))
2032 }
2133
22- globalFunctions := make (map [string ]struct {})
23- namespaceRoots := make (map [string ]struct {})
34+ apiFunctions := make (map [functionIdentity ]struct {})
35+ apiNamespaces := make (map [string ]map [ string ] struct {}, len ( reference . Namespaces ) )
2436 for _ , namespace := range reference .Namespaces {
25- if namespace .Name == "" {
26- for _ , function := range namespace .Functions {
27- globalFunctions [function .Name ] = struct {}{}
28- }
29-
30- continue
37+ functions := make (map [string ]struct {}, len (namespace .Functions ))
38+ apiNamespaces [namespace .Name ] = functions
39+ for _ , function := range namespace .Functions {
40+ functions [function .Name ] = struct {}{}
41+ apiFunctions [functionIdentity {Namespace : namespace .Name , Name : function .Name }] = struct {}{}
3142 }
32-
33- root , _ , _ := strings .Cut (namespace .Name , "::" )
34- namespaceRoots [root ] = struct {}{}
3543 }
3644
37- categorized := make (map [string ]string )
45+ categorized := make (map [functionIdentity ]string )
3846 for _ , category := range catalog .Categories {
3947 for _ , function := range category .Functions {
40- if _ , exists := globalFunctions [function ]; ! exists {
41- problems = append (problems , fmt .Errorf ("catalog category %q references unknown global function %q" , category .ID , function ))
48+ identity := functionIdentity {Namespace : function .Namespace , Name : function .Name }
49+ namespace , exists := apiNamespaces [function .Namespace ]
50+ if ! exists {
51+ problems = append (problems , fmt .Errorf ("catalog category %q references unknown API namespace %q" , category .ID , function .Namespace ))
52+ } else if _ , exists := namespace [function .Name ]; ! exists {
53+ problems = append (problems , fmt .Errorf ("catalog category %q references unknown function %q in API namespace %q" , category .ID , function .Name , function .Namespace ))
4254 }
4355
44- if previous , exists := categorized [function ]; exists {
45- problems = append (problems , fmt .Errorf ("global function %q is assigned to categories %q and %q" , function , previous , category .ID ))
56+ if previous , exists := categorized [identity ]; exists {
57+ problems = append (problems , fmt .Errorf ("function %q is assigned to categories %q and %q" , identity , previous , category .ID ))
4658 }
4759
48- categorized [function ] = category .ID
60+ categorized [identity ] = category .ID
4961 }
5062 }
5163
52- for function := range globalFunctions {
64+ for function := range apiFunctions {
5365 if _ , exists := categorized [function ]; ! exists {
54- problems = append (problems , fmt .Errorf ("global function %q is not assigned to a catalog category" , function ))
55- }
56- }
57-
58- declaredRoots := make (map [string ]struct {}, len (catalog .NamespaceRoots ))
59- for _ , root := range catalog .NamespaceRoots {
60- declaredRoots [root ] = struct {}{}
61- if _ , exists := namespaceRoots [root ]; ! exists {
62- problems = append (problems , fmt .Errorf ("catalog namespace root %q does not cover an API namespace" , root ))
63- }
64- }
65-
66- for root := range namespaceRoots {
67- if _ , exists := declaredRoots [root ]; ! exists {
68- problems = append (problems , fmt .Errorf ("API namespace root %q is not declared by the catalog" , root ))
66+ problems = append (problems , fmt .Errorf ("function %q is not assigned to a catalog category" , function ))
6967 }
7068 }
7169
0 commit comments