Kindsys: Generate plugin Go types under a kinds/ dir (#61899)

Kindsys: Generate plugin Go types under kinds/
This commit is contained in:
sam boyer 2023-01-23 08:56:00 -05:00 committed by GitHub
parent 40feee0d17
commit f62f3cb0e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 12 deletions

View File

@ -5,20 +5,24 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
copenapi "cuelang.org/go/encoding/openapi"
"github.com/dave/dst/dstutil"
"github.com/grafana/codejen" "github.com/grafana/codejen"
corecodegen "github.com/grafana/grafana/pkg/codegen"
"github.com/grafana/grafana/pkg/plugins/pfs" "github.com/grafana/grafana/pkg/plugins/pfs"
"github.com/grafana/thema/encoding/gocode"
"github.com/grafana/thema/encoding/openapi"
) )
func PluginGoTypesJenny(root string, inner codejen.OneToOne[*pfs.PluginDecl]) codejen.OneToOne[*pfs.PluginDecl] { // TODO this is duplicative of other Go type jennies. Remove it in favor of a better-abstracted version in thema itself
func PluginGoTypesJenny(root string) codejen.OneToOne[*pfs.PluginDecl] {
return &pgoJenny{ return &pgoJenny{
inner: inner, root: root,
root: root,
} }
} }
type pgoJenny struct { type pgoJenny struct {
inner codejen.OneToOne[*pfs.PluginDecl] root string
root string
} }
func (j *pgoJenny) JennyName() string { func (j *pgoJenny) JennyName() string {
@ -31,16 +35,22 @@ func (j *pgoJenny) Generate(decl *pfs.PluginDecl) (*codejen.File, error) {
return nil, nil return nil, nil
} }
f, err := j.inner.Generate(decl) slotname := strings.ToLower(decl.SchemaInterface.Name())
byt, err := gocode.GenerateTypesOpenAPI(decl.Lineage.Latest(), &gocode.TypeConfigOpenAPI{
Config: &openapi.Config{
Group: decl.SchemaInterface.IsGroup(),
Config: &copenapi.Config{
ExpandReferences: true,
},
},
PackageName: slotname,
ApplyFuncs: []dstutil.ApplyFunc{corecodegen.PrefixDropper(decl.Lineage.Name()), corecodegen.DecoderCompactor()},
})
if err != nil { if err != nil {
return nil, err return nil, err
} }
pluginfolder := filepath.Base(decl.PluginPath) pluginfolder := filepath.Base(decl.PluginPath)
slotname := strings.ToLower(decl.SchemaInterface.Name())
filename := fmt.Sprintf("types_%s_gen.go", slotname) filename := fmt.Sprintf("types_%s_gen.go", slotname)
f.RelativePath = filepath.Join(j.root, pluginfolder, filename) return codejen.NewFile(filepath.Join(j.root, pluginfolder, "kinds", slotname, filename), byt, j), nil
f.From = append(f.From, j)
return f, nil
} }

View File

@ -54,7 +54,7 @@ func main() {
pluginKindGen.Append( pluginKindGen.Append(
codegen.PluginTreeListJenny(), codegen.PluginTreeListJenny(),
codegen.PluginGoTypesJenny("pkg/tsdb", adaptToPipeline(corecodegen.GoTypesJenny{ExpandReferences: true})), codegen.PluginGoTypesJenny("pkg/tsdb"),
codegen.PluginTSTypesJenny("public/app/plugins", adaptToPipeline(corecodegen.TSTypesJenny{})), codegen.PluginTSTypesJenny("public/app/plugins", adaptToPipeline(corecodegen.TSTypesJenny{})),
codegen.PluginDocsJenny(toDeclForGen(corecodegen.DocsJenny( codegen.PluginDocsJenny(toDeclForGen(corecodegen.DocsJenny(
filepath.Join("docs", "sources", "developers", "kinds", "composable"), filepath.Join("docs", "sources", "developers", "kinds", "composable"),