Skip to content

Commit 072fc74

Browse files
authored
fix(schema, parser): change Transform json schema to allow multiple macros (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
1 parent ed25c12 commit 072fc74

7 files changed

Lines changed: 94 additions & 7 deletions

File tree

cloudformation/template.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ func (t *Transform) UnmarshalJSON(b []byte) error {
134134

135135
case []string:
136136
t.StringArray = &val
137+
138+
case []interface{}:
139+
var strslice []string
140+
for _, i := range val {
141+
switch str := i.(type) {
142+
case string:
143+
strslice = append(strslice, str)
144+
}
145+
}
146+
t.StringArray = &strslice
137147
}
138148

139149
return nil

generate/templates/schema.template

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
"{{.ResourceSpecificationTransform}}"
2828
]
2929
{{else}}
30-
"type": ["object","string"]
30+
"oneOf": [
31+
{"type": ["string"]},
32+
{"type": "array", "items": {"type": "string"}}
33+
]
3134
{{end}}
3235
},
3336

goformation_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,42 @@ var _ = Describe("Goformation", func() {
758758

759759
})
760760

761+
Context("with a YAML template with single transform macro", func() {
762+
template, err := goformation.Open("test/yaml/transform-single.yaml")
763+
764+
It("should parse the template successfully", func() {
765+
Expect(template).ToNot(BeNil())
766+
Expect(err).To(BeNil())
767+
})
768+
769+
It("should parse transform macro into String field", func() {
770+
Expect(*template.Transform.String).To(Equal("MyTranformMacro"))
771+
})
772+
773+
It("should StringArray remain nil", func() {
774+
Expect(template.Transform.StringArray).To(BeNil())
775+
})
776+
777+
})
778+
779+
Context("with a YAML template with multiple transform macros", func() {
780+
template, err := goformation.Open("test/yaml/transform-multiple.yaml")
781+
782+
It("should parse the template successfully", func() {
783+
Expect(template).ToNot(BeNil())
784+
Expect(err).To(BeNil())
785+
})
786+
787+
It("should parse transform macro into StringArray field", func() {
788+
Expect(*template.Transform.StringArray).To(Equal([]string{"FirstMacro", "SecondMacro"}))
789+
})
790+
791+
It("should String remain nil", func() {
792+
Expect(template.Transform.String).To(BeNil())
793+
})
794+
795+
})
796+
761797
Context("with a YAML template with paramter overrides", func() {
762798

763799
template, err := goformation.OpenWithOptions("test/yaml/aws-serverless-function-env-vars.yaml", &intrinsics.ProcessorOptions{

schema/cloudformation.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60700,9 +60700,18 @@ var CloudformationSchema = `{
6070060700
"type": "object"
6070160701
},
6070260702
"Transform": {
60703-
"type": [
60704-
"object",
60705-
"string"
60703+
"oneOf": [
60704+
{
60705+
"type": [
60706+
"string"
60707+
]
60708+
},
60709+
{
60710+
"items": {
60711+
"type": "string"
60712+
},
60713+
"type": "array"
60714+
}
6070660715
]
6070760716
}
6070860717
},

schema/cloudformation.schema.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60697,9 +60697,18 @@
6069760697
"type": "object"
6069860698
},
6069960699
"Transform": {
60700-
"type": [
60701-
"object",
60702-
"string"
60700+
"oneOf": [
60701+
{
60702+
"type": [
60703+
"string"
60704+
]
60705+
},
60706+
{
60707+
"items": {
60708+
"type": "string"
60709+
},
60710+
"type": "array"
60711+
}
6070360712
]
6070460713
}
6070560714
},

test/yaml/transform-multiple.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
AWSTemplateFormatVersion: 2010-09-09
2+
3+
Resources:
4+
ExampleTopic:
5+
Type: AWS::SNS::Topic
6+
Properties:
7+
TopicName: example
8+
9+
Transform:
10+
- FirstMacro
11+
- SecondMacro

test/yaml/transform-single.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
AWSTemplateFormatVersion: 2010-09-09
2+
3+
Resources:
4+
ExampleTopic:
5+
Type: AWS::SNS::Topic
6+
Properties:
7+
TopicName: example
8+
9+
Transform: MyTranformMacro

0 commit comments

Comments
 (0)