2020-08-10 19:21:50 +02:00
|
|
|
package gen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-03-17 14:20:05 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
|
2020-08-10 19:21:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// CRDTypes returns a map from each module name to a buffer containing the
|
|
|
|
// code for its generated types.
|
|
|
|
func CRDTypes(tool string, pkg *schema.Package) (map[string]*bytes.Buffer, error) {
|
|
|
|
if err := pkg.ImportLanguages(map[string]schema.Language{"go": Importer}); err != nil {
|
|
|
|
return map[string]*bytes.Buffer{}, err
|
|
|
|
}
|
|
|
|
|
2020-08-19 22:58:13 +02:00
|
|
|
var goPkgInfo GoPackageInfo
|
|
|
|
if goInfo, ok := pkg.Language["go"].(GoPackageInfo); ok {
|
|
|
|
goPkgInfo = goInfo
|
|
|
|
}
|
|
|
|
packages := generatePackageContextMap(tool, pkg, goPkgInfo)
|
2020-08-10 19:21:50 +02:00
|
|
|
|
|
|
|
var pkgMods []string
|
|
|
|
for mod := range packages {
|
|
|
|
pkgMods = append(pkgMods, mod)
|
|
|
|
}
|
|
|
|
|
|
|
|
buffers := map[string]*bytes.Buffer{}
|
|
|
|
|
|
|
|
for _, mod := range pkgMods {
|
|
|
|
pkg := packages[mod]
|
|
|
|
buffer := &bytes.Buffer{}
|
|
|
|
|
|
|
|
for _, r := range pkg.resources {
|
2021-01-15 19:06:57 +01:00
|
|
|
importsAndAliases := map[string]string{}
|
|
|
|
pkg.getImports(r, importsAndAliases)
|
|
|
|
pkg.genHeader(buffer, []string{"context", "reflect"}, importsAndAliases)
|
2020-08-10 19:21:50 +02:00
|
|
|
|
2021-01-20 20:28:50 +01:00
|
|
|
if err := pkg.genResource(buffer, r, goPkgInfo.GenerateResourceContainerTypes); err != nil {
|
2020-08-10 19:21:50 +02:00
|
|
|
return nil, errors.Wrapf(err, "generating resource %s", mod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pkg.types) > 0 {
|
|
|
|
for _, t := range pkg.types {
|
|
|
|
pkg.genType(buffer, t)
|
|
|
|
}
|
|
|
|
pkg.genTypeRegistrations(buffer, pkg.types)
|
|
|
|
}
|
|
|
|
|
|
|
|
buffers[mod] = buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffers, nil
|
|
|
|
}
|