-
Notifications
You must be signed in to change notification settings - Fork 0
/
usage.go
89 lines (78 loc) · 2.81 KB
/
usage.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package types
import "github.com/pkg/errors"
// WithUsages is a compositing type for parsing the `usages` property
type WithUsages struct {
// https://dictionaryapi.com/products/json#sec-2.usages
UsageParagraphs []UsageParagraphs `json:"usages,omitempty"`
}
// UsageParagraphs https://dictionaryapi.com/products/json#sec-2.usages
type UsageParagraphs struct {
Label string `json:"pl,omitempty"`
Text *UsageParagraphText `json:"pt,omitempty"`
}
// UsageParagraphText https://dictionaryapi.com/products/json#sec-2.usages
type UsageParagraphText SequenceMapping
// UsageParagraphTextItemType is an enum type for the types of items in the paragraph
type UsageParagraphTextItemType int
// Values for UsageParagraphTextItemType
const (
UsageParagraphTextItemTypeUnknown UsageParagraphTextItemType = iota
UsageParagraphTextItemTypeText
UsageParagraphTextItemTypeVerbalIllustration
UsageParagraphTextItemTypeSeeAlso
)
// UsageParagraphTextItemTypeFromString returns a UsageParagraphTextItemType from its string ID
func UsageParagraphTextItemTypeFromString(id string) UsageParagraphTextItemType {
switch id {
case "text":
return UsageParagraphTextItemTypeText
case "vis":
return UsageParagraphTextItemTypeVerbalIllustration
case "uarefs":
return UsageParagraphTextItemTypeSeeAlso
default:
return UsageParagraphTextItemTypeUnknown
}
}
func (t UsageParagraphTextItemType) String() string {
return []string{"", "text", "vis", "uarefs"}[t]
}
// Contents returns a copied slice of the contents in the UsageParagraphText
func (upt UsageParagraphText) Contents() ([]UsageParagraphTextItem, error) {
items := []UsageParagraphTextItem{}
for _, el := range upt {
key, err := el.Key()
if err != nil {
return nil, err
}
typ := UsageParagraphTextItemTypeFromString(key)
switch typ {
case UsageParagraphTextItemTypeText:
var out string
err = el.UnmarshalValue(&out)
items = append(items, UsageParagraphTextItem{Type: typ, Text: &out})
case UsageParagraphTextItemTypeVerbalIllustration:
var out VerbalIllustration
err = el.UnmarshalValue(&out)
items = append(items, UsageParagraphTextItem{Type: typ, VerbalIllustration: &out})
case UsageParagraphTextItemTypeSeeAlso:
var out []UsageSeeAlso
err = el.UnmarshalValue(&out)
items = append(items, UsageParagraphTextItem{Type: typ, SeeAlso: out})
default:
err = errors.New("unknown element type in run-in")
}
}
return items, nil
}
// UsageParagraphTextItem is an item of the UsageParagraphText container
type UsageParagraphTextItem struct {
Type UsageParagraphTextItemType
Text *string
VerbalIllustration *VerbalIllustration
SeeAlso []UsageSeeAlso
}
// UsageSeeAlso is "uaref" here https://dictionaryapi.com/products/json#sec-2.usages
type UsageSeeAlso struct {
Reference string `json:"uaref,omitempty"`
}