Skip to content

Commit

Permalink
Merge #11660
Browse files Browse the repository at this point in the history
11660: Don't panic when annotating with missing objects r=iwahbe a=iwahbe

Fixes #11658

I have been unable to repro the bug, but since `annotateObjectProperties` is advisory, I'm adding some defensive `nil` checks. 

Co-authored-by: Ian Wahbe <ian@wahbe.com>
  • Loading branch information
bors[bot] and iwahbe committed Dec 15, 2022
2 parents 40e0ac2 + 2367148 commit 33cd3e3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
@@ -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

0 comments on commit 33cd3e3

Please sign in to comment.