Skip to content

Commit

Permalink
patched insert custom type registration
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed Jul 8, 2023
1 parent aad8f81 commit 71067d1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
17 changes: 6 additions & 11 deletions internal/codegen/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,17 @@ func (t *Template) GenerateEntity(ctx context.Context, pkg string, info *plugin.
}

func (t *Template) generateRegisterType() string {
var initElements []string
registry := &customTypeRegistry{}
for _, param := range t.State {
if param.In.Kind != view.KindDataView {
continue
}
registerFragment := t.RegisterFragment(param.Schema.DataType)
initElements = append(initElements, registerFragment)
registry.register(param.Schema.DataType)
}

initCode := strings.Join(initElements, "\n")
return initCode
}

func (t *Template) RegisterFragment(dataType string) string {
registerFragment := fmt.Sprintf(registerTypeTemplate, dataType, dataType)
return registerFragment
for _, param := range t.State.FilterByKind(view.KindRequestBody) {
registry.register(param.Schema.DataType)
}
return registry.stringify()
}

func (t *Template) generateMapTypeBody() string {
Expand Down
5 changes: 4 additions & 1 deletion internal/codegen/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ func (t *Template) GenerateHandler(opts *options.Gen, info *plugin.Info) (string

handlerContent := strings.Replace(handlerTemplate, "$Package", opts.Package, 1)
handlerContent = strings.Replace(handlerContent, "$LocalVariable", localVariableDeclaration, 1)
registerTypes := t.RegisterFragment("Handler")

registry := &customTypeRegistry{}
registry.register("Handler")
registerTypes := registry.stringify()
handlerContent = strings.Replace(handlerContent, "$RegisterTypes", registerTypes, 1)
imports := NewImports()
imports.AddPackage(info.ChecksumPkg())
Expand Down
30 changes: 30 additions & 0 deletions internal/codegen/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package codegen

import (
"fmt"
"strings"
)

type customTypeRegistry struct {
Elements []string
index map[string]bool
}

func (c *customTypeRegistry) register(dataType string) {
c.ensureIndex()
if _, ok := c.index[dataType]; ok {
return
}
c.index[dataType] = true
c.Elements = append(c.Elements, fmt.Sprintf(registerTypeTemplate, dataType, dataType))
}

func (c *customTypeRegistry) stringify() string {
return strings.Join(c.Elements, "\n")
}

func (c *customTypeRegistry) ensureIndex() {
if len(c.index) == 0 {
c.index = map[string]bool{}
}
}
16 changes: 8 additions & 8 deletions internal/codegen/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,18 +218,18 @@ func (t *Template) GenerateState(pkg string, info *plugin.Info) string {
fields = append(fields, input.FieldDeclaration())
}
output = strings.Replace(output, "$Fields", strings.Join(fields, "\n\n"), 1)

registerTypes := t.RegisterFragment("State")
registerTypes := ""
importFragment := ""
imports := NewImports()
imports.AddPackage(info.ChecksumPkg())
imports.AddPackage("reflect")
imports.AddPackage(info.TypeCorePkg())
switch info.IntegrationMode {
case plugin.ModeExtension, plugin.ModeCustomTypeModule:
imports := NewImports()
imports.AddPackage(info.ChecksumPkg())
imports.AddPackage("reflect")
imports.AddPackage(info.TypeCorePkg())
importFragment = imports.PackageImports()
default:
registerTypes = ""
registry := &customTypeRegistry{}
registry.register("State")
registerTypes = registry.stringify()
}
output = strings.Replace(output, "$Imports", importFragment, 1)
output = strings.Replace(output, "$RegisterTypes", registerTypes, 1)
Expand Down

0 comments on commit 71067d1

Please sign in to comment.