From 400dfd70ae7a2a9a6831337a0dd36014ec0c7441 Mon Sep 17 00:00:00 2001 From: Benjamin Bennett Date: Fri, 27 Oct 2023 16:44:22 +0100 Subject: [PATCH] Adding a few tests (#78) --- internal/schema/custom_nested_object_test.go | 56 ++++++++++-- .../templates/nested_object_from.gotmpl | 1 + .../schema/templates/nested_object_to.gotmpl | 2 +- internal/schema/to_from_nested_object_test.go | 86 +++++++++++++++++++ 4 files changed, 138 insertions(+), 7 deletions(-) diff --git a/internal/schema/custom_nested_object_test.go b/internal/schema/custom_nested_object_test.go index dc326086..137f50a1 100644 --- a/internal/schema/custom_nested_object_test.go +++ b/internal/schema/custom_nested_object_test.go @@ -825,11 +825,12 @@ func TestCustomNestedObjectValue_renderToObjectValue(t *testing.T) { t.Parallel() testCases := map[string]struct { - name string - attributeTypes map[string]string - attrTypes map[string]string - expected []byte - expectedError error + name string + attributeTypes map[string]string + attrTypes map[string]string + collectionTypes map[string]map[string]string + expected []byte + expectedError error }{ "default": { name: "Example", @@ -851,6 +852,49 @@ map[string]attr.Value{ "bool_attribute": v.BoolAttribute, }) +return objVal, diags +}`), + }, + "collection-type": { + name: "Example", + attributeTypes: map[string]string{ + "list_attribute": "List", + }, + attrTypes: map[string]string{ + "list_attribute": "basetypes.ListType{\nElemType: types.BoolType,\n}", + }, + collectionTypes: map[string]map[string]string{ + "list_attribute": { + "ElementType": "types.BoolType", + "TypeValueFunc": "types.ListValue", + }, + }, + expected: []byte(` +func (v ExampleValue) ToObjectValue(ctx context.Context) (basetypes.ObjectValue, diag.Diagnostics) { +var diags diag.Diagnostics + +listAttributeVal, d := types.ListValue(types.BoolType, v.ListAttribute.Elements()) + +diags.Append(d...) + +if d.HasError() { +return types.ObjectUnknown(map[string]attr.Type{ +"list_attribute": basetypes.ListType{ +ElemType: types.BoolType, +}, +}), diags +} + +objVal, diags := types.ObjectValue( +map[string]attr.Type{ +"list_attribute": basetypes.ListType{ +ElemType: types.BoolType, +}, +}, +map[string]attr.Value{ +"list_attribute": listAttributeVal, +}) + return objVal, diags }`), }, @@ -862,7 +906,7 @@ return objVal, diags t.Run(name, func(t *testing.T) { t.Parallel() - customObjectValue := NewCustomNestedObjectValue(testCase.name, testCase.attributeTypes, testCase.attrTypes, nil, nil) + customObjectValue := NewCustomNestedObjectValue(testCase.name, testCase.attributeTypes, testCase.attrTypes, nil, testCase.collectionTypes) got, err := customObjectValue.renderToObjectValue() diff --git a/internal/schema/templates/nested_object_from.gotmpl b/internal/schema/templates/nested_object_from.gotmpl index df6e1ae3..b08d04d6 100644 --- a/internal/schema/templates/nested_object_from.gotmpl +++ b/internal/schema/templates/nested_object_from.gotmpl @@ -16,6 +16,7 @@ if diags.HasError() { return New{{$.Name}}ValueUnknown(), diags } {{- else if $value.CollectionType.ElementType}} + {{$key.ToCamelCase}}Val, d := {{$value.CollectionType.TypeValueFrom}}(ctx, {{$value.CollectionType.ElementType}}, apiObject.{{$key.ToPascalCase}}) diags.Append(d...) diff --git a/internal/schema/templates/nested_object_to.gotmpl b/internal/schema/templates/nested_object_to.gotmpl index 8fd782d3..a8c33935 100644 --- a/internal/schema/templates/nested_object_to.gotmpl +++ b/internal/schema/templates/nested_object_to.gotmpl @@ -43,7 +43,7 @@ return &{{.AssocExtType.TypeReference}}{ {{$key.ToPascalCase}}: {{$value.AssocExtType.ToCamelCase}}, {{- else if $value.Default}} {{$key.ToPascalCase}}: v.{{$key.ToPascalCase}}.{{$value.Default}}(), -{{- else if $value.CollectionType}} +{{- else if $value.CollectionType.GoType}} {{$key.ToPascalCase}}: {{$key.ToCamelCase}}Field, {{- end}} {{- end}} diff --git a/internal/schema/to_from_nested_object_test.go b/internal/schema/to_from_nested_object_test.go index ef716064..76f9c613 100644 --- a/internal/schema/to_from_nested_object_test.go +++ b/internal/schema/to_from_nested_object_test.go @@ -80,6 +80,44 @@ if diags.HasError() { return NewExampleValueUnknown(), diags } +return ExampleValue{ +BoolAttribute: boolAttributeVal, +state: attr.ValueStateKnown, +}, diags +} +`), + }, + "collection-type": { + name: "Example", + assocExtType: &AssocExtType{ + AssociatedExternalType: &schema.AssociatedExternalType{ + Type: "*apisdk.Type", + }, + }, + fromFuncs: map[string]ToFromConversion{ + "bool_attribute": { + CollectionType: CollectionFields{ + ElementType: "types.BoolType", + TypeValueFrom: "types.ListValueFrom", + }, + }, + }, + expected: []byte(` +func (v ExampleValue) FromApisdkType(ctx context.Context, apiObject *apisdk.Type) (ExampleValue, diag.Diagnostics) { +var diags diag.Diagnostics + +if apiObject == nil { +return NewExampleValueNull(), diags +} + +boolAttributeVal, d := types.ListValueFrom(ctx, types.BoolType, apiObject.BoolAttribute) + +diags.Append(d...) + +if diags.HasError() { +return NewExampleValueUnknown(), diags +} + return ExampleValue{ BoolAttribute: boolAttributeVal, state: attr.ValueStateKnown, @@ -202,6 +240,54 @@ return nil, diags return &apisdk.Type{ BoolAttribute: apiBoolAttribute, }, diags +}`), + }, + "collection-type": { + name: "Example", + assocExtType: &AssocExtType{ + &schema.AssociatedExternalType{ + Import: &code.Import{ + Path: "example.com/apisdk", + }, + Type: "*apisdk.Type", + }, + }, + toFuncs: map[string]ToFromConversion{ + "bool_attribute": { + CollectionType: CollectionFields{ + GoType: "[]*bool", + }, + }, + }, + expected: []byte(`func (v ExampleValue) ToApisdkType(ctx context.Context) (*apisdk.Type, diag.Diagnostics) { +var diags diag.Diagnostics + +if v.IsNull() { +return nil, diags +} + +if v.IsUnknown() { +diags.Append(diag.NewErrorDiagnostic( +"ExampleValue Value Is Unknown", +` + "`" + `"ExampleValue" is unknown.` + "`" + `, +)) + +return nil, diags +} + +var boolAttributeField []*bool + +d := v.BoolAttribute.ElementsAs(ctx, &boolAttributeField, false) + +diags.Append(d...) + +if diags.HasError() { +return nil, diags +} + +return &apisdk.Type{ +BoolAttribute: boolAttributeField, +}, diags }`), }, }