Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't panic when annotating with missing objects #11660

Merged
merged 1 commit into from Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: programgen
description: Don't panic on some empty objects
18 changes: 9 additions & 9 deletions pkg/codegen/pcl/invoke.go
Expand Up @@ -44,20 +44,20 @@ func getInvokeToken(call *hclsyntax.FunctionCallExpr) (string, hcl.Range, bool)
return literal.Val.AsString(), call.Args[0].Range(), true
}

// annotateObjectProperties annotates the properties of an object expression with the types of the corresponding
// properties in the schema. This is used to provide type information
// for invoke calls that didn't have type annotations.
// annotateObjectProperties annotates the properties of an object expression with the
// types of the corresponding properties in the schema. This is used to provide type
// information for invoke calls that didn't have type annotations.
//
// This function will recursively annotate the properties of objects
// that are nested within the object expression type.
// This function will recursively annotate the properties of objects that are nested
// within the object expression type.
func annotateObjectProperties(modelType model.Type, schemaType schema.Type) {
if optionalType, ok := schemaType.(*schema.OptionalType); ok {
if optionalType, ok := schemaType.(*schema.OptionalType); ok && optionalType != nil {
schemaType = optionalType.ElementType
}

switch arg := modelType.(type) {
case *model.ObjectType:
if schemaObjectType, ok := schemaType.(*schema.ObjectType); ok {
if schemaObjectType, ok := schemaType.(*schema.ObjectType); ok && schemaObjectType != nil {
schemaProperties := make(map[string]schema.Type)
for _, schemaProperty := range schemaObjectType.Properties {
schemaProperties[schemaProperty.Name] = schemaProperty.Type
Expand All @@ -74,13 +74,13 @@ func annotateObjectProperties(modelType model.Type, schemaType schema.Type) {
}
case *model.ListType:
underlyingArrayType := arg.ElementType
if schemaArrayType, ok := schemaType.(*schema.ArrayType); ok {
if schemaArrayType, ok := schemaType.(*schema.ArrayType); ok && schemaArrayType != nil {
underlyingSchemaArrayType := schemaArrayType.ElementType
annotateObjectProperties(underlyingArrayType, underlyingSchemaArrayType)
}

case *model.TupleType:
if schemaArrayType, ok := schemaType.(*schema.ArrayType); ok {
if schemaArrayType, ok := schemaType.(*schema.ArrayType); ok && schemaArrayType != nil {
underlyingSchemaArrayType := schemaArrayType.ElementType
elementTypes := arg.ElementTypes
for _, elemType := range elementTypes {
Expand Down