Skip to content

Commit

Permalink
nested_types singlemode object
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Hoehl <[email protected]>
  • Loading branch information
hown3d committed Nov 8, 2024
1 parent 239e492 commit 2fc87b9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 5 additions & 3 deletions pkg/types/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/crossplane/upjet/pkg/config"
"github.com/crossplane/upjet/pkg/schema/traverser"
conversiontfjson "github.com/crossplane/upjet/pkg/types/conversion/tfjson"
)

const (
Expand Down Expand Up @@ -216,7 +217,7 @@ func (g *Builder) buildSchema(f *Field, cfg *config.Resource, names []string, cp
return types.NewPointer(types.Universe.Lookup("int64").Type()), nil, nil
case schema.TypeString:
return types.NewPointer(types.Universe.Lookup("string").Type()), nil, nil
case schema.TypeMap, schema.TypeList, schema.TypeSet:
case schema.TypeMap, schema.TypeList, schema.TypeSet, conversiontfjson.SchemaTypeObject:
names = append(names, f.Name.Camel)
if f.Schema.Type != schema.TypeMap {
// We don't want to have a many-to-many relationship in case of a Map, since we use SecretReference as
Expand Down Expand Up @@ -311,8 +312,9 @@ func (g *Builder) buildSchema(f *Field, cfg *config.Resource, names []string, cp
return nil, nil, errors.Errorf("element type of %s should be either schema.Resource or schema.Schema", traverser.FieldPath(names))
}

// if the singleton list is to be replaced by an embedded object
if cfg.SchemaElementOptions.EmbeddedObject(cpath) {
// if the singleton list is to be replaced by an embedded object or schema
// is an object
if cfg.SchemaElementOptions.EmbeddedObject(cpath) || f.Schema.Type == conversiontfjson.SchemaTypeObject {
return types.NewPointer(elemType), types.NewPointer(initElemType), nil
}
// NOTE(muvaf): Maps and slices are already pointers, so we don't need to
Expand Down
7 changes: 6 additions & 1 deletion pkg/types/conversion/tfjson/tfjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"github.com/zclconf/go-cty/cty"
)

// SchemaTypeObject is the exported version of schema.typeObject to support
// nested types.
// TODO:remove once not dependent of terrafom sdkv2 schema
const SchemaTypeObject = schemav2.ValueType(9)

// GetV2ResourceMap converts input resource schemas with
// "terraform-json" representation to terraform-plugin-sdk representation which
// is what Upjet expects today.
Expand Down Expand Up @@ -93,7 +98,7 @@ func tfJSONNestedAttributeTypeToV2Schema(na *tfjson.SchemaNestedAttributeType) *
case tfjson.SchemaNestingModeMap:
v2sch.Type = schemav2.TypeMap
case tfjson.SchemaNestingModeSingle:
v2sch.Type = schemav2.TypeList
v2sch.Type = SchemaTypeObject
v2sch.MinItems = 0
v2sch.Required = hasNestedAttributeRequiredChild(na)
v2sch.Optional = !v2sch.Required
Expand Down

0 comments on commit 2fc87b9

Please sign in to comment.