Skip to content

Commit

Permalink
Remove generator registry
Browse files Browse the repository at this point in the history
Signed-off-by: Chance Zibolski <[email protected]>
  • Loading branch information
chancez committed Sep 6, 2024
1 parent f076b1e commit 75a6fa7
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 149 deletions.
46 changes: 46 additions & 0 deletions pkg/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,51 @@ func Parse(data []byte) (Config, error) {
}
stagePositions[stage.Name] = pos
}

return cfg, nil
}

func validateGenerators(generatorCfg Generator) error {

Check failure on line 47 in pkg/config/parse.go

View workflow job for this annotation

GitHub Actions / unit-test

func `validateGenerators` is unused (unused)
count := 0
switch {
case generatorCfg.File != nil:
count++
fallthrough
case generatorCfg.Exec != nil:
count++
fallthrough
case generatorCfg.Helm != nil:
count++
fallthrough
case generatorCfg.Kustomize != nil:
count++
fallthrough
case generatorCfg.Merge != nil:
count++
fallthrough
case generatorCfg.GoTemplate != nil:
count++
fallthrough
case generatorCfg.Import != nil:
count++
fallthrough
case generatorCfg.JQ != nil:
count++
fallthrough
case generatorCfg.CELFilter != nil:
count++
fallthrough
case generatorCfg.YAML != nil:
count++
fallthrough
case generatorCfg.JSON != nil:
count++
}
if count == 0 {
return fmt.Errorf("generator not configured")
}
if count > 0 {
return fmt.Errorf("invalid configuration, cannot specify multiple generators in the same generator config")
}
return nil
}
6 changes: 0 additions & 6 deletions pkg/generator/cel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ var (
goBoolType = reflect.TypeOf(false)
)

func init() {
Register("celfilter", config.CELFilterGenerator{}, func(dir string, cfg any, refStore *reference.Store) Generator {
return NewCELFilter(dir, cfg.(config.CELFilterGenerator), refStore)
})
}

