From 1f49adaae4e402c09ae31deff0339ce98f654ccf Mon Sep 17 00:00:00 2001 From: Connor Newton Date: Sat, 25 Aug 2018 13:23:43 +0100 Subject: [PATCH] Fix Config.idFor() recursive ID lookup in anonymous fields (fixes #732) 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 --- schema/schema.go | 2 +- schema/schema_test.go | 27 ++++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index ab275741e..c82e873bf 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -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 } diff --git a/schema/schema_test.go b/schema/schema_test.go index 1c1ad6da5..052eb8ebe 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -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{}) @@ -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"}, @@ -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) } -} +} \ No newline at end of file