Skip to content

fix: preserve leading underscores in ToPascalCase to avoid field collisions#11

Merged
hanneshayashi merged 1 commit into
mainfrom
fix/underscore-field-collision
Jun 3, 2026
Merged

fix: preserve leading underscores in ToPascalCase to avoid field collisions#11
hanneshayashi merged 1 commit into
mainfrom
fix/underscore-field-collision

Conversation

@hanneshayashi

Copy link
Copy Markdown
Collaborator

Problem

Properties like _id and id both normalized to Id in PascalCase, causing Go struct field redeclaration errors:

type CloudDiagramSnapshotModel struct {
    Id types.String `tfsdk:"_id"`   // ← redeclared
    Id types.String `tfsdk:"id"`    // ← original
}

This is common in MongoDB-backed APIs where _id is the document ID.

Fix

ToPascalCase now prepends Underscore when the identifier starts with _:

  • _idUnderscoreId
  • idId (unchanged)

Only the Go struct field name changes — the tfsdk tag preserves the original attribute name.

Result

type CloudDiagramSnapshotModel struct {
    UnderscoreId types.String `tfsdk:"_id"`
    Id           types.String `tfsdk:"id"`
}

Testing

  • All framework tests pass (go test ./...)
  • Provider make generate + go build ./... succeed

…isions

Properties like _id and id both normalized to Id in PascalCase,
causing Go struct field redeclaration errors. Now _id produces
UnderscoreId while id remains Id.

This is a targeted fix — only identifiers starting with underscore
are affected. The tfsdk tags still use the original attribute name.

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 updates the schema identifier casing logic so Terraform attribute names that begin with _ do not collide with their non-underscored equivalents when generating Go struct field names (e.g., _id vs id).

Changes:

  • Update FrameworkIdentifier.ToPascalCase() to prepend Underscore for identifiers starting with _.
  • Update identifier casing tests to reflect the new leading-underscore behavior across camel/pascal conversions.

Reviewed changes

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

File Description
internal/schema/framework_identifier.go Adjusts PascalCase generation to avoid _x/x Go field name collisions by preserving leading underscores via a prefix.
internal/schema/framework_identifier_test.go Updates expected casing outputs for identifiers with leading underscores.

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

Comment on lines +83 to +94
input := string(identifier)

result := snakeLetters.ReplaceAllStringFunc(input, func(s string) string {
return strings.ToUpper(strings.Replace(s, "_", "", -1))
})

// Preserve leading underscore to avoid collisions (e.g., _id vs id).
if strings.HasPrefix(input, "_") {
result = "Underscore" + result
}

return result
Comment on lines 194 to 197
"leading underscore": {
identifier: "_thing",
want: "Thing",
want: "UnderscoreThing",
},
@hanneshayashi hanneshayashi merged commit 0831087 into main Jun 3, 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