Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(cmd/gendao): fix overlapping shardingPattern matching issue#4631

Merged
gqcn merged 1 commit intogogf:masterfrom
lingcoder:fix/gendao-sharding-overlapping-pattern-4603
Jan 21, 2026
Merged

fix(cmd/gendao): fix overlapping shardingPattern matching issue#4631
gqcn merged 1 commit intogogf:masterfrom
lingcoder:fix/gendao-sharding-overlapping-pattern-4603

Conversation

@lingcoder
Copy link
Contributor

Summary

  • Fix overlapping shardingPattern matching issue where shorter patterns incorrectly match tables meant for longer patterns
  • Sort shardingPattern by length descending so longer (more specific) patterns are matched first
  • Add break after successful pattern match to prevent tables from matching multiple patterns

Problem

When shardingPattern contains overlapping prefixes like ["a_?", "a_b_?", "a_c_?"]:

  • Tables a_b_1, a_b_2 should match a_b_? and generate a_b.go
  • Tables a_c_1, a_c_2 should match a_c_? and generate a_c.go
  • Tables a_1, a_2 should match a_? and generate a.go

But without this fix, a_? (converted to regex a_(.+)) would match a_b_1 first, causing a_b_? and a_c_? patterns to fail to generate their respective dao files.

Solution

  1. Sort shardingPattern by length descending before matching
  2. Add break after a table matches a pattern to prevent multiple matches

Test plan

  • Added integration test Test_Gen_Dao_Sharding_Overlapping with overlapping patterns
  • Added SQL test data file sharding_overlapping.sql
  • Verified 3 separate dao files are generated: a.go, a_b.go, a_c.go

Fixes #4603

When shardingPattern contains overlapping prefixes like ["a_?", "a_b_?", "a_c_?"],
tables like "a_b_1" would incorrectly match "a_?" instead of the more specific
"a_b_?" pattern.

Changes:
- Sort shardingPattern by length descending so longer patterns match first
- Add break after successful pattern match to prevent multiple matches

This ensures tables are matched by the most specific (longest) pattern.
@gqcn gqcn merged commit 73560cf into gogf:master Jan 21, 2026
18 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.

gf gen dao 配置多个分表前缀,相互影响无法生成

2 participants