-
Notifications
You must be signed in to change notification settings - Fork 0
/
supplementalinfo.go
74 lines (66 loc) · 2.12 KB
/
supplementalinfo.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
70
71
72
73
74
package types
import (
"fmt"
)
// SupplementalInfo https://dictionaryapi.com/products/json#sec-2.snote
type SupplementalInfo SequenceMapping
// SupplementalInfoItemType is an enum type for the types of items in SupplementalInfo
type SupplementalInfoItemType int
// Values for SupplementalInfoItemType
const (
SupplementalInfoItemTypeUnknown = iota
SupplementalInfoItemTypeText
SupplementalInfoItemTypeRunIn
SupplementalInfoItemTypeVerbalIllustration
)
// SupplementalInfoItemTypeFromString returns a SupplementalInfoItemType from its string ID
func SupplementalInfoItemTypeFromString(id string) SupplementalInfoItemType {
switch id {
case "t":
return SupplementalInfoItemTypeText
case "ri":
return SupplementalInfoItemTypeRunIn
case "vis":
return SupplementalInfoItemTypeVerbalIllustration
default:
return SupplementalInfoItemTypeUnknown
}
}
func (t SupplementalInfoItemType) String() string {
return []string{"", "t", "ri", "vis"}[t]
}
// Contents returns a copied slice of the contents in the SupplementalInfo
func (si SupplementalInfo) Contents() ([]SupplementalInfoItem, error) {
items := []SupplementalInfoItem{}
for _, el := range si {
key, err := el.Key()
if err != nil {
return nil, err
}
typ := SupplementalInfoItemTypeFromString(key)
switch typ {
case SupplementalInfoItemTypeText:
var out string
err = el.UnmarshalValue(&out)
items = append(items, SupplementalInfoItem{Type: typ, Text: &out})
case SupplementalInfoItemTypeRunIn:
var out RunIn
err = el.UnmarshalValue(&out)
items = append(items, SupplementalInfoItem{Type: typ, RunIn: &out})
case SupplementalInfoItemTypeVerbalIllustration:
var out VerbalIllustration
err = el.UnmarshalValue(&out)
items = append(items, SupplementalInfoItem{Type: typ, VerbalIllustration: &out})
default:
err = fmt.Errorf("unknown item type in supplemental info: %v (enum: %v)", key, typ)
}
}
return items, nil
}
// SupplementalInfoItem is an item of the SI container
type SupplementalInfoItem struct {
Type SupplementalInfoItemType
Text *string
RunIn *RunIn
VerbalIllustration *VerbalIllustration
}