Skip to content

Commit

Permalink
If spouse is already nil, short-circuit out. (#311)
Browse files Browse the repository at this point in the history
This fixes a bug from a previous patch in #310.
  • Loading branch information
jazzboME authored May 9, 2020
1 parent e015efa commit 9061dca
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
15 changes: 11 additions & 4 deletions family_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,13 @@ func (node *FamilyNode) AddChild(individual *IndividualNode) *ChildNode {
}

func (node *FamilyNode) SetHusband(individual *IndividualNode) *FamilyNode {
if individual == nil {
if IsNil(individual) {
husband := node.Husband().Individual()
if IsNil(husband) {
return node
}
nodes := husband.Nodes()

for _, subNode := range nodes {
if subNode.Tag() == TagFamilySpouse && subNode.Value() == node.Identifier() {
husband.DeleteNode(subNode)
Expand All @@ -161,8 +164,11 @@ func (node *FamilyNode) SetHusband(individual *IndividualNode) *FamilyNode {
}

func (node *FamilyNode) SetWife(individual *IndividualNode) *FamilyNode {
if individual == nil {
if IsNil(individual) {
wife := node.Wife().Individual()
if IsNil(wife) {
return node
}
nodes := wife.Nodes()

for _, subNode := range nodes {
Expand All @@ -180,7 +186,8 @@ func (node *FamilyNode) SetWife(individual *IndividualNode) *FamilyNode {
n := NewNode(TagFamilySpouse, node.Identifier(), "")
individual.AddNode(n)

return node.SetWifePointer(individual.Pointer())}
return node.SetWifePointer(individual.Pointer())
}

func (node *FamilyNode) SetWifePointer(pointer string) *FamilyNode {
wife := node.Wife()
Expand Down
25 changes: 25 additions & 0 deletions family_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ var familyTests = []struct {
husband: nil,
wife: jane,
},
{
doc: func(doc *gedcom.Document) {
jane := individual(doc, "P2", "Jane /Doe/", "3 Mar 1803", "14 June 1877")
elliot := individual(doc, "P1", "Elliot /Chance/", "4 Jan 1843", "17 Mar 1907")
doc.AddFamilyWithHusbandAndWife("F3", elliot, jane)
},
husband: elliot,
wife: jane,
},
{
doc: func(doc *gedcom.Document) {
elliot := individual(doc, "P1", "Elliot /Chance/", "4 Jan 1843", "17 Mar 1907")
doc.AddFamilyWithHusbandAndWife("F3", elliot, nil)
},
husband: elliot,
wife: nil,
},
{
doc: func(doc *gedcom.Document) {
jane := individual(doc, "P2", "Jane /Doe/", "3 Mar 1803", "14 June 1877")
doc.AddFamilyWithHusbandAndWife("F3", nil, jane)
},
husband: nil,
wife: jane,
},
}

func TestFamilyNode_Husband(t *testing.T) {
Expand Down

0 comments on commit 9061dca

Please sign in to comment.