Skip to content

Commit

Permalink
bugfix for @gofield + @goExtraField combination (#3078)
Browse files Browse the repository at this point in the history
Co-authored-by: Roman A. Grigorovich <ragrigorov@mts.ru>
  • Loading branch information
atzedus and Roman A. Grigorovich committed May 16, 2024
1 parent e61a720 commit e012530
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions codegen/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,22 +355,33 @@ func (c *Config) injectTypesFromSchema() error {
for _, efd := range efds {
if fn := efd.Arguments.ForName("name"); fn != nil {
extraFieldName := ""
extraField := ModelExtraField{}
if fnv, err := fn.Value.Value(nil); err == nil {
if !c.Models.Exists(schemaType.Name) {
c.Models[schemaType.Name] = TypeMapEntry{
ExtraFields: make(map[string]ModelExtraField),
}
}
extraFieldName = fnv.(string)
}

if extraFieldName == "" {
return fmt.Errorf(
"argument 'name' for directive @goExtraField (src: %s, line: %d) cannot by empty",
efd.Position.Src.Name,
efd.Position.Line,
)
}

extraField := ModelExtraField{}
if t := efd.Arguments.ForName("type"); t != nil {
if tv, err := t.Value.Value(nil); err == nil {
extraField.Type = tv.(string)
}
}

if extraField.Type == "" {
return fmt.Errorf(
"argument 'type' for directive @goExtraField (src: %s, line: %d) cannot by empty",
efd.Position.Src.Name,
efd.Position.Line,
)
}

if ot := efd.Arguments.ForName("overrideTags"); ot != nil {
if otv, err := ot.Value.Value(nil); err == nil {
extraField.OverrideTags = otv.(string)
Expand All @@ -383,6 +394,12 @@ func (c *Config) injectTypesFromSchema() error {
}
}

typeMapEntry := c.Models[schemaType.Name]
if typeMapEntry.ExtraFields == nil {
typeMapEntry.ExtraFields = make(map[string]ModelExtraField)
}

c.Models[schemaType.Name] = typeMapEntry
c.Models[schemaType.Name].ExtraFields[extraFieldName] = extraField
}
}
Expand Down

0 comments on commit e012530

Please sign in to comment.