Skip to content

Commit

Permalink
add embed property to field
Browse files Browse the repository at this point in the history
support embedded interfaces
  • Loading branch information
switchupcb committed Nov 15, 2022
1 parent d4722f4 commit 1fb8ff9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions cli/models/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type Field struct {

// The custom options of a field.
Options FieldOptions

// Embedded represents whether the field is an embedded field.
Embedded bool
}

// FieldOptions represent options for a Field.
Expand Down Expand Up @@ -86,6 +89,7 @@ func (f *Field) Deepcopy(cyclic map[*Field]bool) *Field {
Automatch: f.Options.Automatch,
Deepcopy: f.Options.Deepcopy,
},
Embedded: f.Embedded,
}

copied.Tags = make(map[string]map[string][]string, len(f.Tags))
Expand Down Expand Up @@ -114,6 +118,7 @@ func (f *Field) Deepcopy(cyclic map[*Field]bool) *Field {
copied.Fields[i] = sf.Deepcopy(cyclic)
copied.Fields[i].Parent = copied
}

return copied
}

Expand Down
24 changes: 18 additions & 6 deletions cli/parser/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,15 @@ func parseField(typ types.Type) *models.Field {
} else {
var definition strings.Builder
definition.WriteString(models.CollectionInterface + "{")

for i := 0; i < x.NumMethods(); i++ {
definition.WriteString(collectedDefinition(parseField(x.Method(i).Type())) + "; ")
}

for i := 0; i < x.NumEmbeddeds(); i++ {
definition.WriteString(collectedDefinition(parseField(x.EmbeddedType(i))) + "; ")
}

definition.WriteString("}")
field.Definition = definition.String()
}
Expand All @@ -127,22 +133,28 @@ func parseField(typ types.Type) *models.Field {
var definition strings.Builder
definition.WriteString("struct{")
for i := 0; i < x.NumFields(); i++ {
// a deepcopy of subfield is returned and modified.
// a deepcopy of subfield is returned, then modified.
subfield := parseField(x.Field(i).Type()).Deepcopy(nil)
subfield.VariableName = "." + x.Field(i).Name()
subfield.Name = x.Field(i).Name()
setTags(subfield, x.Tag(i))
subfield.Parent = field
field.Fields = append(field.Fields, subfield)

if x.Field(i).Embedded() {
subfield.Embedded = true
}

definition.WriteString(subfield.Name + " " + subfield.FullDefinition() + "; ")

// all subfields are deepcopied with Fields[0].
// Due to the possibility of cyclic structs,
// all subfields are deepcopied with len([]Fields) == (0:?).
//
// in order to correctly represent a deepcopied struct field,
// we must point its fields back to the cached field.Fields,
// which will eventually be filled.
// In order to correctly represent a deepcopied subfield,
// point its fields back to the cached field []Fields,
// which are eventually filled.
//
// cachedsubfield.Fields are never modified.
// cachedsubfield.Fields pointer is never modified.
if cachedsubfield, ok := fieldcache[x.Field(i).String()]; ok {
subfield.Fields = cachedsubfield.Fields
}
Expand Down
2 changes: 2 additions & 0 deletions cli/parser/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ func parseTypeField(vars *types.Tuple) []models.Type {
for i := 0; i < vars.Len(); i++ {
field := parseField(vars.At(i).Type()).Deepcopy(nil)
field.Name = vars.At(i).Name()

if !field.IsPointer() {
field.VariableName = "." + alphastring(field.Definition)
}

types[i] = models.Type{Field: field}
}

Expand Down

0 comments on commit 1fb8ff9

Please sign in to comment.