-
Notifications
You must be signed in to change notification settings - Fork 0
/
etymology.go
69 lines (60 loc) · 1.78 KB
/
etymology.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package types
import "github.com/pkg/errors"
// WithEtymology is a compositing type for parsing the `et` property
type WithEtymology struct {
Etymology *Etymology `json:"et,omitempty"`
}
// Etymology https://dictionaryapi.com/products/json#sec-2.et
type Etymology SequenceMapping
// EtymologyItemType is an enum type for the types of items in Etymology
type EtymologyItemType int
// Values for EtymologyElementType
const (
EtymologyItemTypeUnknown = iota
EtymologyItemTypeText
EtymologyItemTypeSupplementalInfo
)
// EtymologyItemTypeFromString returns a EtymologyItemType from its string ID
func EtymologyItemTypeFromString(id string) EtymologyItemType {
switch id {
case "text":
return EtymologyItemTypeText
case "et_snote":
return EtymologyItemTypeSupplementalInfo
default:
return EtymologyItemTypeUnknown
}
}
func (t EtymologyItemType) String() string {
return []string{"", "text", "et_snote"}[t]
}
// Contents returns a copied slice of the contents in the Etymology
func (ety Etymology) Contents() ([]EtymologyItem, error) {
items := []EtymologyItem{}
for _, el := range ety {
key, err := el.Key()
if err != nil {
return nil, err
}
typ := EtymologyItemTypeFromString(key)
switch typ {
case EtymologyItemTypeText:
var out string
err = el.UnmarshalValue(&out)
items = append(items, EtymologyItem{Type: typ, Text: &out})
case EtymologyItemTypeSupplementalInfo:
var out SupplementalInfo
err = el.UnmarshalValue(&out)
items = append(items, EtymologyItem{Type: typ, SupplementalInfo: &out})
default:
err = errors.New("unknown item type in etymology")
}
}
return items, nil
}
// EtymologyItem is an item of the SI container
type EtymologyItem struct {
Type EtymologyItemType
Text *string
SupplementalInfo *SupplementalInfo
}