feat: use custom type for nested object struct fields#12
Merged
Conversation
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()
There was a problem hiding this comment.
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()toconvert.CustomTypePrimitiveto expose the configured custom attr type string. - Updated
AttrType()/AttrValue()for string attributes indatasource,provider, andresourcegenerators to returnCustomTypetypes/values when configured. - Added unit tests covering
CustomTypePrimitiveand datasourceGeneratorStringAttributeattr 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.
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a
StringAttributehas aCustomType(e.g.,jsontypes.NormalizedType{}), the generated schema correctly includes the custom type, but the struct field inside nested object value types (likeNodeValue.Props) was always generated asbasetypes.StringValueinstead of the custom value type (e.g.,jsontypes.Normalized).This happened because
AttrValue()andAttrType()only checked forAssociatedExternalTypeand ignoredCustomType. Meanwhile,ModelField()(used for top-level model structs) correctly usedCustomType.ValueType()— causing an inconsistency.Fix
TypeString()method toCustomTypePrimitiveto expose the type stringAttrValue()andAttrType()indatasource,resource, andproviderpackages to checkCustomType.ValueType()/CustomType.TypeString()before falling back tobasetypes.StringValue/basetypes.StringType{}Before / After
Tests
TestCustomTypePrimitive_TypeStringandTestCustomTypePrimitive_ValueType(7 cases)TestGeneratorStringAttribute_AttrTypeandTestGeneratorStringAttribute_AttrValue(6 cases)