-
Notifications
You must be signed in to change notification settings - Fork 0
/
definingtext.go
136 lines (124 loc) · 4.21 KB
/
definingtext.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package types
import "github.com/pkg/errors"
// WithDefiningText is a compositing type for parsing the `dt` property
type WithDefiningText struct {
// https://dictionaryapi.com/products/json#sec-2.dt
DefiningText DefiningText `json:"dt,omitempty"`
}
// DefiningTextItemType is an enum type for the types of items in DefiningText
type DefiningTextItemType int
// Values for DefiningTextItemType
const (
DefiningTextItemTypeUnknown DefiningTextItemType = iota
DefiningTextItemTypeText
DefiningTextItemTypeBiography
DefiningTextItemTypeCalledAlso
DefiningTextItemTypeRunIn
DefiningTextItemTypeSupplementalInfo
DefiningTextItemTypeUsageNotes
DefiningTextItemTypeVerbalIllustrations
)
// DefiningTextItemTypeFromString returns a DefiningTextItemType from its string ID
func DefiningTextItemTypeFromString(id string) DefiningTextItemType {
switch id {
case "text":
return DefiningTextItemTypeText
case "bnw":
return DefiningTextItemTypeBiography
case "ca":
return DefiningTextItemTypeCalledAlso
case "ri":
return DefiningTextItemTypeRunIn
case "snote":
return DefiningTextItemTypeSupplementalInfo
case "uns":
return DefiningTextItemTypeUsageNotes
case "vis":
return DefiningTextItemTypeVerbalIllustrations
default:
return DefiningTextItemTypeUnknown
}
}
func (t DefiningTextItemType) String() string {
return []string{"", "text", "bnw", "ca", "ri", "snote", "uns", "vis"}[t]
}
// DefiningText https://dictionaryapi.com/products/json#sec-2.dt
type DefiningText SequenceMapping
// Contents returns a copied slice of the contents in the DefiningText
func (dt DefiningText) Contents() ([]DefiningTextItem, error) {
items := []DefiningTextItem{}
for _, el := range dt {
key, err := el.Key()
if err != nil {
return nil, err
}
typ := DefiningTextItemTypeFromString(key)
switch typ {
case DefiningTextItemTypeText:
var out string
err = el.UnmarshalValue(&out)
items = append(items, DefiningTextItem{Type: typ, Text: &out})
case DefiningTextItemTypeBiography:
var out Biography
err = el.UnmarshalValue(&out)
items = append(items, DefiningTextItem{Type: typ, Biography: &out})
case DefiningTextItemTypeCalledAlso:
var out CalledAlso
err = el.UnmarshalValue(&out)
items = append(items, DefiningTextItem{Type: typ, CalledAlso: &out})
case DefiningTextItemTypeRunIn:
var out RunIn
err = el.UnmarshalValue(&out)
items = append(items, DefiningTextItem{Type: typ, RunIn: &out})
case DefiningTextItemTypeSupplementalInfo:
var out SupplementalInfo
err = el.UnmarshalValue(&out)
items = append(items, DefiningTextItem{Type: typ, SupplementalInfo: &out})
case DefiningTextItemTypeUsageNotes:
var out []UsageNote
err = el.UnmarshalValue(&out)
items = append(items, DefiningTextItem{Type: typ, WithUsageNotes: WithUsageNotes{out}})
case DefiningTextItemTypeVerbalIllustrations:
var out []VerbalIllustration
err = el.UnmarshalValue(&out)
items = append(items, DefiningTextItem{Type: typ, WithVerbalIllustrations: WithVerbalIllustrations{out}})
default:
err = errors.New("unknown item type in defining text")
}
if err != nil {
return nil, err
}
}
return items, nil
}
// DefiningTextItem is an item in the DefiningText container.
// Type indicated which property is populated with data.
type DefiningTextItem struct {
Type DefiningTextItemType
Text *string
Biography *Biography
CalledAlso *CalledAlso
RunIn *RunIn
SupplementalInfo *SupplementalInfo
WithUsageNotes
WithVerbalIllustrations
}
// Biography https://dictionaryapi.com/products/json#sec-2.bnw
type Biography struct {
PersonalName string `json:"pname,omitempty"`
Surname string `json:"sname,omitempty"`
AlternateName string `json:"altname,omitempty"`
}
// CalledAlso https://dictionaryapi.com/products/json#sec-2.ca
type CalledAlso struct {
Intro string `json:"intro,omitempty"`
Targets []CalledAlsoTarget `json:"cats,omitempty"`
}
// CalledAlsoTarget https://dictionaryapi.com/products/json#sec-2.ca
type CalledAlsoTarget struct {
Text string `json:"cat,omitempty"`
TargetID string `json:"catref,omitempty"`
ParenthesizedNumber string `json:"pn,omitempty"`
WithPronounciations
WithParenthesizedSubjectStatusLabel
}