A tool for archiving & converting scans of postcards, and information about them.
1package types
2
3import (
4 "encoding/xml"
5 "fmt"
6 "regexp"
7)
8
9type Person struct {
10 Name string `json:"name,omitempty"`
11 Uri string `json:"uri,omitempty" yaml:"link,omitempty"`
12}
13
14func (p Person) String() string {
15 if p.Uri == "" {
16 return p.Name
17 }
18
19 return fmt.Sprintf("%s (%s)", p.Name, p.Uri)
20}
21
22var personRE = regexp.MustCompile(`^(.+) \(([^()]+)\)$`)
23
24func (p *Person) Scan(str string) {
25 if parts := personRE.FindStringSubmatch(str); len(parts) > 0 {
26 p.Name = parts[1]
27 p.Uri = parts[2]
28 } else {
29 p.Name = str
30 p.Uri = ""
31 }
32}
33
34func (p Person) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
35 return e.EncodeElement(p.String(), start)
36}