Skip to content

Commit

Permalink
Account for version when caching schemas
Browse files Browse the repository at this point in the history
We also make it an explicitly handled error to get back a schema with
the wrong version.
  • Loading branch information
iwahbe committed Nov 12, 2022
1 parent 5c1c462 commit 76589af
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
23 changes: 20 additions & 3 deletions pkg/codegen/schema/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,31 @@ func (l *pluginLoader) LoadPackageReference(pkg string, version *semver.Version)
}

func LoadPackageReference(loader Loader, pkg string, version *semver.Version) (PackageReference, error) {
var ref PackageReference
var err error
if refLoader, ok := loader.(ReferenceLoader); ok {
return refLoader.LoadPackageReference(pkg, version)
ref, err = refLoader.LoadPackageReference(pkg, version)
} else {
p, pErr := loader.LoadPackage(pkg, version)
err = pErr
if err == nil {
ref = p.Reference()
}
}
p, err := loader.LoadPackage(pkg, version)

if err != nil {
return nil, err
}
return p.Reference(), nil

if pkg != ref.Name() || version != nil && ref.Version() != nil && !ref.Version().Equals(*version) {
if l, ok := loader.(*pluginLoader); ok {
return nil, fmt.Errorf("req: %s@%v: entries: %v (returned %s@%v)", pkg, version,
l.entries, ref.Name(), ref.Version())
}
return nil, fmt.Errorf("loader returned %s@%v: expected %s@%v", ref.Name(), ref.Version(), pkg, version)
}

return ref, nil
}

func (l *pluginLoader) loadSchemaBytes(pkg string, version *semver.Version) ([]byte, *semver.Version, error) {
Expand Down
6 changes: 5 additions & 1 deletion sdk/go/common/workspace/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,11 @@ func (info *PluginInfo) SetFileMetadata(path string) error {
info.LastUsedTime = tinfo.AccessTime()

if info.Kind == ResourcePlugin {
info.SchemaPath = filepath.Join(filepath.Dir(path), "schema-"+info.Name+".json")
var v string
if info.Version != nil {
v = "-" + info.Version.String() + "-"
}
info.SchemaPath = filepath.Join(filepath.Dir(path), "schema-"+info.Name+v+".json")
info.SchemaTime = tinfo.ModTime()
}

Expand Down

0 comments on commit 76589af

Please sign in to comment.