Skip to content

Commit

Permalink
Belongs to, Has one with struct name Done, foreign key tag pending
Browse files Browse the repository at this point in the history
  • Loading branch information
pelusa-v committed May 22, 2024
1 parent f041f31 commit aad28eb
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func main() {
admin.RegisterModel(samples.Product{})
admin.RegisterModel(samples.Car{})
admin.RegisterModel(samples.Blog{})
admin.RegisterModel(samples.Employee{})
admin.RegisterModel(samples.Company{})

// db := samples.NewDbInstance()
// samples.TestListHandler(db)
Expand Down
6 changes: 4 additions & 2 deletions src/pkg/data/forms_data.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package data

import "reflect"

type FormData struct {
SimpleInputs []SimpleInput
SelectInputs []SelectInput
Expand All @@ -24,8 +26,8 @@ type SelectInputOption struct {
Value string
}

func (form *FormData) SetFormInputs(model *DbModel) {
fields := GetObjectFields(model.modelType)
func (form *FormData) SetFormInputs(model *DbModel, allTypes *[]reflect.Type) {
fields := GetObjectFields(model.modelType, allTypes)
for _, f := range fields {
input := SimpleInput{}
input.Id = f.Name
Expand Down
6 changes: 3 additions & 3 deletions src/pkg/data/models_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ type ModelObject struct {
TypeName string
}

func MapModelObject(o interface{}) ModelObject {
func MapModelObject(o interface{}, allTypes *[]reflect.Type) ModelObject {
objectValue := reflect.ValueOf(o)
if objectValue.Kind() == reflect.Ptr {
objectValue = objectValue.Elem()
}

objectFields := GetObjectFields(objectValue.Type())
objectFieldsValues := GetObjectFieldsValues(objectValue)
objectFields := GetObjectFields(objectValue.Type(), allTypes)
objectFieldsValues := GetObjectFieldsValues(objectValue, allTypes)
pkField := FindPkField(objectFields)

return ModelObject{
Expand Down
8 changes: 4 additions & 4 deletions src/pkg/data/templates_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (manager *TemplateManager) GetHomePageData() HomePageData {

func (manager *TemplateManager) GetModelDetailPageData(model DbModel) ModelDetailPageData {
modelType := model.modelType
modelFields := GetObjectFields(modelType)
modelFields := GetObjectFields(modelType, manager.configurableData.Models)
data := ModelDetailPageData{Model: modelType.Name(), ModelObjectsFields: modelFields, PreviousURL: "/admin",
AddURL: fmt.Sprintf("/admin/%s/actions/create", model.modelType.Name())}
data.Models = manager.GetSidebarModels()
Expand All @@ -58,7 +58,7 @@ func (manager *TemplateManager) GetModelDetailPageData(model DbModel) ModelDetai
objects := model.ListObjects()
for _, o := range objects {
modelObjectListItem := ModelObjectListItem{}
modelObject := MapModelObject(o)
modelObject := MapModelObject(o, manager.configurableData.Models)
modelObjectListItem.ModelObject = modelObject
modelObjectListItem.DetailURL = fmt.Sprintf("/admin/%s/%v", modelObject.TypeName, modelObject.Pk)
modelObjectListItem.DeleteURL = fmt.Sprintf("/admin/%s/actions/delete/%v", modelObject.TypeName, modelObject.Pk)
Expand All @@ -77,7 +77,7 @@ func (manager *TemplateManager) GetModelDetailPageData(model DbModel) ModelDetai

func (manager *TemplateManager) GetModelObjectDetailPageData(model DbModel, pk string) ModelObjectDetailPageData {
object := model.GetObject(pk)
data := ModelObjectDetailPageData{Model: model.modelType.Name(), ModelObject: MapModelObject(object),
data := ModelObjectDetailPageData{Model: model.modelType.Name(), ModelObject: MapModelObject(object, manager.configurableData.Models),
PreviousURL: fmt.Sprintf("/admin/%s", model.modelType.Name())}
data.Models = manager.GetSidebarModels()
data.AdminName = manager.GetSidebarName()
Expand All @@ -89,7 +89,7 @@ func (manager *TemplateManager) GetModelObjectCreatePageData(model DbModel) Mode
SimpleInputs: make([]SimpleInput, 0),
SelectInputs: make([]SelectInput, 0),
}
templateForm.SetFormInputs(&model)
templateForm.SetFormInputs(&model, manager.configurableData.Models)

data := ModelObjectCreatePageData{Model: model.modelType.Name(), PreviousURL: fmt.Sprintf("/admin/%s", model.modelType.Name()),
CreateObjectURL: fmt.Sprintf("/admin/%s/actions/create", model.modelType.Name()), CreateObjectForm: *templateForm,
Expand Down
25 changes: 21 additions & 4 deletions src/pkg/data/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ func IsPkField(f reflect.StructField) bool {
return f.Tag.Get(GORM_DEFAULT_TAG_NAME) == GORM_PK_DEFAULT_TAG_VALUE || f.Name == GORM_PK_DEFAULT_NAME
}

func IsVirtualField(f reflect.StructField, allTypes *[]reflect.Type) bool {
for _, t := range *allTypes {
if f.Name == t.Name() {
return true
}
}
return false
}

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))
Expand Down Expand Up @@ -72,7 +81,7 @@ func FindPkField(fields []reflect.StructField) reflect.StructField {
panic("Gorm model doesn't have PK")
}

func GetObjectFieldsValues(objectValue reflect.Value) []reflect.Value {
func GetObjectFieldsValues(objectValue reflect.Value, allTypes *[]reflect.Type) []reflect.Value {
if objectValue.Kind() == reflect.Ptr {
objectValue = objectValue.Elem()
}
Expand All @@ -84,8 +93,12 @@ func GetObjectFieldsValues(objectValue reflect.Value) []reflect.Value {
fieldType := objectType.Field(i)
fieldValue := objectValue.Field(i)

if IsVirtualField(fieldType, allTypes) {
continue
}

if FieldHasEmbeddedStructs(fieldType) {
embeddedFieldsValues := GetObjectFieldsValues(fieldValue)
embeddedFieldsValues := GetObjectFieldsValues(fieldValue, allTypes)
objectFieldsValues = append(objectFieldsValues, embeddedFieldsValues...)
} else {
objectFieldsValues = append(objectFieldsValues, fieldValue)
Expand All @@ -95,7 +108,7 @@ func GetObjectFieldsValues(objectValue reflect.Value) []reflect.Value {
return objectFieldsValues
}

func GetObjectFields(objectType reflect.Type) []reflect.StructField {
func GetObjectFields(objectType reflect.Type, allTypes *[]reflect.Type) []reflect.StructField {
var objectFields []reflect.StructField

for i := 0; i < objectType.NumField(); i++ {
Expand All @@ -105,8 +118,12 @@ func GetObjectFields(objectType reflect.Type) []reflect.StructField {
// AddEmbeddedPrefixToField(&fieldType)
// }

if IsVirtualField(fieldType, allTypes) {
continue
}

if FieldHasEmbeddedStructs(fieldType) {
embeddedFields := GetObjectFields(fieldType.Type)
embeddedFields := GetObjectFields(fieldType.Type, allTypes)
objectFields = append(objectFields, embeddedFields...)
} else {
objectFields = append(objectFields, fieldType)
Expand Down
2 changes: 1 addition & 1 deletion src/samples/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewDbInstance() *gorm.DB {
return nil
}

db.AutoMigrate(&User{}, &Product{}, &Car{}, &Blog{})
db.AutoMigrate(&User{}, &Product{}, &Car{}, &Blog{}, &Employee{}, &Company{})
return db
}

Expand Down
12 changes: 12 additions & 0 deletions src/samples/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,15 @@ type Blog struct {
Author Author `gorm:"embedded"`
Upvotes int32
}

type Employee struct {
gorm.Model
Name string
CompanyID int
Company Company
}

type Company struct {
ID int
Name string
}

0 comments on commit aad28eb

Please sign in to comment.