Skip to content

Commit

Permalink
fix: assign inline based on json tag (elastic#48)
Browse files Browse the repository at this point in the history
This considers a field as "inline" based on the presence of "inline" in the json tag instead of the field name and json tag both being empty.

Signed-off-by: Amund Tenstad <github@amund.io>
  • Loading branch information
tenstad committed Oct 4, 2023
1 parent 1badd70 commit c9060e9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
24 changes: 11 additions & 13 deletions processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,6 @@ func (p *processor) processStructFields(parentType *types.Type, pkg *loader.Pack
parentTypeKey := types.Key(parentType)

for _, f := range info.Fields {
t := pkg.TypesInfo.TypeOf(f.RawField.Type)
if t == nil {
zap.S().Debugw("Failed to determine type of field", "field", f.Name)
continue
}

fieldDef := &types.Field{
Name: f.Name,
Doc: f.Doc,
Expand All @@ -324,6 +318,15 @@ func (p *processor) processStructFields(parentType *types.Type, pkg *loader.Pack
if len(args) > 0 && args[0] != "" {
fieldDef.Name = args[0]
}
if len(args) > 1 && args[1] == "inline" {
fieldDef.Inlined = true
}
}

t := pkg.TypesInfo.TypeOf(f.RawField.Type)
if t == nil {
zap.S().Debugw("Failed to determine type of field", "field", fieldDef.Name)
continue
}

logger.Debugw("Loading field type", "field", fieldDef.Name)
Expand All @@ -332,11 +335,8 @@ func (p *processor) processStructFields(parentType *types.Type, pkg *loader.Pack
continue
}

if fieldDef.Embedded {
fieldDef.Inlined = fieldDef.Name == ""
if fieldDef.Name == "" {
fieldDef.Name = fieldDef.Type.Name
}
if fieldDef.Name == "" {
fieldDef.Name = fieldDef.Type.Name
}

if p.shouldIgnoreField(parentTypeKey, fieldDef.Name) {
Expand All @@ -345,8 +345,6 @@ func (p *processor) processStructFields(parentType *types.Type, pkg *loader.Pack
}

parentType.Fields = append(parentType.Fields, fieldDef)

// add to references map
p.addReference(parentType, fieldDef.Type)
}

Expand Down
4 changes: 2 additions & 2 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ func (types TypeMap) InlineTypes(propagateReference func(original *Type, additio
// Field describes a field in a struct.
type Field struct {
Name string
Embedded bool
Inlined bool
Embedded bool // Embedded struct in Go typing
Inlined bool // Inlined struct in serialization
Doc string
Type *Type
}
Expand Down

0 comments on commit c9060e9

Please sign in to comment.