@@ -12,15 +12,16 @@ import (
1212)
1313
1414type Registry struct {
15- schemas map [string ]* Schema
15+ schemas map [string ]* Schema
16+ acceptedEnvelopeVersions map [string ]struct {}
1617}
1718
1819func NewRegistry (efs embed.FS , dir string ) (* Registry , error ) {
1920 entries , err := efs .ReadDir (dir )
2021 if err != nil {
2122 return nil , fmt .Errorf ("read embedded dir %q: %w" , dir , err )
2223 }
23- reg := & Registry { schemas : make ( map [ string ] * Schema )}
24+ reg := newEmptyRegistry ()
2425 for _ , entry := range entries {
2526 if entry .IsDir () {
2627 continue
@@ -37,12 +38,10 @@ func NewRegistry(efs embed.FS, dir string) (*Registry, error) {
3738 if s == nil {
3839 continue
3940 }
40- if existing , ok := reg .schemas [s .Name ]; ok {
41+ if _ , ok := reg .schemas [s .Name ]; ok {
4142 return nil , fmt .Errorf ("duplicate schema name %q (in %s and earlier source)" , s .Name , path )
42- } else {
43- _ = existing
4443 }
45- reg .schemas [ s . Name ] = s
44+ reg .registerSchema ( s )
4645 }
4746 return reg , nil
4847}
@@ -52,7 +51,7 @@ func newRegistryFromFS(efs fs.ReadDirFS, dir string) (*Registry, error) {
5251 if err != nil {
5352 return nil , fmt .Errorf ("read dir %q: %w" , dir , err )
5453 }
55- reg := & Registry { schemas : make ( map [ string ] * Schema )}
54+ reg := newEmptyRegistry ()
5655 for _ , entry := range entries {
5756 if entry .IsDir () {
5857 continue
@@ -66,19 +65,46 @@ func newRegistryFromFS(efs fs.ReadDirFS, dir string) (*Registry, error) {
6665 return nil , fmt .Errorf ("parse %s: %w" , entry .Name (), err )
6766 }
6867 if s != nil {
69- reg .schemas [ s . Name ] = s
68+ reg .registerSchema ( s )
7069 }
7170 }
7271 return reg , nil
7372}
7473
74+ func newEmptyRegistry () * Registry {
75+ return & Registry {
76+ schemas : make (map [string ]* Schema ),
77+ acceptedEnvelopeVersions : map [string ]struct {}{
78+ // v0.1.0 is the manifest-level envelope version. Kind schema
79+ // versions are added as schemas load.
80+ "v0.1.0" : {},
81+ },
82+ }
83+ }
84+
85+ func (r * Registry ) registerSchema (s * Schema ) {
86+ r .schemas [s .Name ] = s
87+ if s .Version == "" {
88+ return
89+ }
90+ r .acceptedEnvelopeVersions [s .Version ] = struct {}{}
91+ }
92+
7593func (r * Registry ) Lookup (kind string ) * Schema {
7694 if r == nil {
7795 return nil
7896 }
7997 return r .schemas [kind ]
8098}
8199
100+ func (r * Registry ) AcceptsVersion (version string ) bool {
101+ if r == nil {
102+ return false
103+ }
104+ _ , ok := r .acceptedEnvelopeVersions [version ]
105+ return ok
106+ }
107+
82108func (r * Registry ) Kinds () []string {
83109 if r == nil {
84110 return nil
0 commit comments