Skip to content

Commit

Permalink
make stuff compile
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Jun 7, 2024
1 parent aa16c47 commit 25e6a46
Show file tree
Hide file tree
Showing 57 changed files with 178 additions and 165 deletions.
3 changes: 1 addition & 2 deletions cmd/cli/inspect/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/grafana/cog/internal/ast"
"github.com/grafana/cog/internal/codegen"
"github.com/grafana/cog/internal/jennies/common"
"github.com/grafana/cog/internal/languages"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -66,7 +65,7 @@ func inspectBuilderIR(schemas []*ast.Schema) error {
var err error
generator := &ast.BuilderGenerator{}

codegenCtx := common.Context{
codegenCtx := languages.Context{
Schemas: schemas,
Builders: generator.FromAST(schemas),
}
Expand Down
5 changes: 2 additions & 3 deletions internal/codegen/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/grafana/cog/internal/ast"
"github.com/grafana/cog/internal/ast/compiler"
"github.com/grafana/cog/internal/jennies/common"
"github.com/grafana/cog/internal/jennies/golang"
"github.com/grafana/cog/internal/jennies/java"
"github.com/grafana/cog/internal/jennies/jsonschema"
Expand Down Expand Up @@ -121,8 +120,8 @@ func (pipeline *Pipeline) interpolate(input string) string {
return interpolated
}

func (pipeline *Pipeline) jenniesConfig() common.Config {
return common.Config{
func (pipeline *Pipeline) jenniesConfig() languages.Config {
return languages.Config{
Debug: pipeline.Debug,
Types: pipeline.Output.Types,
Builders: pipeline.Output.Builders,
Expand Down
18 changes: 9 additions & 9 deletions internal/codegen/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (pipeline *Pipeline) Run(ctx context.Context) (*codejen.FS, error) {
if err != nil {
return nil, err
}
jennyInput := common.BuildOptions{
jennyInput := languages.BuildOptions{
Languages: targetsByLanguage.AsLanguageRefs(),
}

Expand All @@ -87,17 +87,17 @@ func (pipeline *Pipeline) Run(ctx context.Context) (*codejen.FS, error) {
return generatedFS, nil
}

func (pipeline *Pipeline) jenniesInputForLanguage(language languages.Language, schemas ast.Schemas, commonCompilerPasses compiler.Passes, veneers *rewrite.Rewriter) (common.Context, error) {
func (pipeline *Pipeline) jenniesInputForLanguage(language languages.Language, schemas ast.Schemas, commonCompilerPasses compiler.Passes, veneers *rewrite.Rewriter) (languages.Context, error) {
var err error
jenniesInput := common.Context{
jenniesInput := languages.Context{
Schemas: schemas,
}

// apply common and language-specific compiler passes
compilerPasses := commonCompilerPasses.Concat(language.CompilerPasses())
jenniesInput.Schemas, err = compilerPasses.Process(jenniesInput.Schemas)
if err != nil {
return common.Context{}, err
return languages.Context{}, err
}

if !pipeline.Output.Builders {
Expand All @@ -111,33 +111,33 @@ func (pipeline *Pipeline) jenniesInputForLanguage(language languages.Language, s
// apply the builder veneers
builders, err = veneers.ApplyTo(jenniesInput.Schemas, builders, language.Name())

Check failure on line 112 in internal/codegen/run.go

View workflow job for this annotation

GitHub Actions / Linters

ineffectual assignment to builders (ineffassign)
if err != nil {
return common.Context{}, err
return languages.Context{}, err
}

// ensure identifiers are properly formatted
jenniesInput, err = pipeline.formatIdentifiers(language, jenniesInput)
if err != nil {
return common.Context{}, err
return languages.Context{}, err
}

// if the language defines an identifier formatter, let's apply it.
if formatterProvider, ok := language.(languages.IdentifiersFormatterProvider); ok {
jenniesInput, err = languages.FormatIdentifiers(formatterProvider, jenniesInput)
if err != nil {
return common.Context{}, err
return languages.Context{}, err
}
}

// with the veneers applied, generate "nil-checks" for assignments
jenniesInput, err = languages.GenerateBuilderNilChecks(language, jenniesInput)
if err != nil {
return common.Context{}, err
return languages.Context{}, err
}

return jenniesInput, nil
}

func (pipeline *Pipeline) formatIdentifiers(language languages.Language, jenniesInput common.Context) (common.Context, error) {
func (pipeline *Pipeline) formatIdentifiers(language languages.Language, jenniesInput languages.Context) (languages.Context, error) {
var err error

// if the language defines an identifier formatter, let's apply it.
Expand Down
9 changes: 5 additions & 4 deletions internal/codegen/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/grafana/codejen"
"github.com/grafana/cog/internal/jennies/common"
"github.com/grafana/cog/internal/languages"
"github.com/grafana/cog/internal/tools"
)

Expand Down Expand Up @@ -57,13 +58,13 @@ func loadURL(ctx context.Context, url string) (io.ReadCloser, error) {
return resp.Body, nil
}

func repositoryTemplatesJenny(pipeline *Pipeline) (*codejen.JennyList[common.BuildOptions], error) {
func repositoryTemplatesJenny(pipeline *Pipeline) (*codejen.JennyList[languages.BuildOptions], error) {
outputDir, err := pipeline.outputDir(pipeline.currentDirectory)
if err != nil {
return nil, err
}

repoTemplatesJenny := codejen.JennyListWithNamer[common.BuildOptions](func(_ common.BuildOptions) string {
repoTemplatesJenny := codejen.JennyListWithNamer[languages.BuildOptions](func(_ languages.BuildOptions) string {
return "RepositoryTemplates"
})
repoTemplatesJenny.AppendOneToMany(&common.RepositoryTemplate{
Expand All @@ -78,13 +79,13 @@ func repositoryTemplatesJenny(pipeline *Pipeline) (*codejen.JennyList[common.Bui
return repoTemplatesJenny, nil
}

func packageTemplatesJenny(pipeline *Pipeline, language string) (*codejen.JennyList[common.Context], error) {
func packageTemplatesJenny(pipeline *Pipeline, language string) (*codejen.JennyList[languages.Context], error) {
outputDir, err := pipeline.languageOutputDir(pipeline.currentDirectory, language)
if err != nil {
return nil, err
}

pkgTemplatesJenny := codejen.JennyListWithNamer[common.Context](func(_ common.Context) string {
pkgTemplatesJenny := codejen.JennyListWithNamer[languages.Context](func(_ languages.Context) string {
return "PackageTemplates" + tools.UpperCamelCase(language)
})
pkgTemplatesJenny.AppendOneToMany(&common.PackageTemplate{
Expand Down
3 changes: 2 additions & 1 deletion internal/jennies/common/codejen.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"text/template"

"github.com/grafana/codejen"
"github.com/grafana/cog/internal/languages"
)

type noopOneToManyJenny[Input any] struct {
Expand All @@ -31,7 +32,7 @@ func If[Input any](condition bool, innerJenny codejen.OneToMany[Input]) codejen.
// GeneratedCommentHeader produces a FileMapper that injects a comment header onto
// a [codejen.File] indicating the jenny or jennies that constructed the
// file.
func GeneratedCommentHeader(config Config) codejen.FileMapper {
func GeneratedCommentHeader(config languages.Config) codejen.FileMapper {
genHeader := `{{ .Leader }} Code generated - EDITING IS FUTILE. DO NOT EDIT.
{{- with .Using }}
{{ $.Leader }}
Expand Down
3 changes: 2 additions & 1 deletion internal/jennies/common/codejen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/grafana/codejen"
"github.com/grafana/cog/internal/languages"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -126,7 +127,7 @@ With some content`
t.Run(tc.name, func(t *testing.T) {
req := require.New(t)

resultFile, err := GeneratedCommentHeader(Config{Debug: tc.debug})(*tc.inputFile)
resultFile, err := GeneratedCommentHeader(languages.Config{Debug: tc.debug})(*tc.inputFile)
req.NoError(err)

req.Equal(tc.expectedContent, string(resultFile.Data))
Expand Down
5 changes: 3 additions & 2 deletions internal/jennies/common/packagetemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/grafana/codejen"
cogtemplate "github.com/grafana/cog/internal/jennies/template"
"github.com/grafana/cog/internal/languages"
)

type PackageTemplate struct {
Expand All @@ -25,7 +26,7 @@ func (jenny PackageTemplate) JennyName() string {
return fmt.Sprintf("PackageTemplate[%s]", jenny.Language)
}

func (jenny PackageTemplate) Generate(context Context) (codejen.Files, error) {
func (jenny PackageTemplate) Generate(context languages.Context) (codejen.Files, error) {
var files codejen.Files

templateRoot := filepath.Join(jenny.TemplateDir, jenny.Language)
Expand Down Expand Up @@ -75,7 +76,7 @@ func (jenny PackageTemplate) Generate(context Context) (codejen.Files, error) {
return files, nil
}

func (jenny PackageTemplate) templateData(context Context) map[string]any {
func (jenny PackageTemplate) templateData(context languages.Context) map[string]any {
packages := make([]string, 0, len(context.Schemas))
for _, schema := range context.Schemas {
packages = append(packages, schema.Package)
Expand Down
3 changes: 2 additions & 1 deletion internal/jennies/common/repotemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/grafana/codejen"
cogtemplate "github.com/grafana/cog/internal/jennies/template"
"github.com/grafana/cog/internal/languages"
)

type RepositoryTemplate struct {
Expand All @@ -22,7 +23,7 @@ func (jenny RepositoryTemplate) JennyName() string {
return "RepositoryTemplate"
}

func (jenny RepositoryTemplate) Generate(buildOpts BuildOptions) (codejen.Files, error) {
func (jenny RepositoryTemplate) Generate(buildOpts languages.BuildOptions) (codejen.Files, error) {
var files codejen.Files

for _, language := range buildOpts.Languages {
Expand Down
10 changes: 5 additions & 5 deletions internal/jennies/golang/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/grafana/codejen"
"github.com/grafana/cog/internal/ast"
"github.com/grafana/cog/internal/jennies/common"
"github.com/grafana/cog/internal/jennies/template"
"github.com/grafana/cog/internal/languages"
"github.com/grafana/cog/internal/orderedmap"
"github.com/grafana/cog/internal/tools"
)
Expand All @@ -24,7 +24,7 @@ func (jenny *Builder) JennyName() string {
return "GoBuilder"
}

func (jenny *Builder) Generate(context common.Context) (codejen.Files, error) {
func (jenny *Builder) Generate(context languages.Context) (codejen.Files, error) {
files := codejen.Files{}

for _, builder := range context.Builders {
Expand All @@ -44,7 +44,7 @@ func (jenny *Builder) Generate(context common.Context) (codejen.Files, error) {
return files, nil
}

func (jenny *Builder) generateBuilder(context common.Context, builder ast.Builder) ([]byte, error) {
func (jenny *Builder) generateBuilder(context languages.Context, builder ast.Builder) ([]byte, error) {
var buffer strings.Builder

imports := NewImportMap()
Expand Down Expand Up @@ -112,7 +112,7 @@ func (jenny *Builder) generateBuilder(context common.Context, builder ast.Builde
return []byte(buffer.String()), nil
}

func (jenny *Builder) genDefaultOptionsCalls(context common.Context, builder ast.Builder) []template.OptionCall {
func (jenny *Builder) genDefaultOptionsCalls(context languages.Context, builder ast.Builder) []template.OptionCall {
calls := make([]template.OptionCall, 0)
for _, opt := range builder.Options {
if opt.Default == nil {
Expand Down Expand Up @@ -150,7 +150,7 @@ func hasTypedDefaults(opt ast.Option) bool {
return false
}

func (jenny *Builder) formatDefaultTypedArgs(context common.Context, opt ast.Option) []string {
func (jenny *Builder) formatDefaultTypedArgs(context languages.Context, opt ast.Option) []string {
args := make([]string, 0)
for i, arg := range opt.Default.ArgsValues {
val, _ := arg.(map[string]interface{})
Expand Down
5 changes: 2 additions & 3 deletions internal/jennies/golang/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package golang
import (
"testing"

"github.com/grafana/cog/internal/jennies/common"
"github.com/grafana/cog/internal/languages"
"github.com/grafana/cog/internal/testutils"
"github.com/stretchr/testify/require"
)

func TestBuilder_Generate(t *testing.T) {
test := testutils.GoldenFilesTestSuite[common.Context]{
test := testutils.GoldenFilesTestSuite[languages.Context]{
TestDataRoot: "../../../testdata/jennies/builders",
Name: "GoBuilder",
Skip: map[string]string{
Expand All @@ -24,7 +23,7 @@ func TestBuilder_Generate(t *testing.T) {
language := New(config)
jenny := Builder{Config: config}

test.Run(t, func(tc *testutils.Test[common.Context]) {
test.Run(t, func(tc *testutils.Test[languages.Context]) {
var err error
req := require.New(tc)

Expand Down
4 changes: 2 additions & 2 deletions internal/jennies/golang/gomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/grafana/codejen"
"github.com/grafana/cog/internal/jennies/common"
"github.com/grafana/cog/internal/languages"
)

type GoMod struct {
Expand All @@ -15,7 +15,7 @@ func (jenny GoMod) JennyName() string {
return "GoMod"
}

func (jenny GoMod) Generate(_ common.Context) (codejen.Files, error) {
func (jenny GoMod) Generate(_ languages.Context) (codejen.Files, error) {
return codejen.Files{
*codejen.NewFile("go.mod", []byte(jenny.generateGoMod()), jenny),
}, nil
Expand Down
4 changes: 2 additions & 2 deletions internal/jennies/golang/gomod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package golang
import (
"testing"

"github.com/grafana/cog/internal/jennies/common"
"github.com/grafana/cog/internal/languages"
"github.com/stretchr/testify/require"
)

Expand All @@ -14,7 +14,7 @@ func TestGoMod_Generate(t *testing.T) {
Config: Config{PackageRoot: "github.com/grafana/heey"},
}

files, err := jenny.Generate(common.Context{})
files, err := jenny.Generate(languages.Context{})
req.NoError(err)

req.Len(files, 1)
Expand Down
16 changes: 8 additions & 8 deletions internal/jennies/golang/jennies.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (config *Config) InterpolateParameters(interpolator func(input string) stri
config.PackageRoot = interpolator(config.PackageRoot)
}

func (config Config) MergeWithGlobal(global common.Config) Config {
func (config Config) MergeWithGlobal(global languages.Config) Config {
newConfig := config
newConfig.debug = global.Debug
newConfig.generateBuilders = global.Builders
Expand All @@ -63,22 +63,22 @@ func (language *Language) Name() string {
return LanguageRef
}

func (language *Language) Jennies(globalConfig common.Config) *codejen.JennyList[common.Context] {
func (language *Language) Jennies(globalConfig languages.Config) *codejen.JennyList[languages.Context] {
config := language.config.MergeWithGlobal(globalConfig)
identifiersFormatter := language.IdentifiersFormatter()

jenny := codejen.JennyListWithNamer[common.Context](func(_ common.Context) string {
jenny := codejen.JennyListWithNamer[languages.Context](func(_ languages.Context) string {
return LanguageRef
})
jenny.AppendOneToMany(
common.If[common.Context](!config.SkipRuntime, Runtime{Config: config}),
common.If[common.Context](!config.SkipRuntime, VariantsPlugins{Config: config}),
common.If[languages.Context](!config.SkipRuntime, Runtime{Config: config}),
common.If[languages.Context](!config.SkipRuntime, VariantsPlugins{Config: config}),

common.If[common.Context](config.GenerateGoMod, GoMod{Config: config}),
common.If[languages.Context](config.GenerateGoMod, GoMod{Config: config}),

common.If[common.Context](globalConfig.Types, RawTypes{Config: config, IdentifiersFormatter: identifiersFormatter}),
common.If[languages.Context](globalConfig.Types, RawTypes{Config: config, IdentifiersFormatter: identifiersFormatter}),

common.If[common.Context](!config.SkipRuntime && globalConfig.Builders, &Builder{Config: config}),
common.If[languages.Context](!config.SkipRuntime && globalConfig.Builders, &Builder{Config: config}),
)
jenny.AddPostprocessors(PostProcessFile, common.GeneratedCommentHeader(globalConfig))

Expand Down
Loading

0 comments on commit 25e6a46

Please sign in to comment.