This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
/
marshalling.go
80 lines (70 loc) · 2.09 KB
/
marshalling.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
package api
import (
"fmt"
"log"
"github.com/KablamoOSS/yaml"
pluginTypes "github.com/KablamoOSS/kombustion/pkg/plugins/api/types"
kombustionTypes "github.com/KablamoOSS/kombustion/types"
"github.com/vmihailenco/msgpack"
)
func marshallConfig(config pluginTypes.Config) (blob []byte) {
blob, err := msgpack.Marshal(&config)
if err != nil {
log.Fatal("Config marshalling err:", err)
}
return
}
func marshallParserResult(
conditions kombustionTypes.TemplateObject,
metadata kombustionTypes.TemplateObject,
mappings kombustionTypes.TemplateObject,
outputs kombustionTypes.TemplateObject,
parameters kombustionTypes.TemplateObject,
resources kombustionTypes.TemplateObject,
transform kombustionTypes.TemplateObject,
errors []error,
) (blob []byte) {
result := pluginTypes.PluginParserResult{
Conditions: cleanResult(conditions),
Metadata: cleanResult(metadata),
Mappings: cleanResult(mappings),
Outputs: cleanResult(outputs),
Parameters: cleanResult(parameters),
Resources: cleanResult(resources),
Transform: cleanResult(transform),
Errors: errors,
}
blob, err := msgpack.Marshal(&result)
if err != nil {
log.Fatal("Resource marshalling err:", err)
}
return
}
// Clean the templateObject by parsing to YAML and back
// This needs to happen before being marshalled to binary (msgpack)
// because the tags on the struct's are not stored in msgpack,
// therefore the omitempty directive is lost
//
// This prevents null values being output
func cleanResult(objects kombustionTypes.TemplateObject) (result kombustionTypes.TemplateObject) {
result = make(kombustionTypes.TemplateObject)
for k, v := range objects {
// We need to check the value is not empty, to prevent a nil pointer in the yaml.Marshal
// it will be empty when the user leaves a key blank in their template
if v != nil {
obj, err := yaml.Marshal(v)
if err == nil {
var tempObject kombustionTypes.TemplateObject
err = yaml.Unmarshal(obj, &tempObject)
if err == nil {
result[k] = tempObject
} else {
fmt.Println(err)
}
} else {
fmt.Println(err)
}
}
}
return
}