//Magical method that creates form for editing based on provided value func Edit(value interface{}, rules map[string]string, injectInto string, provides string, title string) *element.View { view := &element.View{Template: "edit.tpl", InjectInto: injectInto, Provides: provides, Model: &element.Model{MAP: make(map[string]interface{})}} view.MAP["Title"] = title fieldset := NewFieldset() typ := reflect.TypeOf(value).Elem() val := reflect.Indirect(reflect.ValueOf(value)) // fmt.Printf("Converted typ %v\n", typ) fields := make([]*map[string]interface{}, 0) for i := 0; i < typ.NumField(); i++ { fieldName := typ.Field(i).Name // fmt.Printf("Field name %v\n", fieldName) fieldMap := make(map[string]interface{}) //map passed to field generator fieldMap["Label"] = fieldName fieldMap["Name"] = fieldName fieldMap["Value"] = val.Field(i).Interface() // fmt.Printf("Field map %v\n", fieldMap) fields = append(fields, &fieldMap) fieldRule := &ComponentRule{} var ruleTag string if rules != nil { //Process rules if tag, ok := rules[fieldName]; ok { fmt.Printf("Rule tag %v\n", ruleTag) ruleTag = tag } } //Execute rules fieldRule = execIgnoreRule(ruleTag, fieldRule) // fmt.Printf("Rule at the moment %v\n", fieldRule) fieldRule = execForeignRule(ruleTag, fieldMap, fieldRule) // fmt.Printf("Rule at the moment %v\n", fieldRule) fieldset = element.AddToView(fieldset, fieldRule.View) } view.MAP["Fields"] = fields view = element.AddToView(view, fieldset) view.MAP["Id"] = val.FieldByName("Id").Interface() // fmt.Printf("Fields generated %v\n", fields) return view }
//Component for creating New objects of a model, based on a type //Rules: map with rules for certain field //It is used for not polluting struct definition, you can set it after struct initialization and pass it to New handler //Use it like this: //Key: field name (retreived by reflection) //Value: Struct tag like: name:"value" name:"value" //Example: form ignore func New(typ reflect.Type, rules map[string]string, injectInto string, title string) *element.View { view := &element.View{Template: "new.tpl", InjectInto: injectInto, Model: &element.Model{MAP: make(map[string]interface{})}} view.Model.MAP["Title"] = title var fieldset *element.View fieldset = NewFieldset() fields := make([]map[string]interface{}, 0) fmt.Printf("Number of fields %+v\n", typ.NumField()) for i := 0; i < typ.NumField(); i++ { fieldName := typ.Field(i).Name fmt.Printf("Field name %v\n", fieldName) fieldMap := make(map[string]interface{}) //map passed to field generator fieldMap["Label"] = fieldName fieldMap["Name"] = fieldName // fmt.Printf("Field map %v\n", fieldMap) fields = append(fields, fieldMap) fieldRule := &ComponentRule{} var ruleTag string if rules != nil { //Process rules if tag, ok := rules[fieldName]; ok { fmt.Printf("Rule tag %v\n", ruleTag) ruleTag = tag } } //Execute rules fieldRule = execIgnoreRule(ruleTag, fieldRule) // fmt.Printf("Rule at the moment %v\n", fieldRule) fieldRule = execForeignRule(ruleTag, fieldMap, fieldRule) // fmt.Printf("Rule at the moment %v\n", fieldRule) fieldset = element.AddToView(fieldset, fieldRule.View) } view.Model.MAP["Fields"] = fields view = element.AddToView(view, fieldset) fmt.Printf("Fields generated %v\n", fields) return view }