From 6dafd8de23faa907b127f904f31fcf624e5d7eca Mon Sep 17 00:00:00 2001 From: Benjamin Bennett Date: Mon, 6 Nov 2023 08:43:37 +0000 Subject: [PATCH] Return unimplemented error for objects containing collections or objects (#84) --- .../datasource_generate/object_attribute.go | 12 +++++++-- .../provider_generate/object_attribute.go | 12 +++++++-- .../resource_generate/object_attribute.go | 12 +++++++-- internal/schema/attrtypes.go | 26 ++++++++++++++++--- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/internal/datasource_generate/object_attribute.go b/internal/datasource_generate/object_attribute.go index 326813dc..12ea7cb0 100644 --- a/internal/datasource_generate/object_attribute.go +++ b/internal/datasource_generate/object_attribute.go @@ -174,9 +174,17 @@ func (g GeneratorObjectAttribute) ToFromFunctions(name string) ([]byte, error) { return nil, nil } - attrTypesToFuncs := generatorschema.GetAttrTypesToFuncs(g.AttributeTypes) + attrTypesToFuncs, err := generatorschema.GetAttrTypesToFuncs(g.AttributeTypes) - attrTypesFromFuncs := generatorschema.GetAttrTypesFromFuncs(g.AttributeTypes) + if err != nil { + return nil, err + } + + attrTypesFromFuncs, err := generatorschema.GetAttrTypesFromFuncs(g.AttributeTypes) + + if err != nil { + return nil, err + } toFrom := generatorschema.NewToFromObject(name, g.AssociatedExternalType, attrTypesToFuncs, attrTypesFromFuncs) diff --git a/internal/provider_generate/object_attribute.go b/internal/provider_generate/object_attribute.go index b37ed7d6..40b43edf 100644 --- a/internal/provider_generate/object_attribute.go +++ b/internal/provider_generate/object_attribute.go @@ -174,9 +174,17 @@ func (g GeneratorObjectAttribute) ToFromFunctions(name string) ([]byte, error) { return nil, nil } - attrTypesToFuncs := generatorschema.GetAttrTypesToFuncs(g.AttributeTypes) + attrTypesToFuncs, err := generatorschema.GetAttrTypesToFuncs(g.AttributeTypes) - attrTypesFromFuncs := generatorschema.GetAttrTypesFromFuncs(g.AttributeTypes) + if err != nil { + return nil, err + } + + attrTypesFromFuncs, err := generatorschema.GetAttrTypesFromFuncs(g.AttributeTypes) + + if err != nil { + return nil, err + } toFrom := generatorschema.NewToFromObject(name, g.AssociatedExternalType, attrTypesToFuncs, attrTypesFromFuncs) diff --git a/internal/resource_generate/object_attribute.go b/internal/resource_generate/object_attribute.go index 15c073e6..4f7b0901 100644 --- a/internal/resource_generate/object_attribute.go +++ b/internal/resource_generate/object_attribute.go @@ -207,9 +207,17 @@ func (g GeneratorObjectAttribute) ToFromFunctions(name string) ([]byte, error) { return nil, nil } - attrTypesToFuncs := generatorschema.GetAttrTypesToFuncs(g.AttributeTypes) + attrTypesToFuncs, err := generatorschema.GetAttrTypesToFuncs(g.AttributeTypes) - attrTypesFromFuncs := generatorschema.GetAttrTypesFromFuncs(g.AttributeTypes) + if err != nil { + return nil, err + } + + attrTypesFromFuncs, err := generatorschema.GetAttrTypesFromFuncs(g.AttributeTypes) + + if err != nil { + return nil, err + } toFrom := generatorschema.NewToFromObject(name, g.AssociatedExternalType, attrTypesToFuncs, attrTypesFromFuncs) diff --git a/internal/schema/attrtypes.go b/internal/schema/attrtypes.go index e145db62..178d1962 100644 --- a/internal/schema/attrtypes.go +++ b/internal/schema/attrtypes.go @@ -4,6 +4,7 @@ package schema import ( + "errors" "fmt" "strings" @@ -93,7 +94,7 @@ type AttrTypesToFuncs struct { // GetAttrTypesToFuncs returns string representations of the function that is used // for converting to an API Go type from a framework type. // TODO: Handle custom type, and types other than primitives. -func GetAttrTypesToFuncs(a specschema.ObjectAttributeTypes) map[string]AttrTypesToFuncs { +func GetAttrTypesToFuncs(a specschema.ObjectAttributeTypes) (map[string]AttrTypesToFuncs, error) { attrTypesFuncs := make(map[string]AttrTypesToFuncs, len(a)) for _, v := range a { @@ -113,11 +114,20 @@ func GetAttrTypesToFuncs(a specschema.ObjectAttributeTypes) map[string]AttrTypes AttrValue: "types.Int64", ToFunc: "ValueInt64Pointer", } + case v.List != nil: + return nil, NewUnimplementedError(errors.New("list attribute type is not yet implemented")) + case v.Map != nil: + return nil, NewUnimplementedError(errors.New("map attribute type is not yet implemented")) + case v.Number != nil: attrTypesFuncs[v.Name] = AttrTypesToFuncs{ AttrValue: "types.Number", ToFunc: "ValueBigFloat", } + case v.Object != nil: + return nil, NewUnimplementedError(errors.New("object attribute type is not yet implemented")) + case v.Set != nil: + return nil, NewUnimplementedError(errors.New("set attribute type is not yet implemented")) case v.String != nil: attrTypesFuncs[v.Name] = AttrTypesToFuncs{ AttrValue: "types.String", @@ -126,13 +136,13 @@ func GetAttrTypesToFuncs(a specschema.ObjectAttributeTypes) map[string]AttrTypes } } - return attrTypesFuncs + return attrTypesFuncs, nil } // GetAttrTypesFromFuncs returns string representations of the function that is used // for converting from an API Go type to a framework type. // TODO: Handle custom type, and types other than primitives. -func GetAttrTypesFromFuncs(a specschema.ObjectAttributeTypes) map[string]string { +func GetAttrTypesFromFuncs(a specschema.ObjectAttributeTypes) (map[string]string, error) { attrTypesFuncs := make(map[string]string, len(a)) for _, v := range a { @@ -143,12 +153,20 @@ func GetAttrTypesFromFuncs(a specschema.ObjectAttributeTypes) map[string]string attrTypesFuncs[v.Name] = "types.Float64PointerValue" case v.Int64 != nil: attrTypesFuncs[v.Name] = "types.Int64PointerValue" + case v.List != nil: + return nil, NewUnimplementedError(errors.New("list attribute type is not yet implemented")) + case v.Map != nil: + return nil, NewUnimplementedError(errors.New("map attribute type is not yet implemented")) case v.Number != nil: attrTypesFuncs[v.Name] = "types.NumberValue" + case v.Object != nil: + return nil, NewUnimplementedError(errors.New("object attribute type is not yet implemented")) + case v.Set != nil: + return nil, NewUnimplementedError(errors.New("set attribute type is not yet implemented")) case v.String != nil: attrTypesFuncs[v.Name] = "types.StringPointerValue" } } - return attrTypesFuncs + return attrTypesFuncs, nil }