/
component.go
executable file
·191 lines (152 loc) · 4.54 KB
/
component.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
Copyright (c) 2019 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package component
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
)
// EmptyContentResponse is an empty content response.
var EmptyContentResponse = ContentResponse{}
// ContentResponse is a a content response. It contains a
// title and one or more components.
type ContentResponse struct {
Title []TitleComponent `json:"title,omitempty"`
Components []Component `json:"viewComponents"`
IconName string `json:"iconName,omitempty"`
IconSource string `json:"iconSource,omitempty"`
}
// NewContentResponse creates an instance of ContentResponse.
func NewContentResponse(title []TitleComponent) *ContentResponse {
return &ContentResponse{
Title: title,
}
}
// Add adds zero or more components to a content response.
func (c *ContentResponse) Add(components ...Component) {
c.Components = append(c.Components, components...)
}
// UnmarshalJSON unmarshals a content response from JSON.
func (c *ContentResponse) UnmarshalJSON(data []byte) error {
stage := struct {
Title []TypedObject `json:"title,omitempty"`
Components []TypedObject `json:"viewComponents,omitempty"`
}{}
if err := json.Unmarshal(data, &stage); err != nil {
return err
}
for _, t := range stage.Title {
title, err := getTitleByUnmarshalInterface(t.Config)
if err != nil {
return err
}
c.Title = Title(NewText(title))
}
for _, to := range stage.Components {
vc, err := to.ToComponent()
if err != nil {
return err
}
c.Components = append(c.Components, vc)
}
return nil
}
func getTitleByUnmarshalInterface(config json.RawMessage) (string, error) {
var objmap map[string]interface{}
if err := json.Unmarshal(config, &objmap); err != nil {
return "", err
}
if value, ok := objmap["value"].(string); ok {
return value, nil
}
return "", fmt.Errorf("title does not have a value")
}
type TypedObject struct {
Config json.RawMessage `json:"config,omitempty"`
Metadata Metadata `json:"metadata,omitempty"`
}
func (to *TypedObject) ToComponent() (Component, error) {
o, err := unmarshal(*to)
if err != nil {
return nil, err
}
vc, ok := o.(Component)
if !ok {
return nil, errors.Errorf("unable to convert %T to Component",
o)
}
return vc, nil
}
// Metadata collects common fields describing Components
type Metadata struct {
Type string `json:"type"`
Title []TitleComponent `json:"title,omitempty"`
Accessor string `json:"accessor,omitempty"`
}
// SetTitleText sets the title using text components.
func (m *Metadata) SetTitleText(parts ...string) {
var titleComponents []TitleComponent
for _, part := range parts {
titleComponents = append(titleComponents, NewText(part))
}
m.Title = titleComponents
}
func (m *Metadata) UnmarshalJSON(data []byte) error {
x := struct {
Type string `json:"type,omitempty"`
Title []TypedObject `json:"title,omitempty"`
}{}
if err := json.Unmarshal(data, &x); err != nil {
return err
}
m.Type = x.Type
for _, title := range x.Title {
vc, err := title.ToComponent()
if err != nil {
return errors.Wrap(err, "unmarshaling title")
}
tvc, ok := vc.(TitleComponent)
if !ok {
return errors.New("component in title isn't a title view component")
}
m.Title = append(m.Title, tvc)
}
return nil
}
// Component is a common interface for the data representation
// of visual components as rendered by the UI.
type Component interface {
json.Marshaler
// GetMetadata returns metadata for the component.
GetMetadata() Metadata
// SetAccessor sets the accessfor the component.
SetAccessor(string)
// IsEmpty returns true if the component is "empty".
IsEmpty() bool
// String returns a string representation of the component.
String() string
// LessThan returns true if the components value is less than the other value.
LessThan(other interface{}) bool
}
// TitleComponent is a view component that can be used for a title.
type TitleComponent interface {
Component
SupportsTitle()
}
// Title is a convenience method for creating a title.
func Title(components ...TitleComponent) []TitleComponent {
return components
}
// TitleFromString is a convenience method for create a title from a string.
func TitleFromString(s string) []TitleComponent {
return Title(NewText(s))
}
// TitleFromTitleComponent gets a title from a TitleComponent
func TitleFromTitleComponent(tc []TitleComponent) (string, error) {
if len(tc) != 1 {
return "", errors.New("exactly one title component can be converted")
}
return tc[0].String(), nil
}