Skip to content

Commit

Permalink
fix(schema, parser): change Transform json schema to allow multiple m…
Browse files Browse the repository at this point in the history
…acros (awslabs#268)

Transform field can be String or Array of Strings, this fixes json schema and UnmarshalJSON.
UnmarshalJSON needs fix because it receives []interface{} instead of []string. Added additional case to handle this issue.

fixes awslabs#267
  • Loading branch information
shivas authored Feb 14, 2020
1 parent ed25c12 commit 072fc74
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 7 deletions.
10 changes: 10 additions & 0 deletions cloudformation/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ func (t *Transform) UnmarshalJSON(b []byte) error {

case []string:
t.StringArray = &val

case []interface{}:
var strslice []string
for _, i := range val {
switch str := i.(type) {
case string:
strslice = append(strslice, str)
}
}
t.StringArray = &strslice
}

return nil
Expand Down
5 changes: 4 additions & 1 deletion generate/templates/schema.template
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
"{{.ResourceSpecificationTransform}}"
]
{{else}}
"type": ["object","string"]
"oneOf": [
{"type": ["string"]},
{"type": "array", "items": {"type": "string"}}
]
{{end}}
},

Expand Down
36 changes: 36 additions & 0 deletions goformation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,42 @@ var _ = Describe("Goformation", func() {

})

Context("with a YAML template with single transform macro", func() {
template, err := goformation.Open("test/yaml/transform-single.yaml")

It("should parse the template successfully", func() {
Expect(template).ToNot(BeNil())
Expect(err).To(BeNil())
})

It("should parse transform macro into String field", func() {
Expect(*template.Transform.String).To(Equal("MyTranformMacro"))
})

It("should StringArray remain nil", func() {
Expect(template.Transform.StringArray).To(BeNil())
})

})

Context("with a YAML template with multiple transform macros", func() {
template, err := goformation.Open("test/yaml/transform-multiple.yaml")

It("should parse the template successfully", func() {
Expect(template).ToNot(BeNil())
Expect(err).To(BeNil())
})

It("should parse transform macro into StringArray field", func() {
Expect(*template.Transform.StringArray).To(Equal([]string{"FirstMacro", "SecondMacro"}))
})

It("should String remain nil", func() {
Expect(template.Transform.String).To(BeNil())
})

})

Context("with a YAML template with paramter overrides", func() {

template, err := goformation.OpenWithOptions("test/yaml/aws-serverless-function-env-vars.yaml", &intrinsics.ProcessorOptions{
Expand Down
15 changes: 12 additions & 3 deletions schema/cloudformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -60700,9 +60700,18 @@ var CloudformationSchema = `{
"type": "object"
},
"Transform": {
"type": [
"object",
"string"
"oneOf": [
{
"type": [
"string"
]
},
{
"items": {
"type": "string"
},
"type": "array"
}
]
}
},
Expand Down
15 changes: 12 additions & 3 deletions schema/cloudformation.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -60697,9 +60697,18 @@
"type": "object"
},
"Transform": {
"type": [
"object",
"string"
"oneOf": [
{
"type": [
"string"
]
},
{
"items": {
"type": "string"
},
"type": "array"
}
]
}
},
Expand Down
11 changes: 11 additions & 0 deletions test/yaml/transform-multiple.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
AWSTemplateFormatVersion: 2010-09-09

Resources:
ExampleTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: example

Transform:
- FirstMacro
- SecondMacro
9 changes: 9 additions & 0 deletions test/yaml/transform-single.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
AWSTemplateFormatVersion: 2010-09-09

Resources:
ExampleTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: example

Transform: MyTranformMacro

0 comments on commit 072fc74

Please sign in to comment.