Skip to content

Commit ab6b8a7

Browse files
committed
Update template.go
1 parent dd50d80 commit ab6b8a7

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

utils/template.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ func HasFieldType(fields []Field, fieldType string) bool {
187187
return false
188188
}
189189

190+
// HasImageField checks if any field has image type
191+
func HasImageField(fields []Field) bool {
192+
return HasFieldType(fields, "*storage.Attachment")
193+
}
194+
195+
// HasMediaField checks if any field has media type
196+
func HasMediaField(fields []Field) bool {
197+
for _, field := range fields {
198+
if field.Type == "media.Media" {
199+
return true
200+
}
201+
}
202+
return false
203+
}
204+
190205
// Singularize converts plural to singular (basic implementation)
191206
func Singularize(word string) string {
192207
if strings.HasSuffix(word, "ies") {
@@ -279,6 +294,9 @@ func parseFieldDef(fieldDef string) Field {
279294
case "storage":
280295
field.JSONName = ToSnakeCase(fieldName) + ",omitempty"
281296
field.GORMTag = `gorm:"foreignKey:ModelId;references:Id"`
297+
case "media":
298+
field.JSONName = ToSnakeCase(fieldName) + ",omitempty"
299+
field.GORMTag = `gorm:"foreignKey:ModelId;references:Id"`
282300
case "translation":
283301
// Translation fields are stored as translation.Field and handled like storage attachments
284302
field.Type = resolved.GoType
@@ -377,6 +395,7 @@ func GenerateFileFromTemplate(dir, filename, templateName string, naming *Naming
377395
*NamingConvention
378396
Fields []Field
379397
HasImageField bool
398+
HasMediaField bool
380399
HasTranslatableFields bool
381400
HasSoftDelete bool
382401
HasTimestamps bool
@@ -390,6 +409,7 @@ func GenerateFileFromTemplate(dir, filename, templateName string, naming *Naming
390409
NamingConvention: naming,
391410
Fields: fields,
392411
HasImageField: HasImageField(fields),
412+
HasMediaField: HasMediaField(fields),
393413
HasTranslatableFields: HasFieldType(fields, "translation.Field"),
394414
HasSoftDelete: HasFieldType(fields, "gorm.DeletedAt"),
395415
HasTimestamps: HasFieldType(fields, "time.Time"),
@@ -408,8 +428,3 @@ func GenerateFileFromTemplate(dir, filename, templateName string, naming *Naming
408428

409429
fmt.Printf("Generated %s\n", outputFile)
410430
}
411-
412-
// HasImageField checks if any field has image type
413-
func HasImageField(fields []Field) bool {
414-
return HasFieldType(fields, "*storage.Attachment")
415-
}

0 commit comments

Comments
 (0)