Skip to content

Commit 64a3b13

Browse files
committed
refactor for 2.1
1 parent 9097bba commit 64a3b13

11 files changed

Lines changed: 642 additions & 529 deletions

File tree

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ require (
66
github.com/gertd/go-pluralize v0.2.1
77
github.com/spf13/cobra v1.9.1
88
golang.org/x/text v0.28.0
9+
gorm.io/gorm v1.30.2
910
)
1011

1112
require (
1213
github.com/inconshreveable/mousetrap v1.1.0 // indirect
14+
github.com/jinzhu/inflection v1.0.0 // indirect
15+
github.com/jinzhu/now v1.1.5 // indirect
1316
github.com/spf13/pflag v1.0.7 // indirect
1417
)
1518

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ github.com/gertd/go-pluralize v0.2.1 h1:M3uASbVjMnTsPb0PNqg+E/24Vwigyo/tvyMTtAlL
33
github.com/gertd/go-pluralize v0.2.1/go.mod h1:rbYaKDbsXxmRfr8uygAEKhOWsjyrrqrkHVpZvoOp8zk=
44
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
55
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
6+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
7+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
8+
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
9+
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
610
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
711
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
812
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
@@ -13,3 +17,5 @@ golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
1317
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
1418
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1519
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
20+
gorm.io/gorm v1.30.2 h1:f7bevlVoVe4Byu3pmbWPVHnPsLoWaMjEb7/clyr9Ivs=
21+
gorm.io/gorm v1.30.2/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE=

