From 03e0dda36e612b4956d33cc1ef59f47ae071c793 Mon Sep 17 00:00:00 2001 From: Benjamin Bennett Date: Thu, 19 Oct 2023 13:57:17 +0100 Subject: [PATCH] Adding tests for rendering of nested object to/from methods when nested attributes have associated external types (#71) --- internal/schema/templates/object_from.gotmpl | 1 + internal/schema/templates/object_to.gotmpl | 1 + internal/schema/to_from_object_test.go | 98 ++++++++++++++++++-- 3 files changed, 92 insertions(+), 8 deletions(-) diff --git a/internal/schema/templates/object_from.gotmpl b/internal/schema/templates/object_from.gotmpl index 48badfb3..39ff2b92 100644 --- a/internal/schema/templates/object_from.gotmpl +++ b/internal/schema/templates/object_from.gotmpl @@ -7,6 +7,7 @@ return New{{.Name}}ValueNull(), diags } {{- range $key, $value := .FromFuncs}} {{- if $value.AssocExtType}} + {{$key.ToCamelCase}}Val, d := {{$key.ToPascalCase}}Value{}.From{{$value.AssocExtType.ToPascalCase}}(ctx, apiObject.{{$key.ToPascalCase}}) diags.Append(d...) diff --git a/internal/schema/templates/object_to.gotmpl b/internal/schema/templates/object_to.gotmpl index d524944c..737e8ec0 100644 --- a/internal/schema/templates/object_to.gotmpl +++ b/internal/schema/templates/object_to.gotmpl @@ -15,6 +15,7 @@ return nil, diags } {{- range $key, $value := .ToFuncs}} {{- if $value.AssocExtType}} + {{$value.AssocExtType.ToCamelCase}}, d := v.{{$key.ToPascalCase}}.To{{$value.AssocExtType.ToPascalCase}}(ctx) diags.Append(d...) diff --git a/internal/schema/to_from_object_test.go b/internal/schema/to_from_object_test.go index 642eec11..9fcb0f56 100644 --- a/internal/schema/to_from_object_test.go +++ b/internal/schema/to_from_object_test.go @@ -24,17 +24,13 @@ func TestToFromObject_renderFrom(t *testing.T) { "default": { name: "Example", assocExtType: &AssocExtType{ - &schema.AssociatedExternalType{ - Import: &code.Import{ - Path: "example.com/apisdk", - }, + AssociatedExternalType: &schema.AssociatedExternalType{ Type: "*apisdk.Type", }, }, fromFuncs: map[string]ToFromConversion{ "bool_attribute": { - Default: "BoolPointerValue", - AssocExtType: nil, + Default: "BoolPointerValue", }, }, expected: []byte(` @@ -50,6 +46,45 @@ BoolAttribute: types.BoolPointerValue(apiObject.BoolAttribute), state: attr.ValueStateKnown, }, diags } +`), + }, + "nested-assoc-ext-type": { + name: "Example", + assocExtType: &AssocExtType{ + AssociatedExternalType: &schema.AssociatedExternalType{ + Type: "*apisdk.Type", + }, + }, + fromFuncs: map[string]ToFromConversion{ + "bool_attribute": { + AssocExtType: &AssocExtType{ + AssociatedExternalType: &schema.AssociatedExternalType{ + Type: "*api.BoolAttribute", + }, + }, + }, + }, + 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 := BoolAttributeValue{}.FromApiBoolAttribute(ctx, apiObject.BoolAttribute) + +diags.Append(d...) + +if diags.HasError() { +return NewExampleValueNull(), diags +} + +return ExampleValue{ +BoolAttribute: boolAttributeVal, +state: attr.ValueStateKnown, +}, diags +} `), }, } @@ -97,8 +132,7 @@ func TestToFromObject_renderTo(t *testing.T) { }, toFuncs: map[string]ToFromConversion{ "bool_attribute": { - Default: "ValueBoolPointer", - AssocExtType: nil, + Default: "ValueBoolPointer", }, }, expected: []byte(`func (v ExampleValue) ToApisdkType(ctx context.Context) (*apisdk.Type, diag.Diagnostics) { @@ -120,6 +154,54 @@ return nil, diags return &apisdk.Type{ BoolAttribute: v.BoolAttribute.ValueBoolPointer(), }, diags +}`), + }, + "nested-assoc-ext-type": { + name: "Example", + assocExtType: &AssocExtType{ + &schema.AssociatedExternalType{ + Import: &code.Import{ + Path: "example.com/apisdk", + }, + Type: "*apisdk.Type", + }, + }, + toFuncs: map[string]ToFromConversion{ + "bool_attribute": { + AssocExtType: &AssocExtType{ + AssociatedExternalType: &schema.AssociatedExternalType{ + Type: "*api.BoolAttribute", + }, + }, + }, + }, + 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 +} + +apiBoolAttribute, d := v.BoolAttribute.ToApiBoolAttribute(ctx) + +diags.Append(d...) + +if diags.HasError() { +return nil, diags +} + +return &apisdk.Type{ +BoolAttribute: apiBoolAttribute, +}, diags }`), }, }