type CELFilter struct {
dir string
cfg config.CELFilterGenerator
Expand Down
7 changes: 0 additions & 7 deletions pkg/generator/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@ import (
"os/exec"

"github.com/chancez/yamlforge/pkg/config"
"github.com/chancez/yamlforge/pkg/reference"
)

var _ Generator = (*Exec)(nil)

func init() {
Register("exec", config.ExecGenerator{}, func(dir string, cfg any, refStore *reference.Store) Generator {
return NewExec(dir, cfg.(config.ExecGenerator))
})
}

type Exec struct {
dir string
cfg config.ExecGenerator
Expand Down
7 changes: 0 additions & 7 deletions pkg/generator/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@ import (
"path"

"github.com/chancez/yamlforge/pkg/config"
"github.com/chancez/yamlforge/pkg/reference"
)

var _ Generator = (*File)(nil)

func init() {
Register("file", config.FileGenerator{}, func(dir string, cfg any, refStore *reference.Store) Generator {
return NewFile(dir, cfg.(config.FileGenerator))
})
}

type File struct {
// Path lookups are relative to the dir specified.
dir string
Expand Down
74 changes: 0 additions & 74 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,8 @@ package generator

import (
"context"
"fmt"
"reflect"

"github.com/chancez/yamlforge/pkg/config"
"github.com/chancez/yamlforge/pkg/reference"
)

type Generator interface {
Generate(context.Context) ([]byte, error)
}

type Registry struct {
typeToFactory map[reflect.Type]GeneratorFactory
}

type NewGeneratorFunc func(dir string, cfg any, refStore *reference.Store) Generator

type GeneratorFactory struct {
name string
new NewGeneratorFunc
}

func NewRegistry() *Registry {
return &Registry{typeToFactory: make(map[reflect.Type]GeneratorFactory)}
}

func (reg *Registry) GetGenerator(dir string, refStore *reference.Store, generatorCfg config.Generator) (string, Generator, error) {
v := reflect.ValueOf(generatorCfg)

setFields := 0
var cfg any
for i := 0; i < v.NumField(); i++ {
fieldName := v.Type().Field(i).Name
if fieldName == "Name" {
continue
}
field := v.Field(i)
if field.Kind() != reflect.Pointer {
panic(fmt.Sprintf("expected config.Generator field %q to be a pointer, got: %q", fieldName, field.Kind().String()))
}
if !field.IsNil() {
setFields++
elem := field.Elem()
cfg = elem.Interface()
}
}
if setFields == 0 {
return "", nil, fmt.Errorf("generator not configured")
}
if setFields > 1 {
return "", nil, fmt.Errorf("multiple generators configured")
}

cfgType := reflect.TypeOf(cfg)
factory, exists := reg.typeToFactory[cfgType]
if !exists {
panic(fmt.Sprintf("cannot find factory for %s", cfgType))
}

return factory.name, factory.new(dir, cfg, refStore), nil
}

func (reg *Registry) Register(name string, cfgType any, newGenerator NewGeneratorFunc) {
ty := reflect.TypeOf(cfgType)
if _, exists := reg.typeToFactory[ty]; exists {
panic(fmt.Sprintf("duplicate generator registered for %q", ty.String()))
}
factory := GeneratorFactory{
name: name,
new: newGenerator,
}
reg.typeToFactory[ty] = factory
}

var GlobalRegistry = NewRegistry()

func Register(name string, cfgType any, newGenerator NewGeneratorFunc) {
GlobalRegistry.Register(name, cfgType, newGenerator)
}
6 changes: 0 additions & 6 deletions pkg/generator/gotemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import (

var _ Generator = (*GoTemplate)(nil)

func init() {
Register("gotemplate", config.GoTemplateGenerator{}, func(dir string, cfg any, refStore *reference.Store) Generator {
return NewGoTemplate(dir, cfg.(config.GoTemplateGenerator), refStore)
})
}

type GoTemplate struct {
dir string
cfg config.GoTemplateGenerator
Expand Down
6 changes: 0 additions & 6 deletions pkg/generator/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import (

var _ Generator = (*Helm)(nil)

func init() {
Register("helm", config.HelmGenerator{}, func(dir string, cfg any, refStore *reference.Store) Generator {
return NewHelm(dir, cfg.(config.HelmGenerator), refStore)
})
}

type Helm struct {
dir string
cfg config.HelmGenerator
Expand Down
6 changes: 0 additions & 6 deletions pkg/generator/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ import (

var _ Generator = (*Import)(nil)

func init() {
Register("import", config.ImportGenerator{}, func(dir string, cfg any, refStore *reference.Store) Generator {
return NewImport(dir, cfg.(config.ImportGenerator), refStore)
})
}

type Import struct {
// Path lookups are relative to the dir specified.
dir string
Expand Down
6 changes: 0 additions & 6 deletions pkg/generator/jq.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import (

var _ Generator = (*JQ)(nil)

func init() {
Register("jq", config.JQGenerator{}, func(dir string, cfg any, refStore *reference.Store) Generator {
return NewJQ(dir, cfg.(config.JQGenerator), refStore)
})
}

type JQ struct {
dir string
cfg config.JQGenerator
Expand Down
6 changes: 0 additions & 6 deletions pkg/generator/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ import (

var _ Generator = (*JSON)(nil)

func init() {
Register("json", config.JSONGenerator{}, func(dir string, cfg any, refStore *reference.Store) Generator {
return NewJSON(dir, cfg.(config.JSONGenerator), refStore)
})
}

type JSON struct {
dir string
cfg config.JSONGenerator
Expand Down
6 changes: 0 additions & 6 deletions pkg/generator/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ import (

var _ Generator = (*Kustomize)(nil)

func init() {
Register("kustomize", config.KustomizeGenerator{}, func(dir string, cfg any, refStore *reference.Store) Generator {
return NewKustomize(dir, cfg.(config.KustomizeGenerator), refStore)
})
}

type Kustomize struct {
dir string
cfg config.KustomizeGenerator
Expand Down
6 changes: 0 additions & 6 deletions pkg/generator/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ import (

var _ Generator = (*Merge)(nil)

func init() {
Register("merge", config.MergeGenerator{}, func(dir string, cfg any, refStore *reference.Store) Generator {
return NewMerge(dir, cfg.(config.MergeGenerator), refStore)
})
}

type Merge struct {
dir string
cfg config.MergeGenerator
Expand Down
53 changes: 46 additions & 7 deletions pkg/generator/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ import (

var _ Generator = (*Pipeline)(nil)

func init() {
Register("pipeline", config.PipelineGenerator{}, func(dir string, cfg any, refStore *reference.Store) Generator {
return NewPipeline(dir, cfg.(config.PipelineGenerator), refStore)
})
}

type Pipeline struct {
dir string
cfg config.PipelineGenerator
Expand Down Expand Up @@ -58,7 +52,7 @@ func (pipeline *Pipeline) Generate(ctx context.Context) ([]byte, error) {
}

func (pipeline *Pipeline) executeGenerator(ctx context.Context, generatorCfg config.Generator) ([]byte, error) {
name, gen, err := GlobalRegistry.GetGenerator(pipeline.dir, pipeline.refStore, generatorCfg)
name, gen, err := pipeline.getGenerator(generatorCfg)
if err != nil {
return nil, fmt.Errorf("error getting generator: %w", err)
}
Expand All @@ -68,3 +62,48 @@ func (pipeline *Pipeline) executeGenerator(ctx context.Context, generatorCfg con
}
return result, nil
}

func (pipeline *Pipeline) getGenerator(generatorCfg config.Generator) (string, Generator, error) {
var (
name string
gen Generator
)
switch {
case generatorCfg.File != nil:
name = "file"
gen = NewFile(pipeline.dir, *generatorCfg.File)
case generatorCfg.Exec != nil:
name = "exec"
gen = NewExec(pipeline.dir, *generatorCfg.Exec)
case generatorCfg.Helm != nil:
name = "helm"
gen = NewHelm(pipeline.dir, *generatorCfg.Helm, pipeline.refStore)
case generatorCfg.Kustomize != nil:
name = "kustomize"
gen = NewKustomize(pipeline.dir, *generatorCfg.Kustomize, pipeline.refStore)
case generatorCfg.Merge != nil:
name = "merge"
gen = NewMerge(pipeline.dir, *generatorCfg.Merge, pipeline.refStore)
case generatorCfg.GoTemplate != nil:
name = "gotemplate"
gen = NewGoTemplate(pipeline.dir, *generatorCfg.GoTemplate, pipeline.refStore)
case generatorCfg.Import != nil:
name = "import"
gen = NewImport(pipeline.dir, *generatorCfg.Import, pipeline.refStore)
case generatorCfg.JQ != nil:
name = "jq"
gen = NewJQ(pipeline.dir, *generatorCfg.JQ, pipeline.refStore)
case generatorCfg.CELFilter != nil:
name = "celfilter"
gen = NewCELFilter(pipeline.dir, *generatorCfg.CELFilter, pipeline.refStore)
case generatorCfg.YAML != nil:
name = "yaml"
gen = NewYAML(pipeline.dir, *generatorCfg.YAML, pipeline.refStore)
case generatorCfg.JSON != nil:
name = "json"
gen = NewJSON(pipeline.dir, *generatorCfg.JSON, pipeline.refStore)
default:
return "", nil, fmt.Errorf("generator not configured")
}
return name, gen, nil
}
6 changes: 0 additions & 6 deletions pkg/generator/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ import (

var _ Generator = (*YAML)(nil)

func init() {
Register("yaml", config.YAMLGenerator{}, func(dir string, cfg any, refStore *reference.Store) Generator {
return NewYAML(dir, cfg.(config.YAMLGenerator), refStore)
})
}

type YAML struct {
dir string
cfg config.YAMLGenerator
Expand Down

0 comments on commit 75a6fa7

Please sign in to comment.