-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
component.go
111 lines (91 loc) · 2.91 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
package keygen
import (
"time"
"github.com/keygen-sh/jsonapi-go"
)
type component struct {
ID string `json:"-"`
Type string `json:"-"`
Fingerprint string `json:"fingerprint"`
Name string `json:"name"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
MachineID string `json:"-"`
}
// GetID implements the jsonapi.MarshalResourceIdentifier interface.
func (c component) GetID() string {
return c.ID
}
// GetType implements the jsonapi.MarshalResourceIdentifier interface.
func (c component) GetType() string {
return "components"
}
// GetData implements the jsonapi.MarshalData interface.
func (c component) GetData() interface{} {
return c
}
// GetRelationships implements jsonapi.MarshalRelationships interface.
func (c component) GetRelationships() map[string]interface{} {
relationships := make(map[string]interface{})
if c.MachineID != "" {
relationships["machine"] = jsonapi.ResourceObjectIdentifier{
Type: "machines",
ID: c.MachineID,
}
}
if len(relationships) == 0 {
return nil
}
return relationships
}
// Component represents a Keygen component object.
type Component struct {
ID string `json:"-"`
Type string `json:"-"`
Fingerprint string `json:"fingerprint"`
Name string `json:"name"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
Metadata map[string]interface{} `json:"metadata"`
MachineID string `json:"-"`
}
// GetID implements the jsonapi.MarshalResourceIdentifier interface.
func (c Component) GetID() string {
return c.ID
}
// GetType implements the jsonapi.MarshalResourceIdentifier interface.
func (c Component) GetType() string {
return "components"
}
// GetData implements the jsonapi.MarshalData interface.
func (c Component) GetData() interface{} {
// Transform public component to private component to only send a subset of attrs
return component{
Fingerprint: c.Fingerprint,
Name: c.Name,
Metadata: c.Metadata,
MachineID: c.MachineID,
}
}
func (c Component) UseExperimentalEmbeddedRelationshipData() bool {
return true
}
// SetID implements the jsonapi.UnmarshalResourceIdentifier interface.
func (c *Component) SetID(id string) error {
c.ID = id
return nil
}
// SetType implements the jsonapi.UnmarshalResourceIdentifier interface.
func (c *Component) SetType(t string) error {
c.Type = t
return nil
}
// SetData implements the jsonapi.UnmarshalData interface.
func (c *Component) SetData(to func(target interface{}) error) error {
return to(c)
}
// Components represents an array of component objects.
type Components []Component
// SetData implements the jsonapi.UnmarshalData interface.
func (c *Components) SetData(to func(target interface{}) error) error {
return to(c)
}