Skip to content

Commit

Permalink
Fix Config.idFor() recursive ID lookup in anonymous fields (fixes #732)
Browse files Browse the repository at this point in the history
Prefix was not being propagated in second pass so was only correct at
first level where pref == ""

Add a test case to validate the fix
  • Loading branch information
phyrwork authored and dennwc committed Aug 25, 2018
1 parent 243214c commit 1f49ada
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ func (c *Config) idFor(rules fieldRules, rt reflect.Type, rv reflect.Value, pref
if !fld.Anonymous {
continue
}
id, err = c.idFor(rules, fld.Type, rv.Field(i), fld.Name+".")
id, err = c.idFor(rules, fld.Type, rv.Field(i), pref+fld.Name+".")
if err != nil || id != nil {
return
}
Expand Down
27 changes: 26 additions & 1 deletion schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ type subObject struct {
Num int `quad:"num"`
}

type subSubObject struct {
subObject
Num2 int `quad:"num2"`
}

func init() {
voc.RegisterPrefix("ex:", "http://example.org/")
schema.RegisterType(quad.IRI("ex:Coords"), Coords{})
Expand Down Expand Up @@ -237,6 +242,26 @@ var testWriteValueCases = []struct {
},
nil,
},
{
"simple object (embedded multiple levels)",
subSubObject{
subObject: subObject{
genObject: genObject{
ID: "1234",
Name: "Obj",
},
Num: 3,
},
Num2: 4,
},
iri("1234"),
[]quad.Quad{
{iri("1234"), iri("name"), quad.String("Obj"), nil},
{iri("1234"), iri("num"), quad.Int(3), nil},
{iri("1234"), iri("num2"), quad.Int(4), nil},
},
nil,
},
{
"required field not set",
item2{Name: "partial"},
Expand Down Expand Up @@ -636,4 +661,4 @@ func TestSaveNamespaces(t *testing.T) {
if !reflect.DeepEqual(expect, q) {
t.Fatalf("wrong quads returned: got: %v, expect: %v", q, expect)
}
}
}

0 comments on commit 1f49ada

Please sign in to comment.