Skip to content

Commit

Permalink
updated gen
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed Jul 16, 2024
1 parent 6c0d432 commit f39399b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/command/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func (s *Service) generateGet(ctx context.Context, opts *options.Options) (err e
if repo := opts.Repository(); repo != nil && len(repo.SubstitutesURL) > 0 {
namedResources = append(namedResources, repo.SubstitutesURL...)
}
code := aComponent.GenerateOutputCode(true, embeds, namedResources...)
code := aComponent.GenerateOutputCode(ctx, true, embeds, namedResources...)
destURL := path.Join(moduleLocation, modulePrefix, sourceName+".go")
if err = s.fs.Upload(ctx, destURL, file.DefaultFileOsMode, strings.NewReader(code)); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion gateway/route_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ func (r *Router) handleComponentState(ctx context.Context, response http.Respons
}

func (r *Router) generateComponentContract(component *repository.Component) (int, []byte) {
result := component.GenerateOutputCode(true, map[string]string{})
result := component.GenerateOutputCode(context.Background(), true, map[string]string{})
return http.StatusOK, []byte(result)
}
4 changes: 2 additions & 2 deletions internal/translator/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (s *Service) tryToBuildNamedInputType(resource *Resource, aType state.Type,
}
fieldType := types.EnsureStruct(aStructField.Type)
if fieldType != nil {
if hasTypeDef := buildMarkertTypeDef(fieldType, aType, fieldTypeName); hasTypeDef != nil {
if hasTypeDef := buildMarkerTypeDef(fieldType, aType, fieldTypeName); hasTypeDef != nil {
typeDefs = append(typeDefs, hasTypeDef)
}
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func (s *Service) tryToBuildNamedInputType(resource *Resource, aType state.Type,
resource.AppendTypeDefinition(aTypeDef)
}

func buildMarkertTypeDef(fieldType reflect.Type, aType state.Type, holderName string) *view.TypeDefinition {
func buildMarkerTypeDef(fieldType reflect.Type, aType state.Type, holderName string) *view.TypeDefinition {
for i := 0; i < fieldType.NumField(); i++ {
field := fieldType.Field(i)
if field.Tag.Get(structology.SetMarkerTag) != "" {
Expand Down
11 changes: 10 additions & 1 deletion repository/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var contractInit string
//go:embed codegen/register.gox
var registerInit string

func (c *Component) GenerateOutputCode(withEmbed bool, embeds map[string]string, namedResources ...string) string {
func (c *Component) GenerateOutputCode(ctx context.Context, withEmbed bool, embeds map[string]string, namedResources ...string) string {
builder := strings.Builder{}
input := c.Input.Type.Type()
registry := c.TypeRegistry()
Expand Down Expand Up @@ -112,11 +112,20 @@ func (c *Component) GenerateOutputCode(withEmbed bool, embeds map[string]string,
if snippetBefore != "" {
options = append(options, xreflect.WithSnippetBefore(snippetBefore))
}

inputType := input.Type()
if inputType.Kind() == reflect.Ptr {
inputType = inputType.Elem()
}

if inputType.Name() != "" && len(c.Input.Type.Parameters) > 0 { //to allow input dql changes, rebuild input from parameters
if rawType, _ := c.Input.Type.Parameters.ReflectType(c.Input.Type.Package, registry.Lookup, state.WithSetMarker(), state.WithTypeName(inputType.Name())); rawType != nil {
inputType = rawType
}
}

if inputType.Name() != "" { //rewrite as inline sturct

inputType = types.InlineStruct(input.Type(), func(field *reflect.StructField) {
if markerTag := field.Tag.Get(structology.SetMarkerTag); markerTag != "" {
field.Type = types.InlineStruct(field.Type, nil)
Expand Down

0 comments on commit f39399b

Please sign in to comment.