|
1 | 1 | package schema_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
4 | 5 | "sync" |
5 | 6 | "testing" |
6 | 7 |
|
@@ -227,3 +228,72 @@ func TestEmbeddedStruct(t *testing.T) { |
227 | 228 | }) |
228 | 229 | } |
229 | 230 | } |
| 231 | + |
| 232 | +type CustomizedNamingStrategy struct { |
| 233 | + schema.NamingStrategy |
| 234 | +} |
| 235 | + |
| 236 | +func (ns CustomizedNamingStrategy) ColumnName(table, column string) string { |
| 237 | + baseColumnName := ns.NamingStrategy.ColumnName(table, column) |
| 238 | + |
| 239 | + if table == "" { |
| 240 | + return baseColumnName |
| 241 | + } |
| 242 | + |
| 243 | + s := strings.Split(table, "_") |
| 244 | + |
| 245 | + var prefix string |
| 246 | + switch len(s) { |
| 247 | + case 1: |
| 248 | + prefix = s[0][:3] |
| 249 | + case 2: |
| 250 | + prefix = s[0][:1] + s[1][:2] |
| 251 | + default: |
| 252 | + prefix = s[0][:1] + s[1][:1] + s[2][:1] |
| 253 | + } |
| 254 | + return prefix + "_" + baseColumnName |
| 255 | +} |
| 256 | + |
| 257 | +func TestEmbeddedStructForCustomizedNamingStrategy(t *testing.T) { |
| 258 | + type CorpBase struct { |
| 259 | + gorm.Model |
| 260 | + OwnerID string |
| 261 | + } |
| 262 | + |
| 263 | + type Company struct { |
| 264 | + ID int |
| 265 | + OwnerID int |
| 266 | + Name string |
| 267 | + Ignored string `gorm:"-"` |
| 268 | + } |
| 269 | + |
| 270 | + type Corp struct { |
| 271 | + CorpBase |
| 272 | + Base Company `gorm:"embedded;embeddedPrefix:company_"` |
| 273 | + } |
| 274 | + |
| 275 | + cropSchema, err := schema.Parse(&Corp{}, &sync.Map{}, CustomizedNamingStrategy{schema.NamingStrategy{}}) |
| 276 | + |
| 277 | + if err != nil { |
| 278 | + t.Fatalf("failed to parse embedded struct with primary key, got error %v", err) |
| 279 | + } |
| 280 | + |
| 281 | + fields := []schema.Field{ |
| 282 | + {Name: "ID", DBName: "cor_id", BindNames: []string{"CorpBase", "Model", "ID"}, DataType: schema.Uint, PrimaryKey: true, Size: 64, HasDefaultValue: true, AutoIncrement: true, TagSettings: map[string]string{"PRIMARYKEY": "PRIMARYKEY"}}, |
| 283 | + {Name: "ID", DBName: "company_cor_id", BindNames: []string{"Base", "ID"}, DataType: schema.Int, Size: 64, TagSettings: map[string]string{"EMBEDDED": "EMBEDDED", "EMBEDDEDPREFIX": "company_"}}, |
| 284 | + {Name: "Name", DBName: "company_cor_name", BindNames: []string{"Base", "Name"}, DataType: schema.String, TagSettings: map[string]string{"EMBEDDED": "EMBEDDED", "EMBEDDEDPREFIX": "company_"}}, |
| 285 | + {Name: "Ignored", BindNames: []string{"Base", "Ignored"}, TagSettings: map[string]string{"-": "-", "EMBEDDED": "EMBEDDED", "EMBEDDEDPREFIX": "company_"}}, |
| 286 | + {Name: "OwnerID", DBName: "company_cor_owner_id", BindNames: []string{"Base", "OwnerID"}, DataType: schema.Int, Size: 64, TagSettings: map[string]string{"EMBEDDED": "EMBEDDED", "EMBEDDEDPREFIX": "company_"}}, |
| 287 | + {Name: "OwnerID", DBName: "cor_owner_id", BindNames: []string{"CorpBase", "OwnerID"}, DataType: schema.String}, |
| 288 | + } |
| 289 | + |
| 290 | + for _, f := range fields { |
| 291 | + checkSchemaField(t, cropSchema, &f, func(f *schema.Field) { |
| 292 | + if f.Name != "Ignored" { |
| 293 | + f.Creatable = true |
| 294 | + f.Updatable = true |
| 295 | + f.Readable = true |
| 296 | + } |
| 297 | + }) |
| 298 | + } |
| 299 | +} |
0 commit comments