Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/util/modelHelper/modelHelper.go
akuma06 148f70b53f Models for User, Role and Comment
modelHelper for getting value from request and apply it to a Form
Getting value from a Form and apply to a Model
2017-05-06 17:55:02 +02:00

27 lignes
731 o
Go

package modelHelper
import (
"reflect"
"github.com/dorajistyle/goyangi/util/log"
)
func IsZeroOfUnderlyingType(x interface{}) bool {
return x == reflect.Zero(reflect.TypeOf(x)).Interface()
}
// AssignValue assign form values to model.
func AssignValue(model interface{}, form interface{}) {
modelIndirect := reflect.Indirect(reflect.ValueOf(model))
formElem := reflect.ValueOf(form).Elem()
typeOfTForm := formElem.Type()
for i := 0; i < formElem.NumField(); i++ {
modelField := modelIndirect.FieldByName(typeOfTForm.Field(i).Name)
if modelField.IsValid() {
formField := formElem.Field(i)
modelField.Set(formField)
} else {
log.Warnf("modelField : %s - %s", typeOfTForm.Field(i).Name, modelField)
}
}
}