Skip to content

feat: use custom type for nested object struct fields#12

Merged
hanneshayashi merged 2 commits into
mainfrom
feat/custom-type-nested-struct-fields
Jun 5, 2026
Merged

feat: use custom type for nested object struct fields#12
hanneshayashi merged 2 commits into
mainfrom
feat/custom-type-nested-struct-fields

Conversation

@hanneshayashi

Copy link
Copy Markdown
Collaborator

Problem

When a StringAttribute has a CustomType (e.g., jsontypes.NormalizedType{}), the generated schema correctly includes the custom type, but the struct field inside nested object value types (like NodeValue.Props) was always generated as basetypes.StringValue instead of the custom value type (e.g., jsontypes.Normalized).

This happened because AttrValue() and AttrType() only checked for AssociatedExternalType and ignored CustomType. Meanwhile, ModelField() (used for top-level model structs) correctly used CustomType.ValueType() — causing an inconsistency.

Fix

  • Add TypeString() method to CustomTypePrimitive to expose the type string
  • Update AttrValue() and AttrType() in datasource, resource, and provider packages to check CustomType.ValueType() / CustomType.TypeString() before falling back to basetypes.StringValue / basetypes.StringType{}

Before / After

// Before (incorrect):
type NodeValue struct {
    Props  basetypes.StringValue `tfsdk:"props"`
}

// After (correct):
type NodeValue struct {
    Props  jsontypes.Normalized `tfsdk:"props"`
}

Tests

  • Added TestCustomTypePrimitive_TypeString and TestCustomTypePrimitive_ValueType (7 cases)
  • Added TestGeneratorStringAttribute_AttrType and TestGeneratorStringAttribute_AttrValue (6 cases)
  • All existing tests pass

When a StringAttribute has a CustomType (e.g., jsontypes.NormalizedType{}),
the AttrValue() and AttrType() methods now return the custom type's value
type and type string respectively, instead of always falling back to
basetypes.StringValue / basetypes.StringType{}.

This fixes an inconsistency where ModelField() (for top-level structs)
correctly used CustomType.ValueType(), but AttrValue() (for nested object
value structs like NodeValue) did not. As a result, fields like 'props'
inside nested objects were generated as basetypes.StringValue instead of
jsontypes.Normalized.

Changes:
- Add TypeString() method to CustomTypePrimitive
- Update AttrValue() and AttrType() in datasource, resource, and provider
  packages to check CustomType before falling back to defaults
- Add unit tests for TypeString(), ValueType(), AttrType(), and AttrValue()

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a codegen inconsistency where StringAttribute custom types were applied in generated schema (and top-level model fields) but not respected when generating nested object struct field value types, causing nested fields to default to basetypes.StringValue.

Changes:

  • Added TypeString() to convert.CustomTypePrimitive to expose the configured custom attr type string.
  • Updated AttrType() / AttrValue() for string attributes in datasource, provider, and resource generators to return CustomType types/values when configured.
  • Added unit tests covering CustomTypePrimitive and datasource GeneratorStringAttribute attr type/value behavior.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
internal/resource/string_attribute.go Uses custom type type/value strings for generated attr type/value of string attributes.
internal/provider/string_attribute.go Uses custom type type/value strings for generated attr type/value of string attributes.
internal/datasource/string_attribute.go Uses custom type type/value strings for generated attr type/value of string attributes.
internal/datasource/string_attribute_test.go Adds tests for AttrType()/AttrValue() on datasource string attributes.
internal/convert/custom_type_primitive.go Adds TypeString() accessor for spec custom type string.
internal/convert/custom_type_primitive_test.go Adds tests for TypeString() and ValueType() precedence behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/datasource/string_attribute.go
Comment thread internal/datasource/string_attribute.go
Comment thread internal/provider/string_attribute.go
Comment thread internal/provider/string_attribute.go
Comment thread internal/resource/string_attribute.go
Comment thread internal/resource/string_attribute.go
Comment thread internal/datasource/string_attribute_test.go
Comment thread internal/datasource/string_attribute_test.go
Comment thread internal/provider/string_attribute.go
Comment thread internal/resource/string_attribute.go
…ype/AttrValue

Swap the order of checks so CustomType is evaluated first, consistent with
how Schema() and ModelField() already handle precedence. This ensures
correct nested struct field types when both CustomType and
AssociatedExternalType are configured.

Also adds AttrType/AttrValue tests to resource and provider packages for
parity with datasource, including precedence coverage.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.

@hanneshayashi hanneshayashi merged commit bd13bae into main Jun 5, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants