-
Notifications
You must be signed in to change notification settings - Fork 5
/
decoder.go
218 lines (171 loc) · 4.88 KB
/
decoder.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package dsapi
import (
"github.com/MerlinDMC/dsapid"
"github.com/MerlinDMC/dsapid/converter"
"github.com/MerlinDMC/dsapid/storage"
"path"
)
type dsapiDecoder struct {
provider dsapid.SyncProvider
users storage.UserStorage
}
func NewDecoder(provider dsapid.SyncProvider, users storage.UserStorage) (decoder *dsapiDecoder, err error) {
decoder = new(dsapiDecoder)
if provider != "" {
decoder.provider = provider
} else {
decoder.provider = dsapid.SyncProviderCommunity
}
decoder.users = users
return decoder, nil
}
func (me *dsapiDecoder) Decode(data dsapid.Table) *dsapid.ManifestResource {
var manifest *dsapid.ManifestResource
manifest = new(dsapid.ManifestResource)
manifest.Options = make(dsapid.Table)
manifest.SyncInfo = make(dsapid.Table)
manifest.BuilderInfo = make(dsapid.Table)
manifest.MetadataInfo = make([]dsapid.Table, 0)
var creator_uuid string = dsapid.DefaultUserUuid
var creator_name string = dsapid.DefaultUserName
if v, ok := data["creator_uuid"]; ok && v.(string) != "" {
creator_uuid = v.(string)
// } else {
// logger.Warn("manifest has no creator_uuid")
}
if v, ok := data["creator_name"]; ok && v.(string) != "" {
creator_name = v.(string)
// } else {
// logger.Warn("manifest has no creator_name")
}
user := me.users.EnsureExists(creator_uuid, creator_name)
manifest.Owner = user.Uuid
if v, ok := data["provider"]; ok {
manifest.Provider = dsapid.SyncProvider(v.(string))
} else {
if user.Provider != "" {
manifest.Provider = user.Provider
} else {
manifest.Provider = me.provider
}
}
if v, ok := data["uuid"]; ok {
manifest.Uuid = v.(string)
}
if v, ok := data["name"]; ok {
manifest.Name = v.(string)
}
if v, ok := data["version"]; ok {
manifest.Version = v.(string)
}
if v, ok := data["description"]; ok {
manifest.Description = v.(string)
}
if v, ok := data["type"]; ok {
manifest.Type = dsapid.ManifestType(v.(string))
}
if v, ok := data["os"]; ok {
manifest.Os = v.(string)
}
if v, ok := data["requirements"]; ok {
manifest.Requirements = dsapid.Table(v.(map[string]interface{}))
}
if v, ok := data["tags"]; ok {
manifest.Tags = dsapid.Table(v.(map[string]interface{}))
}
if v, ok := data["users"]; ok {
for _, u := range v.([]interface{}) {
manifest.Users = append(manifest.Users, dsapid.Table(u.(map[string]interface{})))
}
}
if v, ok := data["public"]; ok {
manifest.Public = v.(bool)
} else {
manifest.Public = true
}
if v, ok := data["disabled"]; ok {
manifest.Disabled = v.(bool)
} else {
manifest.Disabled = false
}
if manifest.Disabled {
manifest.State = dsapid.ManifestStateDisabled
} else {
manifest.State = dsapid.ManifestStateActive
}
if v, ok := data["homepage"]; ok {
manifest.Homepage = v.(string)
}
if v, ok := data["urn"]; ok {
manifest.Urn = v.(string)
} else {
manifest.Urn = converter.ComputeUrn(manifest)
}
if v, ok := data["options"]; ok {
manifest.Options = dsapid.Table(v.(map[string]interface{}))
}
if manifest.Type == dsapid.ManifestTypeZvol {
if v, ok := data["nic_driver"]; ok {
manifest.Options["nic_driver"] = v.(string)
}
if v, ok := data["disk_driver"]; ok {
manifest.Options["disk_driver"] = v.(string)
}
if v, ok := data["cpu_type"]; ok {
manifest.Options["cpu_type"] = v.(string)
}
if v, ok := data["image_size"]; ok {
manifest.Options["image_size"] = converter.DecodeToInt64(v)
}
}
if v, ok := data["published_at"]; ok {
if dt, err := converter.ParseDateTime(v.(string)); err == nil {
manifest.PublishedAt = dt
}
}
if v, ok := data["created_at"]; ok {
if dt, err := converter.ParseDateTime(v.(string)); err == nil {
manifest.CreatedAt = dt
} else {
manifest.CreatedAt = manifest.PublishedAt
}
} else {
manifest.CreatedAt = manifest.PublishedAt
}
decode_file := func(data dsapid.Table) (file dsapid.ManifestFileResource) {
file.Compression = dsapid.CompressionTypeNone
if v, ok := data["size"]; ok {
file.Size = converter.DecodeToInt64(v)
}
if v, ok := data["path"]; ok {
file.Path = v.(string)
}
if v, ok := data["md5"]; ok {
file.Md5 = v.(string)
}
if v, ok := data["sha1"]; ok {
file.Sha1 = v.(string)
}
if ext := path.Ext(file.Path); ext != "" {
if v, ok := dsapid.CompressionExtensionMap[ext[1:]]; ok {
file.Compression = v
}
}
return file
}
if v, ok := data["files"]; ok {
for _, u := range v.([]interface{}) {
manifest.Files = append(manifest.Files, decode_file(dsapid.Table(u.(map[string]interface{}))))
}
}
// internal extra stuff which might only be accessible if we sync from another instance of ourselve
if v, ok := data["builder_info"]; ok {
manifest.BuilderInfo = dsapid.Table(v.(map[string]interface{}))
}
if v, ok := data["metadata_info"]; ok {
for _, m := range v.([]interface{}) {
manifest.MetadataInfo = append(manifest.MetadataInfo, dsapid.Table(m.(map[string]interface{})))
}
}
return manifest
}