Skip to content

Commit

Permalink
fix multiple fields with the same name
Browse files Browse the repository at this point in the history
  • Loading branch information
tsingbx committed Jan 10, 2025
1 parent 29aa45c commit c6ec794
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion cmd/gogensig/convert/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,36 @@ func (p *TypeConv) RecordTypeToStruct(recordType *ast.RecordType) (types.Type, e
fields = []*types.Var{maxFld}
}
}
return types.NewStruct(fields, nil), nil
uniFields := p.uniqueFields(fields)
return types.NewStruct(uniFields, nil), nil
}

func genUniqueName(name string, index int, fieldMap map[string]struct{}) string {
newName := fmt.Sprintf("%s%d", name, index)
_, ok := fieldMap[newName]
if !ok {
return newName
}
return genUniqueName(name, index+1, fieldMap)
}

func (p *TypeConv) uniqueFields(fields []*types.Var) []*types.Var {
fieldMap := make(map[string]struct{})
newFields := make([]*types.Var, 0, len(fields))
for index, field := range fields {
name := field.Name()
_, ok := fieldMap[name]
if ok {
name = genUniqueName(field.Name(), index, fieldMap)
fieldVar := types.NewVar(token.NoPos, p.Types, name, field.Type())
newFields = append(newFields, fieldVar)
fieldMap[name] = struct{}{}
} else {
fieldMap[name] = struct{}{}
newFields = append(newFields, field)
}
}
return newFields
}

func (p *TypeConv) ToDefaultEnumType() types.Type {
Expand Down

0 comments on commit c6ec794

Please sign in to comment.