Skip to content

Commit

Permalink
Adding tests for rendering of nested object to/from methods when nest…
Browse files Browse the repository at this point in the history
…ed attributes have associated external types (#71)
  • Loading branch information
bendbennett committed Oct 19, 2023
1 parent 024e229 commit 03e0dda
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 8 deletions.
1 change: 1 addition & 0 deletions internal/schema/templates/object_from.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
1 change: 1 addition & 0 deletions internal/schema/templates/object_to.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
98 changes: 90 additions & 8 deletions internal/schema/to_from_object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand All @@ -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
}
`),
},
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}`),
},
}
Expand Down

0 comments on commit 03e0dda

Please sign in to comment.