From f62f3cb0e90ef530bb534103ef3943644b2cb501 Mon Sep 17 00:00:00 2001 From: sam boyer Date: Mon, 23 Jan 2023 08:56:00 -0500 Subject: [PATCH] Kindsys: Generate plugin Go types under a kinds/ dir (#61899) Kindsys: Generate plugin Go types under kinds/ --- pkg/plugins/codegen/jenny_plugingotypes.go | 32 ++++++++++++++-------- public/app/plugins/gen.go | 2 +- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/pkg/plugins/codegen/jenny_plugingotypes.go b/pkg/plugins/codegen/jenny_plugingotypes.go index f3e2257d84f..3ab9dec730a 100644 --- a/pkg/plugins/codegen/jenny_plugingotypes.go +++ b/pkg/plugins/codegen/jenny_plugingotypes.go @@ -5,20 +5,24 @@ import ( "path/filepath" "strings" + copenapi "cuelang.org/go/encoding/openapi" + "github.com/dave/dst/dstutil" "github.com/grafana/codejen" + corecodegen "github.com/grafana/grafana/pkg/codegen" "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{ - inner: inner, - root: root, + root: root, } } type pgoJenny struct { - inner codejen.OneToOne[*pfs.PluginDecl] - root string + root string } func (j *pgoJenny) JennyName() string { @@ -31,16 +35,22 @@ func (j *pgoJenny) Generate(decl *pfs.PluginDecl) (*codejen.File, error) { 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 { return nil, err } pluginfolder := filepath.Base(decl.PluginPath) - slotname := strings.ToLower(decl.SchemaInterface.Name()) filename := fmt.Sprintf("types_%s_gen.go", slotname) - f.RelativePath = filepath.Join(j.root, pluginfolder, filename) - f.From = append(f.From, j) - - return f, nil + return codejen.NewFile(filepath.Join(j.root, pluginfolder, "kinds", slotname, filename), byt, j), nil } diff --git a/public/app/plugins/gen.go b/public/app/plugins/gen.go index 6d33e4e5713..9baebddf60b 100644 --- a/public/app/plugins/gen.go +++ b/public/app/plugins/gen.go @@ -54,7 +54,7 @@ func main() { pluginKindGen.Append( codegen.PluginTreeListJenny(), - codegen.PluginGoTypesJenny("pkg/tsdb", adaptToPipeline(corecodegen.GoTypesJenny{ExpandReferences: true})), + codegen.PluginGoTypesJenny("pkg/tsdb"), codegen.PluginTSTypesJenny("public/app/plugins", adaptToPipeline(corecodegen.TSTypesJenny{})), codegen.PluginDocsJenny(toDeclForGen(corecodegen.DocsJenny( filepath.Join("docs", "sources", "developers", "kinds", "composable"),