/
link.go
74 lines (59 loc) · 1.44 KB
/
link.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
/*
Copyright (c) 2019 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package component
import "encoding/json"
// Link is a text component that contains a link.
type Link struct {
base
Config LinkConfig `json:"config"`
}
// LinkConfig is the contents of Link
type LinkConfig struct {
Text string `json:"value"`
Ref string `json:"ref"`
}
// NewLink creates a link component
func NewLink(title, s, ref string) *Link {
return &Link{
base: newBase(typeLink, TitleFromString(title)),
Config: LinkConfig{
Text: s,
Ref: ref,
},
}
}
// SupportsTitle designates this is a TextComponent.
func (t *Link) SupportsTitle() {}
// GetMetadata accesses the components metadata. Implements Component.
func (t *Link) GetMetadata() Metadata {
return t.Metadata
}
// Text returns the link's text.
func (t *Link) Text() string {
return t.Config.Text
}
// Ref returns the link's ref.
func (t *Link) Ref() string {
return t.Config.Ref
}
type linkMarshal Link
// MarshalJSON implements json.Marshaler
func (t *Link) MarshalJSON() ([]byte, error) {
m := linkMarshal(*t)
m.Metadata.Type = typeLink
return json.Marshal(&m)
}
// String returns the link's text.
func (t *Link) String() string {
return t.Config.Text
}
// LessThan returns true if this component's value is less than the argument supplied.
func (t *Link) LessThan(i interface{}) bool {
v, ok := i.(*Link)
if !ok {
return false
}
return t.Config.Text < v.Config.Text
}