utils/field_aliases.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package utils
2+
3+
import "strings"
4+
5+
// FieldTypeAlias represents a mapping from user-friendly aliases to canonical types
6+
type FieldTypeAlias struct {
7+
Alias string // User input (e.g., "image", "belongsTo", "manyToMany")
8+
CanonicalType string // Standardized type (e.g., "storage.Attachment", "belongs_to", "many_to_many")
9+
GoType string // Go type for struct fields
10+
Category string // "storage", "relationship", "basic", "translation"
11+
}
12+
13+
// FieldTypeAliases defines all supported field type aliases
14+
var FieldTypeAliases = []FieldTypeAlias{
15+
// Storage types
16+
{"image", "storage.Attachment", "*storage.Attachment", "storage"},
17+
{"file", "storage.Attachment", "*storage.Attachment", "storage"},
18+
{"attachment", "storage.Attachment", "*storage.Attachment", "storage"},
19+
{"*storage.Attachment", "storage.Attachment", "*storage.Attachment", "storage"},
20+
21+
// Translation types
22+
{"translation", "translation.Field", "translation.Field", "translation"},
23+
{"locale", "translation.Field", "translation.Field", "translation"},
24+
{"translatable", "translation.Field", "translation.Field", "translation"},
25+
{"translation.Field", "translation.Field", "translation.Field", "translation"},
26+
27+
// Basic types with aliases
28+
{"text", "string", "string", "basic"},
29+
{"email", "string", "string", "basic"},
30+
{"password", "string", "string", "basic"},
31+
{"url", "string", "string", "basic"},
32+
{"phone", "string", "string", "basic"},
33+
34+
// Relationship types - GORM standard names
35+
{"belongsTo", "belongs_to", "", "relationship"},
36+
{"belongs_to", "belongs_to", "", "relationship"},
37+
{"hasMany", "has_many", "", "relationship"},
38+
{"has_many", "has_many", "", "relationship"},
39+
{"hasOne", "has_one", "", "relationship"},
40+
{"has_one", "has_one", "", "relationship"},
41+
{"manyToMany", "many_to_many", "", "relationship"},
42+
{"many_to_many", "many_to_many", "", "relationship"},
43+
{"toMany", "many_to_many", "", "relationship"},
44+
{"to_many", "many_to_many", "", "relationship"},
45+
46+
// Date/time aliases
47+
{"datetime", "types.DateTime", "types.DateTime", "basic"},
48+
{"date", "types.DateTime", "types.DateTime", "basic"},
49+
{"timestamp", "types.DateTime", "types.DateTime", "basic"},
50+
51+
// Basic Go types
52+
{"string", "string", "string", "basic"},
53+
{"int", "int", "int", "basic"},
54+
{"uint", "uint", "uint", "basic"},
55+
{"bool", "bool", "bool", "basic"},
56+
{"float64", "float64", "float64", "basic"},
57+
{"time.Time", "time.Time", "time.Time", "basic"},
58+
}
59+
60+
// ResolveFieldType resolves a field type alias to its canonical form
61+
func ResolveFieldType(input string) FieldTypeAlias {
62+
// First, try exact match
63+
for _, alias := range FieldTypeAliases {
64+
if strings.EqualFold(alias.Alias, input) {
65+
return alias
66+
}
67+
}
68+
69+
// If no match found, treat as custom type
70+
return FieldTypeAlias{
71+
Alias: input,
72+
CanonicalType: input,
73+
GoType: input,
74+
Category: "custom",
75+
}
76+
}
77+
78+
// IsRelationshipType checks if a type is a relationship
79+
func IsRelationshipType(typeStr string) bool {
80+
resolved := ResolveFieldType(typeStr)
81+
return resolved.Category == "relationship"
82+
}
83+
84+
// IsStorageType checks if a type is a storage attachment
85+
func IsStorageType(typeStr string) bool {
86+
resolved := ResolveFieldType(typeStr)
87+
return resolved.Category == "storage"
88+
}
89+
90+
// IsTranslationType checks if a type is a translation field
91+
func IsTranslationType(typeStr string) bool {
92+
resolved := ResolveFieldType(typeStr)
93+
return resolved.Category == "translation"
94+
}
95+
96+
// GetCanonicalRelationship returns the canonical relationship name
97+
func GetCanonicalRelationship(input string) string {
98+
resolved := ResolveFieldType(input)
99+
if resolved.Category == "relationship" {
100+
return resolved.CanonicalType
101+
}
102+
return ""
103+
}
104+
105+
// GetGoTypeFromAlias returns the Go type for a field type using the alias system
106+
func GetGoTypeFromAlias(input string) string {
107+
resolved := ResolveFieldType(input)
108+
if resolved.GoType != "" {
109+
return resolved.GoType
110+
}
111+
return resolved.CanonicalType
112+
}
113+
114+
// GetStorageType returns the storage type if applicable
115+
func GetStorageType(input string) string {
116+
resolved := ResolveFieldType(input)
117+
if resolved.Category == "storage" {
118+
return resolved.CanonicalType
119+
}
120+
return ""
121+
}
122+
123+
// GetTranslationType returns the translation type if applicable
124+
func GetTranslationType(input string) string {
125+
resolved := ResolveFieldType(input)
126+
if resolved.Category == "translation" {
127+
return resolved.CanonicalType
128+
}
129+
return ""
130+
}
131+
132+
// IsManyToManyRelationship checks if the type represents a many-to-many relationship
133+
func IsManyToManyRelationship(typeStr string) bool {
134+
canonical := GetCanonicalRelationship(typeStr)
135+
return canonical == "many_to_many"
136+
}
137+
138+
// IsBelongsToRelationship checks if the type represents a belongs_to relationship
139+
func IsBelongsToRelationship(typeStr string) bool {
140+
canonical := GetCanonicalRelationship(typeStr)
141+
return canonical == "belongs_to"
142+
}
143+
144+
// IsHasManyRelationship checks if the type represents a has_many relationship
145+
func IsHasManyRelationship(typeStr string) bool {
146+
canonical := GetCanonicalRelationship(typeStr)
147+
return canonical == "has_many"
148+
}
149+
150+
// IsHasOneRelationship checks if the type represents a has_one relationship
151+
func IsHasOneRelationship(typeStr string) bool {
152+
canonical := GetCanonicalRelationship(typeStr)
153+
return canonical == "has_one"
154+
}

utils/helpers.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ func ToPlural(s string) string {
167167
return PluralizeClient.Plural(s)
168168
}
169169

170+
func TrimIdSuffix(s string) string {
171+
if strings.HasSuffix(s, "Id") && len(s) > 2 {
172+
return s[:len(s)-2]
173+
}
174+
return s
175+
}
176+
170177
func UpdateInitFile(singularName, pluralName string) error {
171178
initFilePath := "app/init.go"
172179

0 commit comments

Comments
 (0)