|
| 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 | +} |
0 commit comments