This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
/
signpost.go
95 lines (79 loc) · 2.19 KB
/
signpost.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
/*
Copyright (c) 2021 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package component
import (
"github.com/vmware-tanzu/octant/internal/util/json"
)
// Signpost is a component for a signpost
// +octant:component
type Signpost struct {
Base
Config SignpostConfig `json:"config"`
}
// SignpostConfig is the contents of a signpost
type SignpostConfig struct {
// Trigger is the component that will trigger the signpost.
Trigger Component `json:"trigger"`
// Message is the text that will be displayed.
Message string `json:"message"`
// Position of Signpost
Position Position `json:"position"`
}
type Position string
const (
PositionTopLeft Position = "top-left"
PositionTopMiddle Position = "top-middle"
PositionTopRight Position = "top-right"
PositionRightTop Position = "right-top"
PositionRightMiddle Position = "right-middle"
PositionRightBottom Position = "right-bottom"
PositionBottomRight Position = "bottom-right"
PositionBottomMiddle Position = "bottom-middle"
PositionBottomLeft Position = "bottom-left"
PositionLeftBottom Position = "left-bottom"
PositionLeftMiddle Position = "left-middle"
PositionLeftTop Position = "left-top"
)
func NewSignpost(t Component, m string) *Signpost {
so := &Signpost{
Base: newBase(TypeSignpost, nil),
Config: SignpostConfig{
Trigger: t,
Message: m,
},
}
return so
}
// SetPosition sets the status of the text component.
func (t *Signpost) SetPosition(position Position) {
t.Config.Position = position
}
type signpostMarshal Signpost
// MarshalJSON implements json.Marshaller
func (t *Signpost) MarshalJSON() ([]byte, error) {
m := signpostMarshal(*t)
m.Metadata.Type = TypeSignpost
return json.Marshal(&m)
}
func (c *SignpostConfig) UnmarshalJSON(data []byte) error {
x := struct {
Trigger *TypedObject `json:"trigger"`
Message string `json:"message,omitempty"`
Position Position `json:"position"`
}{}
if err := json.Unmarshal(data, &x); err != nil {
return err
}
if x.Trigger != nil {
trigger, err := x.Trigger.ToComponent()
if err != nil {
return err
}
c.Trigger = trigger
}
c.Message = x.Message
c.Position = x.Position
return nil
}