Export pid and person as public packages (#61)
internal/pid and internal/person move to the module root so bytestone-loader can import them instead of carrying copies. The record's YAML tags gain omitempty, so a writer emits only what is set. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
8b4a555256
commit
e3b947e1aa
21 changed files with 135 additions and 41 deletions
|
|
@ -9,7 +9,7 @@ Read the global instructions first; this file adds only what is repo-specific.
|
|||
`~/.local/share/bytestone/archive`. `site/` is derived
|
||||
and gitignored. README has the record format and the vocabulary — use
|
||||
those names in code, tests and docs.
|
||||
- IDs are `internal/pid`: Crockford base32, lower case. Never introduce
|
||||
- IDs are `pid` (a public package, as is `person`: bytestone-loader imports both): Crockford base32, lower case. Never introduce
|
||||
another encoding or a case-sensitive one; URLs use the id and the
|
||||
archive uses `pid.Dir` of it, nothing else.
|
||||
- Victoria Drummond's documents and photographs are the lecture repo's
|
||||
|
|
|
|||
|
|
@ -23,6 +23,9 @@ life-weaver id 20000000000 # jm1fj00
|
|||
life-weaver id jm1fj00 # 20000000000
|
||||
```
|
||||
|
||||
`pid` is a public package, as is `person` (the record and the register):
|
||||
bytestone-loader imports both to write records into a sandbox archive.
|
||||
|
||||
## Records
|
||||
|
||||
Each person is one directory in the archive, `people/<dir>/`, holding
|
||||
|
|
|
|||
|
|
@ -19,9 +19,9 @@ import (
|
|||
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/archive"
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/item"
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/person"
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/pid"
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/site"
|
||||
"git.bytestone.uk/hum3/life-weaver/person"
|
||||
"git.bytestone.uk/hum3/life-weaver/pid"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
|
|||
46
docs/stories/002-export-pid-and-person-as-public-packages.md
Normal file
46
docs/stories/002-export-pid-and-person-as-public-packages.md
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
# 002 — Export pid and person as public packages (#61)
|
||||
|
||||
**Status:** in progress
|
||||
**Roadmap:** none — a fix for a consumer: bytestone-loader
|
||||
|
||||
## Why
|
||||
|
||||
bytestone-loader writes `person.yaml` records into a sandbox archive for
|
||||
life-weaver to build. The id encoding and the record are life-weaver's
|
||||
contract, but they live under `internal/`, so the loader carries copies
|
||||
pinned to the README table. Two implementations of one format drift
|
||||
silently; the builder is where the record is defined, so it should be the
|
||||
one place.
|
||||
|
||||
## What changes
|
||||
|
||||
- `internal/pid` becomes `pid`, `internal/person` becomes `person`:
|
||||
importable as `git.bytestone.uk/hum3/life-weaver/pid` and `/person`.
|
||||
Nothing else in the API changes.
|
||||
- The record's YAML tags gain `omitempty`, so a writer marshalling a
|
||||
`Person` emits only what is set, as the hand-written records do. Reading
|
||||
is unchanged.
|
||||
|
||||
## Acceptance
|
||||
|
||||
| Writer marshals | YAML |
|
||||
|---|---|
|
||||
| `Person{Name: "A"}` | `name: A` and nothing else |
|
||||
| `Person{Name: "A", Born: Event{Date: "1894"}}` | `name`, then `born` with `date` alone |
|
||||
| any record the register loads | marshals and loads back to the same record |
|
||||
|
||||
## Out of scope
|
||||
|
||||
- A shared `bytestone` module: two consumers do not earn one. When the
|
||||
register moves to Postgres (#7) and more things read it, revisit.
|
||||
- Exporting `archive`, `item` or `site`.
|
||||
|
||||
## Decisions
|
||||
|
||||
none
|
||||
|
||||
## Done when
|
||||
|
||||
- [ ] tests express the acceptance table and pass
|
||||
- [ ] README and CLAUDE.md name the packages
|
||||
- [ ] changelog entry
|
||||
|
|
@ -12,8 +12,8 @@ import (
|
|||
"strings"
|
||||
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/item"
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/person"
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/pid"
|
||||
"git.bytestone.uk/hum3/life-weaver/person"
|
||||
"git.bytestone.uk/hum3/life-weaver/pid"
|
||||
)
|
||||
|
||||
// Service serves one loaded register and the archive's items.
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/item"
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/person"
|
||||
"git.bytestone.uk/hum3/life-weaver/person"
|
||||
)
|
||||
|
||||
func serve(t *testing.T) *httptest.Server {
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ import (
|
|||
"strings"
|
||||
"sync"
|
||||
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/pid"
|
||||
"git.bytestone.uk/hum3/life-weaver/pid"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/pid"
|
||||
"git.bytestone.uk/hum3/life-weaver/pid"
|
||||
)
|
||||
|
||||
// root is an empty archive: a people tree with no items.
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ import (
|
|||
"gopkg.in/yaml.v3"
|
||||
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/item"
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/pid"
|
||||
"git.bytestone.uk/hum3/life-weaver/pid"
|
||||
)
|
||||
|
||||
// itemView is one held item: on the person page its front's thumbnail,
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ import (
|
|||
"github.com/yuin/goldmark"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/person"
|
||||
"git.bytestone.uk/hum3/life-weaver/person"
|
||||
)
|
||||
|
||||
//go:embed templates
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/person"
|
||||
"git.bytestone.uk/hum3/life-weaver/person"
|
||||
)
|
||||
|
||||
func build(t *testing.T) string {
|
||||
|
|
|
|||
|
|
@ -5,32 +5,32 @@
|
|||
// resolved.
|
||||
package person
|
||||
|
||||
import "git.bytestone.uk/hum3/life-weaver/internal/pid"
|
||||
import "git.bytestone.uk/hum3/life-weaver/pid"
|
||||
|
||||
// Event is a dated, placed moment in a life.
|
||||
type Event struct {
|
||||
Date Date `yaml:"date" json:"date,omitempty"`
|
||||
Place string `yaml:"place" json:"place,omitempty"`
|
||||
Date Date `yaml:"date,omitempty" json:"date,omitempty"`
|
||||
Place string `yaml:"place,omitempty" json:"place,omitempty"`
|
||||
}
|
||||
|
||||
// Marriage is recorded on one spouse's record and shown on both.
|
||||
type Marriage struct {
|
||||
Spouse pid.ID `yaml:"spouse" json:"spouse"`
|
||||
Date Date `yaml:"date" json:"date,omitempty"`
|
||||
Place string `yaml:"place" json:"place,omitempty"`
|
||||
Date Date `yaml:"date,omitempty" json:"date,omitempty"`
|
||||
Place string `yaml:"place,omitempty" json:"place,omitempty"`
|
||||
}
|
||||
|
||||
// Photo is a picture we may publish, with its credit.
|
||||
type Photo struct {
|
||||
File string `yaml:"file" json:"file,omitempty"`
|
||||
Caption string `yaml:"caption" json:"caption,omitempty"`
|
||||
Credit string `yaml:"credit" json:"credit,omitempty"`
|
||||
File string `yaml:"file,omitempty" json:"file,omitempty"`
|
||||
Caption string `yaml:"caption,omitempty" json:"caption,omitempty"`
|
||||
Credit string `yaml:"credit,omitempty" json:"credit,omitempty"`
|
||||
}
|
||||
|
||||
// Album is a titled set of photographs.
|
||||
type Album struct {
|
||||
Title string `yaml:"title" json:"title,omitempty"`
|
||||
Photos []Photo `yaml:"photos" json:"photos,omitempty"`
|
||||
Title string `yaml:"title,omitempty" json:"title,omitempty"`
|
||||
Photos []Photo `yaml:"photos,omitempty" json:"photos,omitempty"`
|
||||
}
|
||||
|
||||
// Resource is a document about the person that the site publishes: a
|
||||
|
|
@ -38,15 +38,15 @@ type Album struct {
|
|||
// index.html and assets) is published as it is, anything else is offered
|
||||
// for download.
|
||||
type Resource struct {
|
||||
Title string `yaml:"title" json:"title,omitempty"`
|
||||
File string `yaml:"file" json:"file,omitempty"`
|
||||
Note string `yaml:"note" json:"note,omitempty"`
|
||||
Title string `yaml:"title,omitempty" json:"title,omitempty"`
|
||||
File string `yaml:"file,omitempty" json:"file,omitempty"`
|
||||
Note string `yaml:"note,omitempty" json:"note,omitempty"`
|
||||
}
|
||||
|
||||
// Link is a page elsewhere about the person.
|
||||
type Link struct {
|
||||
Title string `yaml:"title" json:"title,omitempty"`
|
||||
URL string `yaml:"url" json:"url,omitempty"`
|
||||
Title string `yaml:"title,omitempty" json:"title,omitempty"`
|
||||
URL string `yaml:"url,omitempty" json:"url,omitempty"`
|
||||
}
|
||||
|
||||
// Person is one record. File paths are relative to Dir, the person's
|
||||
|
|
@ -55,19 +55,19 @@ type Person struct {
|
|||
ID pid.ID `yaml:"-" json:"id"`
|
||||
Dir string `yaml:"-" json:"-"`
|
||||
Name string `yaml:"name" json:"name"`
|
||||
Short string `yaml:"short" json:"short,omitempty"`
|
||||
Summary string `yaml:"summary" json:"summary,omitempty"`
|
||||
Portrait string `yaml:"portrait" json:"portrait,omitempty"` // a file under files/, shown at the head of the page
|
||||
Born Event `yaml:"born" json:"born,omitzero"`
|
||||
Died Event `yaml:"died" json:"died,omitzero"`
|
||||
Buried Event `yaml:"buried" json:"buried,omitzero"`
|
||||
Parents []pid.ID `yaml:"parents" json:"parents,omitempty"`
|
||||
Marriages []Marriage `yaml:"marriages" json:"marriages,omitempty"`
|
||||
Bio string `yaml:"bio" json:"bio,omitempty"`
|
||||
Albums []Album `yaml:"albums" json:"albums,omitempty"`
|
||||
Resources []Resource `yaml:"resources" json:"resources,omitempty"`
|
||||
Links []Link `yaml:"links" json:"links,omitempty"`
|
||||
Sources []string `yaml:"sources" json:"sources,omitempty"`
|
||||
Short string `yaml:"short,omitempty" json:"short,omitempty"`
|
||||
Summary string `yaml:"summary,omitempty" json:"summary,omitempty"`
|
||||
Portrait string `yaml:"portrait,omitempty" json:"portrait,omitempty"` // a file under files/, shown at the head of the page
|
||||
Born Event `yaml:"born,omitempty" json:"born,omitzero"`
|
||||
Died Event `yaml:"died,omitempty" json:"died,omitzero"`
|
||||
Buried Event `yaml:"buried,omitempty" json:"buried,omitzero"`
|
||||
Parents []pid.ID `yaml:"parents,omitempty" json:"parents,omitempty"`
|
||||
Marriages []Marriage `yaml:"marriages,omitempty" json:"marriages,omitempty"`
|
||||
Bio string `yaml:"bio,omitempty" json:"bio,omitempty"`
|
||||
Albums []Album `yaml:"albums,omitempty" json:"albums,omitempty"`
|
||||
Resources []Resource `yaml:"resources,omitempty" json:"resources,omitempty"`
|
||||
Links []Link `yaml:"links,omitempty" json:"links,omitempty"`
|
||||
Sources []string `yaml:"sources,omitempty" json:"sources,omitempty"`
|
||||
}
|
||||
|
||||
// Label is the name to link by: the short name if given, else the full one.
|
||||
|
|
@ -3,10 +3,13 @@ package person
|
|||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/pid"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"git.bytestone.uk/hum3/life-weaver/pid"
|
||||
)
|
||||
|
||||
func TestDateFormat(t *testing.T) {
|
||||
|
|
@ -182,3 +185,45 @@ func TestLoadSetsDir(t *testing.T) {
|
|||
t.Errorf("Dir = %q, want the absolute path of testdata/people/0/00", p.Dir)
|
||||
}
|
||||
}
|
||||
|
||||
// A writer (bytestone-loader) marshals a Person and gets only what is set;
|
||||
// what the register loads marshals and loads back unchanged.
|
||||
func TestMarshalOmitsUnset(t *testing.T) {
|
||||
cases := []struct {
|
||||
p Person
|
||||
want string
|
||||
}{
|
||||
{Person{Name: "A"}, "name: A\n"},
|
||||
{Person{Name: "A", Born: Event{Date: "1894"}}, "name: A\nborn:\n date: \"1894\"\n"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
b, err := yaml.Marshal(&c.p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(b) != c.want {
|
||||
t.Errorf("Marshal(%+v) =\n%s\nwant\n%s", c.p, b, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalRoundTrip(t *testing.T) {
|
||||
reg, err := Load("testdata/people")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, p := range reg.People {
|
||||
b, err := yaml.Marshal(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var back Person
|
||||
if err := yaml.Unmarshal(b, &back); err != nil {
|
||||
t.Fatalf("%s: %v\n%s", p.ID, err, b)
|
||||
}
|
||||
back.ID, back.Dir = p.ID, p.Dir
|
||||
if !reflect.DeepEqual(&back, p) {
|
||||
t.Errorf("%s: round trip differs\n%s", p.ID, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -12,7 +12,7 @@ import (
|
|||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"git.bytestone.uk/hum3/life-weaver/internal/pid"
|
||||
"git.bytestone.uk/hum3/life-weaver/pid"
|
||||
)
|
||||
|
||||
// Register is every record in a directory, with the ties between them checked.
|
||||
Loading…
Reference in a new issue