Skip to content

Commit d9bfbd6

Browse files
authored
internal/generator - embed inherited types (#20)
Refactor struct inheritance by creating separate structs for parent types and embedding them in child structs instead of using inline property inheritance.
1 parent cb8af22 commit d9bfbd6

File tree

3 files changed

+269
-442
lines changed

3 files changed

+269
-442
lines changed

internal/generator/internal/codegen/generator.go

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -238,25 +238,6 @@ func (b *Generator) addInterface(f *jen.File, ifc *spec.Interface) error {
238238
return nil
239239
}
240240

241-
func (b *Generator) addInheritedFields(g *jen.Group, ifc *spec.Interface) error {
242-
if ifc.Inherits.TypeName.Name == "" {
243-
return nil
244-
}
245-
246-
parent, found := b.allTypes[ifc.Inherits.TypeName]
247-
if !found {
248-
return fmt.Errorf("failed to find parent type %s.%s", ifc.Inherits.TypeName.Namespace, ifc.Inherits.TypeName.Name)
249-
}
250-
251-
ifcType, ok := parent.Value.(spec.Interface)
252-
if !ok {
253-
return fmt.Errorf("%s inherits from an unexpected kind %T", ifc.TypeName.Name, parent)
254-
}
255-
256-
b.addProperties(g, ifcType.Properties)
257-
return nil
258-
}
259-
260241
func (b *Generator) addProperties(g *jen.Group, props []spec.Property) {
261242
for _, p := range props {
262243
required := p.Required != nil && *p.Required
@@ -338,8 +319,8 @@ func (b *Generator) goStruct(ifc *spec.Interface, f *jen.File) error {
338319

339320
var err error
340321
f.Type().Id(ifc.TypeName.Name).StructFunc(func(g *jen.Group) {
341-
if err = b.addInheritedFields(g, ifc); err != nil {
342-
return
322+
if ifc.Inherits.TypeName.Name != "" {
323+
g.Id(ifc.Inherits.TypeName.Name)
343324
}
344325
b.addProperties(g, ifc.Properties)
345326
})
@@ -354,6 +335,13 @@ func (b *Generator) depsOfType(t *spec.TypeDefinition, deps map[spec.TypeName]bo
354335

355336
switch v := t.Value.(type) {
356337
case spec.Interface:
338+
if v.Inherits.TypeName.Name != "" {
339+
deps[v.Inherits.TypeName] = true
340+
if parent, found := b.allTypes[v.Inherits.TypeName]; found {
341+
b.depsOfType(parent, deps)
342+
}
343+
}
344+
357345
for _, p := range v.Properties {
358346
b.depsOfValue(p.Type, deps)
359347
}

0 commit comments

Comments
 (0)