diff --git a/go.mod b/go.mod index 9edbe559b78..1e5d862639d 100644 --- a/go.mod +++ b/go.mod @@ -48,7 +48,7 @@ require ( github.com/go-sql-driver/mysql v1.6.0 github.com/go-stack/stack v1.8.1 github.com/gobwas/glob v0.2.3 - github.com/gofrs/uuid v4.3.0+incompatible + github.com/gofrs/uuid v4.3.0+incompatible // indirect github.com/gogo/protobuf v1.3.2 github.com/golang/mock v1.6.0 github.com/golang/snappy v0.0.4 @@ -253,7 +253,7 @@ require ( github.com/getkin/kin-openapi v0.103.0 github.com/golang-migrate/migrate/v4 v4.7.0 github.com/google/go-github/v45 v45.2.0 - github.com/grafana/codejen v0.0.2 + github.com/grafana/codejen v0.0.3 github.com/grafana/dskit v0.0.0-20211011144203-3a88ec0b675f github.com/jmoiron/sqlx v1.3.5 github.com/kr/pretty v0.3.0 diff --git a/go.sum b/go.sum index 9abdd47c2e5..88e4e481756 100644 --- a/go.sum +++ b/go.sum @@ -1348,8 +1348,8 @@ github.com/gosimple/slug v1.12.0 h1:xzuhj7G7cGtd34NXnW/yF0l+AGNfWqwgh/IXgFy7dnc= github.com/gosimple/slug v1.12.0/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ= github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o= github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc= -github.com/grafana/codejen v0.0.2 h1:Ssp27X7SOnYxaPUTByW/6201tNV5Q60l1BSF+s3lRP8= -github.com/grafana/codejen v0.0.2/go.mod h1:zmwwM/DRyQB7pfuBjTWII3CWtxcXh8LTwAYGfDfpR6s= +github.com/grafana/codejen v0.0.3 h1:tAWxoTUuhgmEqxJPOLtJoxlPBbMULFwKFOcRsPRPXDw= +github.com/grafana/codejen v0.0.3/go.mod h1:zmwwM/DRyQB7pfuBjTWII3CWtxcXh8LTwAYGfDfpR6s= github.com/grafana/cuetsy v0.1.1 h1:+1jaDDYCpvKlcOWJgBRbkc5+VZIClCEn5mbI+4PLZqM= github.com/grafana/cuetsy v0.1.1/go.mod h1:4KWkUOslwvRTpEv7wdQG0jDFTuJmU+0L9x0h4kWxa2A= github.com/grafana/dskit v0.0.0-20211011144203-3a88ec0b675f h1:FvvSVEbnGeM2bUivGmsiXTi8URJyBU7TcFEEoRe5wWI= @@ -1369,8 +1369,6 @@ github.com/grafana/prometheus-alertmanager v0.24.1-0.20221012142027-823cd9150293 github.com/grafana/prometheus-alertmanager v0.24.1-0.20221012142027-823cd9150293/go.mod h1:HVHqK+BVPa/tmL8EMhLCCrPt2a1GdJpEyxr5hgur2UI= github.com/grafana/saml v0.4.9-0.20220727151557-61cd9c9353fc h1:1PY8n+rXuBNr3r1JQhoytWDCpc+pq+BibxV0SZv+Cr4= github.com/grafana/saml v0.4.9-0.20220727151557-61cd9c9353fc/go.mod h1:9Zh6dWPtB3MSzTRt8fIFH60Z351QQ+s7hCU3J/tTlA4= -github.com/grafana/thema v0.0.0-20221113034006-50fd3c0da5ce h1:N1K0WWaG0B5i/703ri0WSazQYVsCYj1mgODgElCz0o8= -github.com/grafana/thema v0.0.0-20221113034006-50fd3c0da5ce/go.mod h1:ZJHKwNE86ngdQ7edJIFHepCiIg9YP9x+YZPEm3dlkL4= github.com/grafana/thema v0.0.0-20221113112305-b441ed85a1fd h1:y6H9I5fy4sRKf2FJ7W94YWero4mXH50Ft8NAPZ9DapQ= github.com/grafana/thema v0.0.0-20221113112305-b441ed85a1fd/go.mod h1:ZJHKwNE86ngdQ7edJIFHepCiIg9YP9x+YZPEm3dlkL4= github.com/grafana/xorm v0.8.3-0.20220614223926-2fcda7565af6 h1:I9dh1MXGX0wGyxdV/Sl7+ugnki4Dfsy8lv2s5Yf887o= diff --git a/kinds/gen.go b/kinds/gen.go index 9b58b05e79e..41f0d527049 100644 --- a/kinds/gen.go +++ b/kinds/gen.go @@ -104,7 +104,7 @@ func main() { return nameFor(all[i].Meta) < nameFor(all[j].Meta) }) - jfs, err := coreKindsGen.GenerateFS(all) + jfs, err := coreKindsGen.GenerateFS(all...) if err != nil { die(fmt.Errorf("core kinddirs codegen failed: %w", err)) } diff --git a/pkg/codegen/generators.go b/pkg/codegen/generators.go index 51d9075557d..a7b7aed83f8 100644 --- a/pkg/codegen/generators.go +++ b/pkg/codegen/generators.go @@ -34,12 +34,20 @@ func ForGen(rt *thema.Runtime, decl *kindsys.SomeDecl) (*DeclForGen, error) { type DeclForGen struct { *kindsys.SomeDecl lin thema.Lineage + sch thema.Lineage } +// Lineage returns the [thema.Lineage] for the underlying [kindsys.SomeDecl]. func (decl *DeclForGen) Lineage() thema.Lineage { return decl.lin } +// Schema returns the [thema.Schema] that a jenny should operate against, for those +// jennies that target a single schema. +func (decl *DeclForGen) Schema() thema.Lineage { + return decl.sch +} + // SlashHeaderMapper produces a FileMapper that injects a comment header onto // a [codejen.File] indicating the main generator that produced it (via the provided // maingen, which should be a path) and the jenny or jennies that constructed the diff --git a/pkg/codegen/jenny_basecorereg.go b/pkg/codegen/jenny_basecorereg.go index 96298b61cef..acbb6a50a85 100644 --- a/pkg/codegen/jenny_basecorereg.go +++ b/pkg/codegen/jenny_basecorereg.go @@ -31,7 +31,7 @@ func (gen *genBaseRegistry) JennyName() string { return "BaseCoreRegistryJenny" } -func (gen *genBaseRegistry) Generate(decls []*DeclForGen) (*codejen.File, error) { +func (gen *genBaseRegistry) Generate(decls ...*DeclForGen) (*codejen.File, error) { var numRaw int for _, k := range decls { if k.IsRaw() { diff --git a/pkg/codegen/jenny_tsveneerindex.go b/pkg/codegen/jenny_tsveneerindex.go index 0bb82482e5f..6052cc94bf5 100644 --- a/pkg/codegen/jenny_tsveneerindex.go +++ b/pkg/codegen/jenny_tsveneerindex.go @@ -39,7 +39,7 @@ func (gen *genTSVeneerIndex) JennyName() string { return "TSVeneerIndexJenny" } -func (gen *genTSVeneerIndex) Generate(decls []*DeclForGen) (*codejen.File, error) { +func (gen *genTSVeneerIndex) Generate(decls ...*DeclForGen) (*codejen.File, error) { tsf := new(ast.File) for _, decl := range decls { if decl.IsRaw() { diff --git a/pkg/kindsys/EXTENDING.md b/pkg/kindsys/EXTENDING.md index 93f36639f45..9aa7c125460 100644 --- a/pkg/kindsys/EXTENDING.md +++ b/pkg/kindsys/EXTENDING.md @@ -10,7 +10,7 @@ This document is the guide to extending kindsys. But first, we have to identify * **CUE framework** - the collection of .cue files in this directory, `pkg/kindsys`. These are schemas that define how Kinds are declared. * **Go framework** - the Go package in this directory containing utilities for loading individual kind declarations, validating them against the CUE framework, and representing them consistently in Go. -* **Code generators** - `pkg/codegen` contains the codegen framework. Individual generators (which take one or many `pkg/kindsys.Decl`, and produce a single file) each have a `pkg/codegen/generator_*.go` file. +* **Code generators** - written using the `github.com/grafana/codejen` framework, which applies the [single responsibility principle](https://en.wikipedia.org/wiki/Single-responsibility_principle) to code generation, allowing us to compose modular code generators. Each jenny - a modular generator with a single responsibility - is declared as a `pkg/codegen/jenny_*.go` file. * **Registries** - generated lists of all or a well-defined subset of kinds that can be used in code. `pkg/registries/corekind` is a registry of all core `pkg/kindsys.Interface` implementations; `packages/grafana-schema/src/index.gen.ts` is a registry of all the TypeScript types generated from the current versions of each kind's schema. * **Kind declarations** - the declarations of individual kinds. By kind category: * **Core Structured** - each child directory of `kinds/structured`. diff --git a/pkg/plugins/plugindef/gen.go b/pkg/plugins/plugindef/gen.go index 07354ec2c46..adaea3b315b 100644 --- a/pkg/plugins/plugindef/gen.go +++ b/pkg/plugins/plugindef/gen.go @@ -44,7 +44,7 @@ func main() { grootp := strings.Split(cwd, string(os.PathSeparator)) groot := filepath.Join(string(os.PathSeparator), filepath.Join(grootp[:len(grootp)-3]...)) - jfs := elsedie(jl.GenerateFS([]thema.Lineage{lin}))("plugindef jenny pipeline failed") + jfs := elsedie(jl.GenerateFS(lin))("plugindef jenny pipeline failed") if _, set := os.LookupEnv("CODEGEN_VERIFY"); set { if err := jfs.Verify(context.Background(), groot); err != nil { die(fmt.Errorf("generated code is out of sync with inputs:\n%s\nrun `make gen-cue` to regenerate", err))