-
Notifications
You must be signed in to change notification settings - Fork 0
/
synonyms.go
77 lines (67 loc) · 2.48 KB
/
synonyms.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
package types
import "github.com/pkg/errors"
// WithSynonyms is a compositing type for parsing the `syns` property
type WithSynonyms struct {
// https://dictionaryapi.com/products/json#sec-2.syns
SynonymParagraphs []SynonymParagraph `json:"syns,omitempty"`
}
// SynonymParagraph https://dictionaryapi.com/products/json#sec-2.syns
type SynonymParagraph struct {
Label string `json:"pl,omitempty"`
Text *SynonymParagraphText `json:"pt,omitempty"`
SeeAlsoRefs []string `json:"sarefs,omitempty"`
}
// SynonymParagraphText https://dictionaryapi.com/products/json#sec-2.syns
type SynonymParagraphText SequenceMapping
// SynonymParagraphTextItemType is an enum type for the types of items in the paragraph
type SynonymParagraphTextItemType int
// Values for SynonymParagraphTextItemType
const (
SynonymParagraphTextItemTypeUnknown SynonymParagraphTextItemType = iota
SynonymParagraphTextItemTypeText
SynonymParagraphTextItemTypeVerbalIllustration
)
// SynonymParagraphTextItemTypeFromString returns a SynonymParagraphTextItemType from its string ID
func SynonymParagraphTextItemTypeFromString(id string) SynonymParagraphTextItemType {
switch id {
case "text":
return SynonymParagraphTextItemTypeText
case "vis":
return SynonymParagraphTextItemTypeVerbalIllustration
default:
return SynonymParagraphTextItemTypeUnknown
}
}
func (t SynonymParagraphTextItemType) String() string {
return []string{"", "text", "vis"}[t]
}
// Contents returns a copied slice of the contents in the SynonymParagraphText
func (upt SynonymParagraphText) Contents() ([]SynonymParagraphTextItem, error) {
items := []SynonymParagraphTextItem{}
for _, el := range upt {
key, err := el.Key()
if err != nil {
return nil, err
}
typ := SynonymParagraphTextItemTypeFromString(key)
switch typ {
case SynonymParagraphTextItemTypeText:
var out string
err = el.UnmarshalValue(&out)
items = append(items, SynonymParagraphTextItem{Type: typ, Text: &out})
case SynonymParagraphTextItemTypeVerbalIllustration:
var out VerbalIllustration
err = el.UnmarshalValue(&out)
items = append(items, SynonymParagraphTextItem{Type: typ, VerbalIllustration: &out})
default:
err = errors.New("unknown item type in run-in")
}
}
return items, nil
}
// SynonymParagraphTextItem is an item of the SynonymParagraphText container
type SynonymParagraphTextItem struct {
Type SynonymParagraphTextItemType
Text *string
VerbalIllustration *VerbalIllustration
}