From 901ca919cc5d1ef8503d1a1a59a7107b76fd4317 Mon Sep 17 00:00:00 2001 From: Ian Davis <18375+iand@users.noreply.github.com> Date: Sun, 28 Jul 2024 17:11:33 +0100 Subject: [PATCH] Handle values on RESI attribute --- decoder.go | 20 ++++++++++-- decoder_test.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 4 deletions(-) diff --git a/decoder.go b/decoder.go index 20d070b..2b7c71a 100644 --- a/decoder.go +++ b/decoder.go @@ -257,7 +257,7 @@ func makeIndividualParser(d *Decoder, i *IndividualRecord, minLevel int) parser if value == "Y" && (tag == "BIRT" || tag == "CHR" || tag == "DEAT") { e.Value = "Y" } else { - // any event other value is invalid and added as a note instead + // event value is invalid and added as a note instead r := &NoteRecord{Note: value} e.Note = append(i.Note, r) } @@ -265,7 +265,16 @@ func makeIndividualParser(d *Decoder, i *IndividualRecord, minLevel int) parser i.Event = append(i.Event, e) d.pushParser(makeEventParser(d, tag, e, level)) case "CAST", "DSCR", "EDUC", "IDNO", "NATI", "NCHI", "NMR", "OCCU", "PROP", "RELI", "RESI", "SSN", "TITL", "FACT": - e := &EventRecord{Tag: tag, Value: value} + e := &EventRecord{Tag: tag} + if value != "" { + if tag == "RESI" { + // event value is invalid and added as a note instead + r := &NoteRecord{Note: value} + e.Note = append(i.Note, r) + } else { + e.Value = value + } + } i.Attribute = append(i.Attribute, e) d.pushParser(makeEventParser(d, tag, e, level)) case "FAMC": @@ -864,7 +873,12 @@ func makeFamilyParser(d *Decoder, f *FamilyRecord, minLevel int) parser { case "CHIL": f.Child = append(f.Child, d.individual(stripXref(value))) case "ANUL", "CENS", "DIV", "DIVF", "ENGA", "MARR", "MARB", "MARC", "MARL", "MARS", "EVEN", "RESI": - e := &EventRecord{Tag: tag, Value: value} + e := &EventRecord{Tag: tag} + if value != "" { + // any event other value is invalid and added as a note instead + r := &NoteRecord{Note: value} + e.Note = append(e.Note, r) + } f.Event = append(f.Event, e) d.pushParser(makeEventParser(d, tag, e, level)) case "NCHI": diff --git a/decoder_test.go b/decoder_test.go index 8790f5f..5d608aa 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -1488,7 +1488,7 @@ func TestPlace(t *testing.T) { } } -func TestEvent(t *testing.T) { +func TestIndividualEvent(t *testing.T) { testCases := []struct { name string input string @@ -1575,3 +1575,86 @@ func TestEvent(t *testing.T) { }) } } + +func TestIndividualAttribute(t *testing.T) { + testCases := []struct { + name string + input string + want *EventRecord + }{ + { + name: "even_value_to_note", + input: ` + 1 RESI Marital Status: MarriedRelation to Head of House: Head + 2 DATE 1 Jun 1921 + `, + want: &EventRecord{ + Tag: "RESI", + Date: "1 Jun 1921", + Note: []*NoteRecord{ + {Note: "Marital Status: MarriedRelation to Head of House: Head"}, + }, + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + tc.input = "0 @test@ INDI\n" + tc.input + d := NewDecoder(bytes.NewReader([]byte(tc.input))) + + g, err := d.Decode() + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if diff := cmp.Diff(tc.want, g.Individual[0].Attribute[0]); diff != "" { + t.Errorf("event mismatch (-want +got):\n%s", diff) + } + }) + } +} + +func TestFamilyEvent(t *testing.T) { + testCases := []struct { + name string + input string + want *EventRecord + }{ + { + // findmypast uses the note as the value of the EVEN + name: "even_value_to_note", + input: ` + 1 EVEN was age 10 and the daughter of the head of the household + 2 TYPE Census UK 1881 + 2 _PRIM Y + 2 DATE 3 Apr 1881 + `, + want: &EventRecord{ + Tag: "EVEN", + Type: "Census UK 1881", + Date: "3 Apr 1881", + Note: []*NoteRecord{ + {Note: "was age 10 and the daughter of the head of the household"}, + }, + UserDefined: []UserDefinedTag{{Tag: "_PRIM", Value: "Y", Level: 2}}, + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + tc.input = "0 @test@ FAM\n" + tc.input + d := NewDecoder(bytes.NewReader([]byte(tc.input))) + + g, err := d.Decode() + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if diff := cmp.Diff(tc.want, g.Family[0].Event[0]); diff != "" { + t.Errorf("event mismatch (-want +got):\n%s", diff) + } + }) + } +}