Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
added cleaner to prevent null values from being output (#81)
Browse files Browse the repository at this point in the history
* added cleaner to prevent null values from being output

* updated api test case
  • Loading branch information
ojkelly authored Jul 31, 2018
1 parent f34139d commit c8e1df1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
19 changes: 15 additions & 4 deletions pkg/plugins/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,29 @@ func TestRegisterParser(t *testing.T) {
transform types.TemplateObject,
errors []error,
) {
resources = types.TemplateObject{
resources = make(types.TemplateObject)

resources[name] = types.TemplateObject{
"Name": name,
"Data": data,
}

return
},
output: apiTypes.PluginParserResult{
Conditions: types.TemplateObject{},
Metadata: types.TemplateObject{},
Mappings: types.TemplateObject{},
Outputs: types.TemplateObject{},
Parameters: types.TemplateObject{},
Resources: types.TemplateObject{
"Name": "TestName",
"Data": "TestData",
"TestName": map[string]interface{}{
"Name": "TestName",
"Data": "TestData",
},
},
Transform: types.TemplateObject{},
Errors: []error(nil),
},
},
}
Expand All @@ -101,6 +112,6 @@ func TestRegisterParser(t *testing.T) {
t.Error(err)
}

assert.Equal(pluginResult, test.output, fmt.Sprintf("Test %d", i))
assert.Equal(test.output, pluginResult, fmt.Sprintf("Test %d", i))
}
}
39 changes: 31 additions & 8 deletions pkg/plugins/api/marshalling.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package api
import (
"log"

"github.com/KablamoOSS/yaml"

pluginTypes "github.com/KablamoOSS/kombustion/pkg/plugins/api/types"
kombustionTypes "github.com/KablamoOSS/kombustion/types"

Expand All @@ -26,16 +28,16 @@ func marshallParserResult(
resources kombustionTypes.TemplateObject,
transform kombustionTypes.TemplateObject,
errors []error,

) (blob []byte) {

result := pluginTypes.PluginParserResult{
Conditions: conditions,
Metadata: metadata,
Mappings: mappings,
Outputs: outputs,
Parameters: parameters,
Resources: resources,
Transform: transform,
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)
Expand All @@ -44,3 +46,24 @@ func marshallParserResult(
}
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 {
if obj, err := yaml.Marshal(v); err == nil {
var tempObject kombustionTypes.TemplateObject
if err = yaml.Unmarshal(obj, &tempObject); err == nil {
result[k] = tempObject
}
}
}

return
}

0 comments on commit c8e1df1

Please sign in to comment.