forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelationship_resource.go
89 lines (72 loc) · 1.86 KB
/
relationship_resource.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
package resources
import (
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
)
// Relationships represent associations between resources. Relationships is a
// map of RelationshipTypes to Relationship.
type Relationships map[constant.RelationshipType]Relationship
// Relationship represents a one to one relationship.
// An empty GUID will be marshaled as `null`.
type Relationship struct {
GUID string
}
func (r Relationship) MarshalJSON() ([]byte, error) {
if r.GUID == "" {
var emptyCCRelationship struct {
Data interface{} `json:"data"`
}
return json.Marshal(emptyCCRelationship)
}
var ccRelationship struct {
Data struct {
GUID string `json:"guid"`
} `json:"data"`
}
ccRelationship.Data.GUID = r.GUID
return json.Marshal(ccRelationship)
}
func (r *Relationship) UnmarshalJSON(data []byte) error {
var ccRelationship struct {
Data struct {
GUID string `json:"guid"`
} `json:"data"`
}
err := cloudcontroller.DecodeJSON(data, &ccRelationship)
if err != nil {
return err
}
r.GUID = ccRelationship.Data.GUID
return nil
}
// RelationshipList represents a one to many relationship.
type RelationshipList struct {
GUIDs []string
}
func (r RelationshipList) MarshalJSON() ([]byte, error) {
var ccRelationship struct {
Data []map[string]string `json:"data"`
}
for _, guid := range r.GUIDs {
ccRelationship.Data = append(
ccRelationship.Data,
map[string]string{
"guid": guid,
})
}
return json.Marshal(ccRelationship)
}
func (r *RelationshipList) UnmarshalJSON(data []byte) error {
var ccRelationships struct {
Data []map[string]string `json:"data"`
}
err := cloudcontroller.DecodeJSON(data, &ccRelationships)
if err != nil {
return err
}
for _, partner := range ccRelationships.Data {
r.GUIDs = append(r.GUIDs, partner["guid"])
}
return nil
}