Skip to content

Commit

Permalink
Merge pull request #62 from xythian/recursive_resolve
Browse files Browse the repository at this point in the history
Handle case when a struct has a field that refers to the same struct …
  • Loading branch information
xythian authored Sep 20, 2018
2 parents e3ac31f + dd22bf9 commit e1fe189
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
10 changes: 6 additions & 4 deletions rdl/schemabuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,13 @@ func (sb *SchemaBuilder) resolve(ordered []*Type, resolved map[string]bool, all
case "struct":
if t.StructTypeDef != nil {
for _, f := range t.StructTypeDef.Fields {
res, err := sb.resolveRef(ordered, resolved, all, strings.ToLower(string(f.Type)))
if err != nil {
return nil, err
if strings.ToLower(string(f.Type)) != strings.ToLower(string(t.StructTypeDef.Name)) {
res, err := sb.resolveRef(ordered, resolved, all, strings.ToLower(string(f.Type)))
if err != nil {
return nil, err
}
ordered = res
}
ordered = res
}
}
default:
Expand Down
27 changes: 27 additions & 0 deletions rdl/schemabuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,30 @@ func TestSBBaseTypeCaseSensitivity(test *testing.T) {
})
}
}

func TestRecursiveStruct(t *testing.T) {
sb := NewSchemaBuilder("test")
tEmployee := NewStructTypeBuilder("Struct", "Employee")
tEmployee.Field("manager", "Employee", true, nil, "")
sb.AddType(tEmployee.Build())
schema, err := sb.BuildParanoid()
if err != nil {
t.Errorf("build failed: %v", err)
} else {
found := false
for _, t := range schema.Types {
if t.StructTypeDef != nil {
if t.StructTypeDef.Name == TypeName("Employee") {
if Identifier("manager") == t.StructTypeDef.Fields[0].Name {
if t.StructTypeDef.Fields[0].Type == TypeRef("Employee") {
found = true
}
}
}
}
}
if !found {
t.Errorf("did not find recursive field manager")
}
}
}

0 comments on commit e1fe189

Please sign in to comment.