/
resource.go
199 lines (174 loc) · 5.48 KB
/
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
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
192
193
194
195
196
197
198
199
// Copyright (C) 2015 NTT Innovation Institute, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package schema
import (
"encoding/json"
"fmt"
)
//Tags are additional metadata for resources
type Tags map[string]string // Tags for each resource
//TODO(nati)_define proper error struct.
// Stop using fmt.Errorf
//Resource is a instance of resource
type Resource struct {
parentID string
schema *Schema
tags Tags
properties map[string]interface{}
}
//ID gets id from resource
func (resource *Resource) ID() string {
id, _ := resource.properties["id"]
idString := fmt.Sprint(id)
return idString
}
//Get gets property from resource
func (resource *Resource) Get(key string) interface{} {
return resource.properties[key]
}
//ParentID get parent id of the resource
func (resource *Resource) ParentID() string {
return resource.parentID
}
//SetParentID set parent id of the resource
func (resource *Resource) SetParentID(id string) {
schema := resource.schema
if schema.Parent != "" {
resource.properties[schema.Parent+"_id"] = id
resource.parentID = id
}
}
//Path generate path for this resource
func (resource *Resource) Path() string {
s := resource.Schema()
key := s.URL + "/" + resource.ID()
return key
}
//Data gets data from resource
func (resource *Resource) Data() map[string]interface{} {
return resource.properties
}
//JSONString returns json string of the resource
func (resource *Resource) JSONString() (string, error) {
bytes, err := json.Marshal(resource.Data())
return string(bytes), err
}
//Schema gets schema from resource
func (resource *Resource) Schema() *Schema {
return resource.schema
}
//Values returns list of values
func (resource *Resource) Values() []interface{} {
var values []interface{}
schema := resource.schema
data := resource.properties
for _, attr := range schema.Properties {
values = append(values, data[attr.ID])
}
return values
}
//NewResource is a constructor for a resource
func NewResource(schema *Schema, properties map[string]interface{}) (*Resource, error) {
resource := &Resource{
schema: schema,
properties: properties,
}
resource.tags = make(Tags)
if schema.Parent != "" {
parentID, ok := properties[schema.Parent+"_id"]
if ok {
parentIDStr, _ := parentID.(string)
resource.SetParentID(parentIDStr)
}
}
return resource, nil
}
//String return string form representation
func (resource *Resource) String() string {
return fmt.Sprintf("%v", resource.properties)
}
//Update resource data
func (resource *Resource) Update(updateData map[string]interface{}) error {
data := resource.properties
for _, property := range resource.schema.Properties {
id := property.ID
if val, ok := updateData[id]; ok {
data[id] = updateResourceRecursion(val, data[id])
}
}
return nil
}
//Data already validated
func updateResourceRecursion(updateData interface{}, sourceData interface{}) interface{} {
if sourceData == nil {
return updateData
}
switch newUpdate := updateData.(type) {
case map[string]interface{}:
newSource := sourceData.(map[string]interface{})
for key, val := range newUpdate {
newSource[key] = updateResourceRecursion(val, newSource[key])
}
return newSource
default:
return updateData
}
}
func fillObjectDefaults(objectProperty Property, resourceMap, objectMask map[string]interface{}) {
for objectPropertyID, objectPropertyIface := range objectProperty.Properties {
objectPropertyMap := objectPropertyIface.(map[string]interface{})
innerProperty := NewPropertyFromObj(objectPropertyID, objectPropertyMap, false)
if objectMaskInnerProperty, ok := objectMask[objectPropertyID]; ok {
resourceFilledProperty, resourceFilled := resourceMap[objectPropertyID]
if innerProperty.Type == "object" {
innerPropertyMaskMap := objectMaskInnerProperty.(map[string]interface{})
if resourceFilled {
fillObjectDefaults(*innerProperty, resourceFilledProperty.(map[string]interface{}), innerPropertyMaskMap)
} else {
if innerPropertyMaskMap != nil && innerProperty.Default != nil {
resourceMap[objectPropertyID] = innerPropertyMaskMap
}
}
} else {
if !resourceFilled {
resourceMap[objectPropertyID] = objectMaskInnerProperty
}
}
}
}
}
//PopulateDefaults Populates not provided data with defaults
func (resource *Resource) PopulateDefaults() error {
for _, property := range resource.Schema().Properties {
defaultValueMask := property.getDefaultMask()
resourceProperty, propertyFilled := resource.properties[property.ID]
if defaultValueMask != nil {
if property.Type == "object" {
defaultValueMaskMap := defaultValueMask.(map[string]interface{})
if propertyFilled && resourceProperty != nil {
resourceMap := resourceProperty.(map[string]interface{})
fillObjectDefaults(property, resourceMap, defaultValueMaskMap)
} else if defaultValueMaskMap != nil {
resource.properties[property.ID] = defaultValueMaskMap
}
} else {
if !propertyFilled {
resource.properties[property.ID] = defaultValueMask
}
}
}
}
return nil
}