Skip to content

Conversation

@kankankanp
Copy link

@kankankanp kankankanp commented Oct 27, 2025

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

This pull request adds support for escaping double quotes (" → ") in struct tag parsing.
Now you can write struct tags like gorm:"expression:to_tsvector('english', \"Name\")" and the parser will correctly interpret the escaped double quote.

This change addresses #7600.

User Case Description

When using PostgreSQL full text search, it is often necessary to refer to a column name with double quotes (e.g., "Name").
Previously, GORM's struct tag parser did not support writing double quotes inside the tag expression.
With this change, you can escape double quotes using a backslash, and the intended SQL expression will be parsed and used correctly.

Example:

type Object struct {
    Name string `gorm:"expression:to_tsvector('english', \"Name\")"`
}

This struct tag will now be correctly interpreted as:
to_tsvector('english', "Name")

@propel-code-bot
Copy link
Contributor

propel-code-bot bot commented Oct 27, 2025

Enable escaped double-quote handling in ParseTagSetting

Bug-fix PR that updates the struct tag parser to recognise and unescape the \" sequence so that users can embed literal double quotes inside gorm tags. The logic that glued backslash-escaped segments has been refactored for clarity, and an accompanying unit test demonstrates the new behaviour.

Key Changes

• Refactored loop in schema/utils.go to rebuild escaped segments using strings.HasSuffix instead of manual index juggling
• Added strings.ReplaceAll(val, ", ") to unescape double quotes for each setting value
• Introduced new test TestParseTagSettingWithDoubleQuoteEscape in schema/utils_test.go
• Removed obsolete variables and reduced code complexity within ParseTagSetting

Affected Areas

schema/utils.go – function ParseTagSetting
schema/utils_test.go – unit tests

This summary was automatically generated by @propel-code-bot

@propel-code-bot propel-code-bot bot changed the title fix: support escaping double quotes in struct tag parser fix(schema): Allow escaped double quotes in struct tag parser Oct 27, 2025
@kankankanp kankankanp changed the title fix(schema): Allow escaped double quotes in struct tag parser fix: Allow escaped double quotes in struct tag parser Oct 27, 2025
@kankankanp
Copy link
Author

@jinzhu
If possible, I would be grateful if you could review this PR.

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