Skip to content

Commit

Permalink
feat(schema): Support generation of array items that should be combin…
Browse files Browse the repository at this point in the history
…ed in one anyOf
  • Loading branch information
jpinkney-aws authored and rubenfonseca committed Apr 25, 2022
1 parent e8229a9 commit d5e468f
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 62 deletions.
2 changes: 2 additions & 0 deletions generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,9 @@ func generatePolymorphicProperty(typename string, name string, property Property

types := append([]string{}, property.PrimitiveTypes...)
types = append(types, property.PrimitiveItemTypes...)
types = append(types, property.InclusivePrimitiveItemTypes...)
types = append(types, property.ItemTypes...)
types = append(types, property.InclusiveItemTypes...)
types = append(types, property.Types...)

packageName, err := packageName(typename, true)
Expand Down
26 changes: 25 additions & 1 deletion generate/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ type Property struct {
// resource type documentation.
UpdateType string `json:"UpdateType"`

// InclusivePrimitiveItemTypes - items of an array that must be grouped together with other InclusivePrimitiveItemTypes
// and InclusiveItemTypes. If the value of the Type field is List or Map, indicates the type of list or map
// if they contain primitive types. Otherwise, this field is omitted. Valid types are the same as primitive item
// types String, Long, Integer, Double, Boolean, or Timestamp.
//
// This will allow a single array to contain multiple item types rather than one type per array instance
InclusivePrimitiveItemTypes []string `json:"InclusivePrimitiveItemTypes"`

// InclusiveItemTypes - items of an array that must be grouped together with other InclusiveItemTypes
// and InclusivePrimitiveItemTypes. If the value of the Type field is List or Map, indicates the type of list or
// map if they contain non-primitive types. Otherwise, this field is omitted.
//
// A subproperty name is a valid item type. For example, if the type value is List and the item type
// value is PortMapping, you can specify a list of port mapping properties.
//
// This will allow a single array to contain multiple item types rather than one type per array instance
InclusiveItemTypes []string `json:"InclusiveItemTypes"`

// Types - if a property can be different types, they will be listed here
PrimitiveTypes []string `json:"PrimitiveTypes"`
PrimitiveItemTypes []string `json:"PrimitiveItemTypes"`
Expand Down Expand Up @@ -185,7 +203,13 @@ func (p Property) HasValidType() bool {

// IsPolymorphic checks whether a property can be multiple different types
func (p Property) IsPolymorphic() bool {
return len(p.PrimitiveTypes) > 0 || len(p.PrimitiveItemTypes) > 0 || len(p.PrimitiveItemTypes) > 0 || len(p.ItemTypes) > 0 || len(p.Types) > 0
return len(p.PrimitiveTypes) > 0 ||
len(p.PrimitiveItemTypes) > 0 ||
len(p.PrimitiveItemTypes) > 0 ||
len(p.ItemTypes) > 0 ||
len(p.Types) > 0 ||
len(p.InclusivePrimitiveItemTypes) > 0 ||
len(p.InclusiveItemTypes) > 0
}

// IsPrimitive checks whether a property is a primitive type
Expand Down
16 changes: 8 additions & 8 deletions generate/sam-2016-10-31.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@
"PrimitiveTypes": [
"String"
],
"PrimitiveItemTypes": [
"String"
],
"Types": [
"IAMPolicyDocument"
],
"ItemTypes": [
"InclusivePrimitiveItemTypes": [
"String"
],
"InclusiveItemTypes": [
"IAMPolicyDocument",
"SAMPolicyTemplate"
],
Expand Down Expand Up @@ -705,13 +705,13 @@
"PrimitiveTypes": [
"String"
],
"PrimitiveItemTypes": [
"String"
],
"Types": [
"IAMPolicyDocument"
],
"ItemTypes": [
"InclusivePrimitiveItemTypes": [
"String"
],
"InclusiveItemTypes": [
"IAMPolicyDocument",
"SAMPolicyTemplate"
],
Expand Down
31 changes: 30 additions & 1 deletion generate/templates/polymorphic-property.template
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ type {{.Name}} struct {
{{range $type := $.Property.PrimitiveItemTypes}}
{{$type}}Array *[]{{convertToGoType $type}}{{end}}

{{range $type := $.Property.InclusivePrimitiveItemTypes}}
{{$type}}Array *[]{{convertToGoType $type}}{{end}}

{{range $type := $.Property.Types}}
{{$type}} *{{$.Basename}}_{{$type}}{{end}}

{{range $type := $.Property.ItemTypes}}
{{$type}}Array *[]{{$.Basename}}_{{$type}}{{end}}

{{range $type := $.Property.InclusiveItemTypes}}
{{$type}}Array *[]{{$.Basename}}_{{$type}}{{end}}
}

func (r {{.Name}}) value() interface{} {
Expand All @@ -41,6 +47,12 @@ func (r {{.Name}}) value() interface{} {
}
{{end}}

{{range $type := $.Property.InclusivePrimitiveItemTypes}}
if r.{{$type}}Array != nil {
ret = append(ret, r.{{$type}}Array)
}
{{end}}

{{ if (.Property.Types) }}
{{range $type := $.Property.Types}}
if r.{{$type}} != nil {
Expand All @@ -55,6 +67,12 @@ func (r {{.Name}}) value() interface{} {
}
{{end}}

{{range $type := $.Property.InclusiveItemTypes}}
if r.{{$type}}Array != nil {
ret = append(ret, r.{{$type}}Array)
}
{{end}}

sort.Sort(utils.ByJSONLength(ret)) // Heuristic to select best attribute
if len(ret) > 0 {
return ret[0]
Expand Down Expand Up @@ -88,6 +106,11 @@ func (r *{{.Name}}) UnmarshalJSON(b []byte) error {
r.{{$type}}Array = &val
{{end}}

{{range $type := $.Property.InclusivePrimitiveItemTypes}}
case []{{convertToGoType $type}}:
r.{{$type}}Array = &val
{{end}}

case map[string]interface{}:
val = val // This ensures val is used to stop an error

Expand All @@ -107,9 +130,15 @@ func (r *{{.Name}}) UnmarshalJSON(b []byte) error {
{{range $type := $.Property.PrimitiveItemTypes}}
json.Unmarshal(b, &r.{{$type}}Array)
{{end}}
{{range $type := $.Property.ItemTypes}}
{{range $type := $.Property.InclusivePrimitiveItemTypes}}
json.Unmarshal(b, &r.{{$type}}Array)
{{end}}
{{range $type := $.Property.ItemTypes}}
json.Unmarshal(b, &r.{{$type}}Array)
{{end}}
{{range $type := $.Property.InclusiveItemTypes}}
json.Unmarshal(b, &r.{{$type}}Array)
{{end}}

}

Expand Down
20 changes: 20 additions & 0 deletions generate/templates/schema-property.template
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@
}{{if (or .Property.PrimitiveItemTypes (or .Property.Types .Property.ItemTypes))}},{{end}}
{{end}}

{{if (or .Property.InclusivePrimitiveItemTypes .Property.InclusiveItemTypes)}}
{
"type": "array",
"items": {
"anyOf": [
{{$length := len .Property.InclusivePrimitiveItemTypes}}{{$rc := counter $length}}{{range $index, $primitiveType := .Property.InclusivePrimitiveItemTypes}}
{
"type": "{{convertToJSONType $primitiveType}}"{{if call $rc}},{{end}}
}{{if $.Property.InclusiveItemTypes}},{{end}}
{{end}}
{{$length := len .Property.InclusiveItemTypes}}{{$rc := counter $length}}{{range $index, $itemType := .Property.InclusiveItemTypes}}
{
"$ref": "#/definitions/{{$.Parent}}.{{$itemType}}"
}{{if call $rc}},{{end}}
{{end}}
]
}
}{{if (or $.Property.InclusivePrimitiveItemTypes $.Property.InclusiveItemTypes)}},{{end}}
{{end}}

{{if .Property.PrimitiveItemTypes}}
{{$length := len .Property.PrimitiveItemTypes}}{{$rc := counter $length}}{{range $index, $primitiveItemType := .Property.PrimitiveItemTypes}}
{
Expand Down
48 changes: 22 additions & 26 deletions schema/sam.go
Original file line number Diff line number Diff line change
Expand Up @@ -124500,24 +124500,22 @@ var SamSchema = `{
},
{
"items": {
"type": "string"
"anyOf": [
{
"type": "string"
},
{
"$ref": "#/definitions/AWS::Serverless::Function.IAMPolicyDocument"
},
{
"$ref": "#/definitions/AWS::Serverless::Function.SAMPolicyTemplate"
}
]
},
"type": "array"
},
{
"$ref": "#/definitions/AWS::Serverless::Function.IAMPolicyDocument"
},
{
"items": {
"$ref": "#/definitions/AWS::Serverless::Function.IAMPolicyDocument"
},
"type": "array"
},
{
"items": {
"$ref": "#/definitions/AWS::Serverless::Function.SAMPolicyTemplate"
},
"type": "array"
}
]
},
Expand Down Expand Up @@ -126113,24 +126111,22 @@ var SamSchema = `{
},
{
"items": {
"type": "string"
"anyOf": [
{
"type": "string"
},
{
"$ref": "#/definitions/AWS::Serverless::StateMachine.IAMPolicyDocument"
},
{
"$ref": "#/definitions/AWS::Serverless::StateMachine.SAMPolicyTemplate"
}
]
},
"type": "array"
},
{
"$ref": "#/definitions/AWS::Serverless::StateMachine.IAMPolicyDocument"
},
{
"items": {
"$ref": "#/definitions/AWS::Serverless::StateMachine.IAMPolicyDocument"
},
"type": "array"
},
{
"items": {
"$ref": "#/definitions/AWS::Serverless::StateMachine.SAMPolicyTemplate"
},
"type": "array"
}
]
},
Expand Down
48 changes: 22 additions & 26 deletions schema/sam.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -124495,24 +124495,22 @@
},
{
"items": {
"type": "string"
"anyOf": [
{
"type": "string"
},
{
"$ref": "#/definitions/AWS::Serverless::Function.IAMPolicyDocument"
},
{
"$ref": "#/definitions/AWS::Serverless::Function.SAMPolicyTemplate"
}
]
},
"type": "array"
},
{
"$ref": "#/definitions/AWS::Serverless::Function.IAMPolicyDocument"
},
{
"items": {
"$ref": "#/definitions/AWS::Serverless::Function.IAMPolicyDocument"
},
"type": "array"
},
{
"items": {
"$ref": "#/definitions/AWS::Serverless::Function.SAMPolicyTemplate"
},
"type": "array"
}
]
},
Expand Down Expand Up @@ -126108,24 +126106,22 @@
},
{
"items": {
"type": "string"
"anyOf": [
{
"type": "string"
},
{
"$ref": "#/definitions/AWS::Serverless::StateMachine.IAMPolicyDocument"
},
{
"$ref": "#/definitions/AWS::Serverless::StateMachine.SAMPolicyTemplate"
}
]
},
"type": "array"
},
{
"$ref": "#/definitions/AWS::Serverless::StateMachine.IAMPolicyDocument"
},
{
"items": {
"$ref": "#/definitions/AWS::Serverless::StateMachine.IAMPolicyDocument"
},
"type": "array"
},
{
"items": {
"$ref": "#/definitions/AWS::Serverless::StateMachine.SAMPolicyTemplate"
},
"type": "array"
}
]
},
Expand Down

0 comments on commit d5e468f

Please sign in to comment.