Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GECOM encoder #16

Merged
merged 8 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# gedcom

Go package to parse GEDCOM files.
Go package to parse and produce GEDCOM files.

[![Check Status](https://github.com/iand/gedcom/actions/workflows/check.yml/badge.svg?branch=master)](https://github.com/iand/gedcom/actions/workflows/check.yml)
[![Test Status](https://github.com/iand/gedcom/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/iand/gedcom/actions/workflows/test.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/iand/gedcom)](https://goreportcard.com/report/github.com/iand/gedcom)
[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white)](https://pkg.go.dev/github.com/iand/gedcom)

## Purpose

The `gedcom` package provides tools for working with GEDCOM files in Go. GEDCOM (Genealogical Data Communication) is a standard format used for exchanging genealogical data between software applications. This package includes functionality for both parsing existing GEDCOM files and generating new ones.

The package includes a streaming decoder for reading GEDCOM files and an encoder for creating GEDCOM files from Go structs.

## Usage

The package provides a Decoder with a single Decode method that returns a Gedcom struct. Use the NewDecoder method to create a new decoder.
The package provides a `Decoder` with a single `Decode` method that returns a Gedcom struct. Use the `NewDecoder` method to create a new decoder.

This example shows how to parse a GEDCOM file and list all the individuals. In this example the entire input file is read into memory, but the decoder is streaming so it should be able to deal with very large files: just pass an appropriate Reader.

Expand Down Expand Up @@ -40,17 +45,33 @@ The structures produced by the Decoder are in [types.go](types.go) and correspon

This package does not implement the entire GEDCOM specification, I'm still working on it. It's about 80% complete which is enough for about 99% of GEDCOM files. It has not been extensively tested with non-ASCII character sets nor with pathological cases such as the [GEDCOM 5.5 Torture Test Files](http://www.geditcom.com/gedcom.html).

### Using the Encoder

In addition to decoding GEDCOM files, this package also provides an Encoder for generating GEDCOM files from the structs in [types.go](types.go). You can create an encoder using the `NewEncoder` method, which writes to an `io.Writer`.

To see an example of how to use the encoder, refer to [encoder_example.go](encoder_example.go). This example illustrates how to create individual and family records, populate them with data, and encode them into a valid GEDCOM file.

You can run the example using the following command:

```bash
go run encoder_example.go
```

## Installation

Simply run

go get github.com/iand/gedcom
Run the following in the directory containing your project's `go.mod` file:

```bash
go get github.com/iand/gedcom@latest
```

Documentation is at [http://godoc.org/github.com/iand/gedcom](http://godoc.org/github.com/iand/gedcom)
Documentation is at [https://pkg.go.dev/github.com/iand/gedcom](https://pkg.go.dev/github.com/iand/gedcom)

## Authors

* [Ian Davis](http://github.com/iand) - <http://iandavis.com/>
* [Ian Davis](http://github.com/iand)


## Contributors
Expand Down
84 changes: 84 additions & 0 deletions cmp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package gedcom

import "github.com/google/go-cmp/cmp"

// familyXrefComparer is a Comparer that compares FamilyLinkRecords only by Family xref
var familyXrefComparer = cmp.Comparer(func(a, b *FamilyLinkRecord) bool {
if a == nil {
return b == nil
}

if b == nil {
return a == nil
}

if a.Family == nil {
return b.Family == nil
}

if b.Family == nil {
return a.Family == nil
}

return a.Family.Xref == b.Family.Xref
})

// individualXrefComparer is a Comparer that compares IndividualRecords only by xref
var individualXrefComparer = cmp.Comparer(func(a, b *IndividualRecord) bool {
if a == nil {
return b == nil
}

if b == nil {
return a == nil
}

return a.Xref == b.Xref
})

// sourceXrefComparer is a Comparer that compares CitationRecords only by source xref
var sourceXrefComparer = cmp.Comparer(func(a, b *CitationRecord) bool {
if a == nil {
return b == nil
}

if b == nil {
return a == nil
}

if a.Source == nil {
return b.Source == nil
}

if b.Source == nil {
return a.Source == nil
}

return a.Source.Xref == b.Source.Xref
})

// eventIgnoreComparer is a Comparer that ignores event comparisons
var eventIgnoreComparer = cmp.Comparer(func(a, b []*EventRecord) bool {
return true
})

// mediaFileNameCompare is a Comparer that compares MediaRecord only by first file name
var mediaFileNameCompare = cmp.Comparer(func(a, b *MediaRecord) bool {
if a == nil {
return b == nil
}

if b == nil {
return a == nil
}

if len(a.File) == 0 {
return len(b.File) == 0
}

if len(b.File) == 0 {
return len(a.File) == 0
}

return a.File[0].Name == b.File[0].Name
})
56 changes: 42 additions & 14 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ func makeRootParser(d *Decoder, g *Gedcom) parser {
d.pushParser(makeIndividualParser(d, obj, level))
case "SUBM":
// TODO: parse submitters
g.Submitter = append(g.Submitter, &SubmitterRecord{})
obj := d.submitter(xref)
g.Submitter = append(g.Submitter, obj)
case "FAM":
obj := d.family(xref)
g.Family = append(g.Family, obj)
Expand All @@ -224,6 +225,8 @@ func makeRootParser(d *Decoder, g *Gedcom) parser {
obj := d.media(xref)
g.Media = append(g.Media, obj)
d.pushParser(makeMediaParser(d, obj, level))
case "TRLR":
g.Trailer = &Trailer{}
default:
g.UserDefined = append(g.UserDefined, UserDefinedTag{
Tag: tag,
Expand Down Expand Up @@ -251,11 +254,29 @@ func makeIndividualParser(d *Decoder, i *IndividualRecord, minLevel int) parser
case "SEX":
i.Sex = value
case "BIRT", "CHR", "DEAT", "BURI", "CREM", "ADOP", "BAPM", "BARM", "BASM", "BLES", "CHRA", "CONF", "FCOM", "ORDN", "NATU", "EMIG", "IMMI", "CENS", "PROB", "WILL", "GRAD", "RETI", "EVEN":
e := &EventRecord{Tag: tag, Value: value}
e := &EventRecord{Tag: tag}
if value != "" {
if value == "Y" && (tag == "BIRT" || tag == "CHR" || tag == "DEAT") {
e.Value = "Y"
} else {
// event value is invalid and added as a note instead
r := &NoteRecord{Note: value}
e.Note = append(i.Note, r)
}
}
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":
Expand Down Expand Up @@ -356,7 +377,13 @@ func makeNameParser(d *Decoder, n *NameRecord, minLevel int) parser {
n.Note = append(n.Note, r)
d.pushParser(makeNoteParser(d, r, level))
default:
d.unhandledTag(level, tag, value, xref)
n.UserDefined = append(n.UserDefined, UserDefinedTag{
Tag: tag,
Value: value,
Xref: xref,
Level: level,
})
d.pushParser(makeUserDefinedTagParser(d, &n.UserDefined[len(n.UserDefined)-1], level))
}

return nil
Expand Down Expand Up @@ -415,6 +442,7 @@ func makeSourceParser(d *Decoder, s *SourceRecord, minLevel int) parser {
d.pushParser(makeTextParser(d, &s.Title, level))
case "ABBR":
s.FiledBy = value
d.pushParser(makeTextParser(d, &s.FiledBy, level))
case "AUTH":
s.Originator = value
d.pushParser(makeTextParser(d, &s.Originator, level))
Expand Down Expand Up @@ -541,8 +569,10 @@ func makeCitationParser(d *Decoder, c *CitationRecord, minLevel int) parser {
switch tag {
case "PAGE":
c.Page = value
d.pushParser(makeTextParser(d, &c.Page, level))
case "QUAY":
c.Quay = value
d.pushParser(makeTextParser(d, &c.Quay, level))
case "NOTE":
r := &NoteRecord{Note: value}
c.Note = append(c.Note, r)
Expand Down Expand Up @@ -845,7 +875,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":
Expand Down Expand Up @@ -910,15 +945,8 @@ func makeMediaParser(d *Decoder, m *MediaRecord, minLevel int) parser {
f.Format = value
d.pushParser(makeMediaFileFormatParser(d, f, level))
case "TITL": // version 5.5
var f *FileRecord
if len(m.File) == 0 {
f = &FileRecord{}
m.File = append(m.File, f)
} else {
f = m.File[len(m.File)-1]
}
f.Title = value
d.pushParser(makeTextParser(d, &f.Title, level))
m.Title = value
d.pushParser(makeTextParser(d, &m.Title, level))
case "RIN":
m.AutomatedRecordId = value
case "REFN":
Expand Down
Loading