Skip to content

Conversation

@hanneshayashi
Copy link

@hanneshayashi hanneshayashi commented Dec 23, 2025

Description

This PR addresses several issues related to code generation, specifically improving how custom types are handled.

Key Changes

  1. Deduplication of Custom Types: Implemented logic to track generated types and prevent duplicate declarations. This ensures that custom types used across multiple resources or nested blocks are only defined once, resolving potential compilation errors and reducing code noise.
  2. Simplified Value Assignment for Nested Objects: Refactored nested_object_value_to_object_value.gotmpl to simplify the value assignment logic. This makes the generated code cleaner and less error-prone.

Examples

Simplified Nested Object Assignment

Previously, the generated code for checking null/unknown values for nested objects was verbose:

var myObject basetypes.ObjectValue

if v.MyObject.IsNull() {
    myObject = types.ObjectNull(
        MyObjectValue{}.AttributeTypes(ctx),
    )
}

if v.MyObject.IsUnknown() {
    myObject = types.ObjectUnknown(
        MyObjectValue{}.AttributeTypes(ctx),
    )
}
// ...

Now, it is directly assigned, relying on the underlying framework types to handle the state correctly:

var myObject attr.Value

{
    myObject = v.MyObject
}

Why these changes were necessary

  • Duplicate Types: Previously, the generator could produce duplicate type definitions if the same custom type was referenced in multiple places, leading to invalid Go code.
  • Code Quality: simplifying the templates leads to more readable and maintainable generated code.

Tests

Updated internal/schema/custom_nested_object_test.go to match the simplified code generation logic.

  • Extended the simplification logic (direct assignment) to ListNested, MapNested, and SetNested types in the template to ensure consistency.
  • Updated test expectations to reflect these changes and fixed whitespace mismatches.
  • Confirmed that all tests in internal/schema pass.

Checklists

  • Code compiles correctly
  • Tests run successfully
  • Documentation updated (if applicable)

@hanneshayashi hanneshayashi requested a review from a team as a code owner December 23, 2025 06:59
@hanneshayashi hanneshayashi changed the title feat: improve custom type generation and cleanup dependencies feat: improve custom type generation and fix duplicate definitions Dec 23, 2025
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.

1 participant