Skip to content

Commit

Permalink
Model's embedded fields Done
Browse files Browse the repository at this point in the history
  • Loading branch information
pelusa-v committed May 15, 2024
1 parent af1a1e4 commit 4441059
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func main() {
admin.RegisterModel(samples.User{})
admin.RegisterModel(samples.Product{})
admin.RegisterModel(samples.Car{})
admin.RegisterModel(samples.Blog{})

// db := samples.NewDbInstance()
// samples.TestListHandler(db)
Expand Down
6 changes: 6 additions & 0 deletions src/pkg/data/forms_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@ func (form *FormData) SetFormInputs(model *DbModel) {
// input.Disabled = IsPkField(f)
input.Type = GetHtmlInputType(f)
form.SimpleInputs = append(form.SimpleInputs, input)

// fmt.Println(f.Tag)
// fmt.Println(f.Name)
// fmt.Println(f.Index)
// fmt.Println(f.Type)
// fmt.Println("---------------")
}
}
37 changes: 25 additions & 12 deletions src/pkg/data/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package data

import (
"encoding/json"
"fmt"
"reflect"
"regexp"
"strings"
"time"

"gorm.io/gorm"
Expand All @@ -15,17 +18,31 @@ const HTML_INPUT_DATETIME_TYPE = "datetime-local"
const HTML_INPUT_BOOL_TYPE = "checkbox"
const HTML_INPUT_NUMBER_TYPE = "number"

const GORM_PK_DEFAULT_TAG_NAME string = "gorm"
const GORM_DEFAULT_TAG_NAME string = "gorm"
const GORM_PK_DEFAULT_TAG_VALUE string = "primaryKey"
const GORM_PK_DEFAULT_NAME string = "ID"
const GORM_EMBEDDED_DEFAULT_TAG_VALUE string = "embedded"
const GORM_EMBEDDED_PREFIX_DEFAULT_TAG_VALUE string = "embeddedPrefix"

func FieldHasEmbeddedStructs(f reflect.StructField) bool {
// return f.Anonymous || f.Type == reflect.TypeOf(gorm.DeletedAt{})
return f.Anonymous
return f.Anonymous ||
strings.Contains(f.Tag.Get(GORM_DEFAULT_TAG_NAME), GORM_EMBEDDED_DEFAULT_TAG_VALUE)
}

func FieldHasEmbeddedPrefix(f reflect.StructField) bool {
return strings.Contains(f.Tag.Get(GORM_DEFAULT_TAG_NAME), GORM_EMBEDDED_PREFIX_DEFAULT_TAG_VALUE)
}

func IsPkField(f reflect.StructField) bool {
return f.Tag.Get(GORM_PK_DEFAULT_TAG_NAME) == GORM_PK_DEFAULT_TAG_VALUE || f.Name == GORM_PK_DEFAULT_NAME
return f.Tag.Get(GORM_DEFAULT_TAG_NAME) == GORM_PK_DEFAULT_TAG_VALUE || f.Name == GORM_PK_DEFAULT_NAME
}

func AddEmbeddedPrefixToField(f *reflect.StructField) {
re := regexp.MustCompile(fmt.Sprintf(`%s:([^";]+)`, GORM_EMBEDDED_PREFIX_DEFAULT_TAG_VALUE))
matches := re.FindStringSubmatch(f.Tag.Get(GORM_DEFAULT_TAG_NAME))
if len(matches) > 1 {
f.Name = matches[1]
}
}

func GetHtmlInputType(f reflect.StructField) string {
Expand Down Expand Up @@ -84,6 +101,10 @@ func GetObjectFields(objectType reflect.Type) []reflect.StructField {
for i := 0; i < objectType.NumField(); i++ {
fieldType := objectType.Field(i)

// if FieldHasEmbeddedPrefix(fieldType) {
// AddEmbeddedPrefixToField(&fieldType)
// }

if FieldHasEmbeddedStructs(fieldType) {
embeddedFields := GetObjectFields(fieldType.Type)
objectFields = append(objectFields, embeddedFields...)
Expand All @@ -95,14 +116,6 @@ func GetObjectFields(objectType reflect.Type) []reflect.StructField {
return objectFields
}

// func GetTypesNames(objectsTypes *[]reflect.Type) []string {
// var names []string
// for _, objectType := range *objectsTypes {
// names = append(names, objectType.Name())
// }
// return names
// }

func GetObjectInstanceFromBytes(data []byte, typ reflect.Type) (interface{}, error) {
instancePtr := reflect.New(typ).Interface() // Create a new pointer to a type instance

Expand Down
17 changes: 17 additions & 0 deletions src/samples/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ type Car struct {
Number string
Passengers int
}

type Hint struct {
Test string
}

type Author struct {
Name string
Email string
// Hint Hint `gorm:"embedded;embeddedPrefix:hint_"`
Hint Hint `gorm:"embedded"`
}

type Blog struct {
ID int
Author Author `gorm:"embedded"`
Upvotes int32
}

0 comments on commit 4441059

Please sign in to comment.