This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 488
/
conversion.go
178 lines (144 loc) · 4.76 KB
/
conversion.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
/*
Copyright (c) 2019 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package plugin
import (
"github.com/vmware-tanzu/octant/internal/util/json"
"k8s.io/apimachinery/pkg/runtime/schema"
"github.com/vmware-tanzu/octant/pkg/navigation"
"github.com/vmware-tanzu/octant/pkg/plugin/dashboard"
"github.com/vmware-tanzu/octant/pkg/view/component"
)
func convertToCapabilities(in *dashboard.RegisterResponse_Capabilities) Capabilities {
if in == nil {
return Capabilities{}
}
c := Capabilities{
SupportsPrinterStatus: convertToGroupVersionKindList(in.SupportsPrinterStatus),
SupportsPrinterConfig: convertToGroupVersionKindList(in.SupportsPrinterConfig),
SupportsPrinterItems: convertToGroupVersionKindList(in.SupportsPrinterItems),
SupportsObjectStatus: convertToGroupVersionKindList(in.SupportsObjectStatus),
SupportsTab: convertToGroupVersionKindList(in.SupportsTab),
IsModule: in.IsModule,
ActionNames: in.ActionNames,
}
return c
}
func convertFromCapabilities(in Capabilities) *dashboard.RegisterResponse_Capabilities {
c := dashboard.RegisterResponse_Capabilities{
SupportsPrinterStatus: convertFromGroupVersionKindList(in.SupportsObjectStatus),
SupportsPrinterConfig: convertFromGroupVersionKindList(in.SupportsPrinterConfig),
SupportsPrinterItems: convertFromGroupVersionKindList(in.SupportsPrinterItems),
SupportsObjectStatus: convertFromGroupVersionKindList(in.SupportsObjectStatus),
SupportsTab: convertFromGroupVersionKindList(in.SupportsTab),
IsModule: in.IsModule,
ActionNames: in.ActionNames,
}
return &c
}
func convertToGroupVersionKindList(in []*dashboard.RegisterResponse_GroupVersionKind) []schema.GroupVersionKind {
var list []schema.GroupVersionKind
for i := range in {
list = append(list, convertToGroupVersionKind(in[i]))
}
return list
}
func convertFromGroupVersionKindList(in []schema.GroupVersionKind) []*dashboard.RegisterResponse_GroupVersionKind {
var list []*dashboard.RegisterResponse_GroupVersionKind
for i := range in {
item := convertFromGroupVersionKind(in[i])
list = append(list, &item)
}
return list
}
func convertToGroupVersionKind(in *dashboard.RegisterResponse_GroupVersionKind) schema.GroupVersionKind {
return schema.GroupVersionKind{
Group: in.Group,
Version: in.Version,
Kind: in.Kind,
}
}
func convertFromGroupVersionKind(in schema.GroupVersionKind) dashboard.RegisterResponse_GroupVersionKind {
return dashboard.RegisterResponse_GroupVersionKind{
Group: in.Group,
Version: in.Version,
Kind: in.Kind,
}
}
func convertToNavigation(in *dashboard.NavigationResponse_Navigation) navigation.Navigation {
if in == nil {
return navigation.Navigation{}
}
out := navigation.Navigation{
Title: in.Title,
Path: in.Path,
IconName: in.IconName,
CustomSvg: in.CustomSvg,
}
for _, child := range in.Children {
converted := convertToNavigation(child)
out.Children = append(out.Children, converted)
}
return out
}
func convertFromNavigation(in navigation.Navigation) *dashboard.NavigationResponse_Navigation {
out := dashboard.NavigationResponse_Navigation{
Title: in.Title,
Path: in.Path,
IconName: in.IconName,
CustomSvg: in.CustomSvg,
}
for _, child := range in.Children {
converted := convertFromNavigation(child)
out.Children = append(out.Children, converted)
}
return &out
}
func convertToSummarySections(in []*dashboard.PrintResponse_SummaryItem) ([]component.SummarySection, error) {
var list []component.SummarySection
for _, item := range in {
converted, err := convertToSummarySection(item)
if err != nil {
return nil, err
}
list = append(list, converted)
}
return list, nil
}
func convertFromSummarySections(in []component.SummarySection) ([]*dashboard.PrintResponse_SummaryItem, error) {
var list []*dashboard.PrintResponse_SummaryItem
for _, section := range in {
converted, err := convertFromSummarySection(section)
if err != nil {
return nil, err
}
list = append(list, converted)
}
return list, nil
}
func convertToSummarySection(in *dashboard.PrintResponse_SummaryItem) (component.SummarySection, error) {
var typedObject component.TypedObject
err := json.Unmarshal(in.Component, &typedObject)
if err != nil {
return component.SummarySection{}, err
}
view, err := typedObject.ToComponent()
if err != nil {
return component.SummarySection{}, err
}
return component.SummarySection{
Header: in.Header,
Content: view,
}, nil
}
func convertFromSummarySection(in component.SummarySection) (*dashboard.PrintResponse_SummaryItem, error) {
data, err := json.Marshal(in.Content)
if err != nil {
return nil, err
}
return &dashboard.PrintResponse_SummaryItem{
Header: in.Header,
Component: data,
}, nil
}