public
anonymous / gist:978407
Created

render form

  • Download Gist
gistfile1.go
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
package web
 
import (
"reflect"
"strconv"
"strings"
"util"
)
 
const (
Create = iota
Edit
Show
)
 
//Render type
const (
RT_select = iota
RT_input
RT_text
RT_textarea
RT_hidden
)
 
type View struct {
Name string
Msg string
Fields []Field
}
 
type Form struct {
Name string
Action string
Type int //Create(0), Edit(1) ...
Msg string
Fields []Field
}
 
//TODO: may merge to FieldAttr
type Option struct {
Value string
Selected bool
}
 
type FieldAttr struct {
BoolAttr map[string]bool
StrAttr map[string]string
}
 
type Field struct {
Name, Value, Type string
RenderType int
ErrorType int
ErrorMsg string
Options []Option
FieldAttr FieldAttr
}
 
func indirect(v reflect.Value) reflect.Value {
for {
pv := v
if pv.Kind() != reflect.Ptr {
break
}
if pv.IsNil() {
pv.Set(reflect.Zero(pv.Type().Elem()).Addr())
}
v = pv.Elem()
}
return v
}
 
func handleFieldValue(v reflect.Value) string {
var val string
switch av := v; av.Kind() {
case reflect.Bool:
val = strconv.Btoa(av.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
val = strconv.Itoa64(av.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
val = strconv.Uitoa64(uint64(av.Uint()))
case reflect.Float32, reflect.Float64:
if av.Type().Size() == 4 {
val = strconv.Ftoa32(float32(av.Float()), 'f', 2)
} else {
val = strconv.Ftoa64(float64(av.Float()), 'f', 2)
}
case reflect.String:
val = av.String()
default:
println("default : unsupported type !!")
}
return val
}
 
func handleField(v reflect.Value, f reflect.StructField) Field {
var field Field
field.Name = f.Name
field.Type = f.Type.Name()
 
v = indirect(v)
switch av := v; av.Kind() {
case reflect.Bool:
field.Value = strconv.Btoa(av.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
field.Value = strconv.Itoa64(av.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
field.Value = strconv.Uitoa64(uint64(av.Uint()))
case reflect.Float32, reflect.Float64:
if av.Type().Size() == 4 {
field.Value = strconv.Ftoa32(float32(av.Float()), 'f', 2)
} else {
field.Value = strconv.Ftoa64(float64(av.Float()), 'f', 2)
}
case reflect.String:
field.Value = av.String()
case reflect.Map:
println("MapValue")
case reflect.Array, reflect.Slice:
println("ArrayOrSliceValue", av.Len())
for i := 0; i < av.Len(); i++ {
field.Options = append(field.Options, Option{handleFieldValue(av.Index(i)), true})
}
case reflect.Ptr:
println("PtrValue")
case reflect.Struct:
println("StructValue")
default:
println("default : unknown type for ", f.Name)
}
return field
}
 
func ShouldExclude(f string, e ...string) bool {
for _, val := range e {
if val == f {
return true
}
}
return false
}
 
func NewForm(i interface{}, exclude ...string) *Form {
var form Form
v := indirect(reflect.NewValue(i))
switch v.Kind() {
case reflect.Struct:
t := v.Type()
form.Name = t.Name()
form.Fields = []Field{}
for i := 0; i < v.NumField(); i++ {
f := t.Field(i)
if !ShouldExclude(f.Name, exclude...) {
fv := v.Field(i)
field := handleField(fv, f)
field.RenderType = RT_input //default
form.Fields = append(form.Fields, field)
}
}
default:
//util.Logger.Println("New: input needs to be struct type")
println("New: input needs to be struct type")
}
return &form
}
 
func (f *Field) renderSelect() string {
name := strings.ToLower(f.Name)
//text := "\t\t<span class=\"field\">\n"
text := "\t\t<label for=" + name + ">" + f.Name + ":</label>\n"
text += "\t\t<select name=\"" + name + "\">\n"
for _, o := range f.Options {
if o.Selected {
text += "\t\t<option selected=\"selected\">" + o.Value + "</option>\n"
} else {
text += "\t\t<option>" + o.Value + "</option>\n"
}
}
text += "\t\t</select>\n"
// text += "\t\t</span>\n\n"
return text
}
 
func (f *Field) renderInput() string {
//name := strings.ToLower(f.Name)
text := "\t\t<span class=\"field\">\n"
text += "\t\t<label for=" + f.Name + ">" + f.Name + ":</label>\n"
text += "\t\t<input type=text id=" + f.Name + " name=" + f.Name + " value=" + f.Value + ">\n"
if f.ErrorType != util.NOERROR {
text += "\t\t<span class=\"errormsg\">" + f.ErrorMsg + "</span>\n"
}
text += "\t\t</span>\n\n"
return text
}
 
func (f *Field) renderTextarea() string {
text := "\t\t<span class=\"field\">\n"
text += "\t\t<label for=" + f.Name + ">" + f.Name + ":</label>\n"
text += "\t\t<textarea id=" + f.Name + " name=" + f.Name + " value=" + f.Value + "></textarea>\n"
text += "\t\t</span>\n\n"
return text
}
 
func (f *Field) renderHidden() string {
return "\t\t<input type=hidden id=" + f.Name + " name=" + f.Name + " value=" + f.Value + ">\n"
}
 
 
//TODO: legend
func (f *Form) Render() string {
var form string
form += "<form id=ui.form." + f.Name + " method=\"post\" action=\"" + f.Action + "\">\n\t<fieldset>\n\t<legend>Enter details</legend>\n\n"
for _, v := range f.Fields {
form += v.render()
}
if f.Type == Create {
form += "\t</fieldset>\n\t<fieldset>\n\t\t<button type=submit>Create</button>\n\t</fieldset>\n"
} else {
form += "\t</fieldset>\n\t<fieldset>\n\t\t<button type=submit>Save</button>\n\t</fieldset>\n"
}
form += "\t</form>"
return form
}
 
//TODO: not sure about setting selection for multiple select
func (f *Form) SetOptionField(fn string, o []Option, preserve bool) {
for i := 0; i < len(f.Fields); i++ {
v := &f.Fields[i]
if v.Name == fn {
v.RenderType = RT_select
if preserve {
for i := range o {
c := &o[i]
if v.Value == c.Value {
c.Selected = true
}
}
}
v.Options = o
}
}
}
 
func (f *Form) MapValidation(val util.Reply) {
for _, vf := range val.GetFields() {
//for _, ff := range f.Fields { -- cannot use range because copy by value
for i := 0; i < len(f.Fields); i++ {
ff := &f.Fields[i]
if ff.Name == vf.Name {
if vf.ErrorType != util.NOERROR {
ff.ErrorType = vf.ErrorType
ff.ErrorMsg = vf.ErrorMsg
}
}
}
}
}
 
func NewView(i interface{}, exclude ...string) *View {
var view View
nv := reflect.NewValue(i)
v := indirect(nv)
t := v.Type()
view.Name = t.Name()
for i := 0; i < v.NumField(); i++ {
f := t.Field(i)
if !ShouldExclude(f.Name, exclude...) {
fv := v.Field(i)
field := handleField(fv, f)
field.RenderType = RT_text //default
view.Fields = append(view.Fields, field)
}
}
return &view
}
 
func (f *Field) renderText() string {
name := strings.ToLower(f.Name)
//text := "\t\t<span class=\"field\">"
text := "\t\t<label for=" + name + ">" + f.Name + ":</label>"
text += "<span id=" + name + " >" + f.Value + "</span>\n\n"
//text += "\t\t</span>"
return text
 
}
 
func (f *Field) render() string {
doc := ""
switch f.RenderType {
case RT_input:
doc = f.renderInput()
case RT_select:
doc = f.renderSelect()
case RT_text:
doc = f.renderText()
case RT_textarea:
doc = f.renderTextarea()
case RT_hidden:
doc = f.renderHidden()
}
return doc
}
 
 
func (v *View) Render() string {
doc := "<div id=ui.doc." + v.Name + " class=show" + ">\n <fieldset>\n\t<legend>Details</legend>\n\n"
for _, field := range v.Fields {
doc += field.render()
}
doc += " </fieldset>\n <fieldset>\n\t<button type=submit>Edit</button>\n </fieldset>\n"
doc += "</div>"
return doc
}
 
func (f *Form) SetRenderType(fn string, rt int, fop FieldAttr) {
for i, v := range f.Fields {
if v.Name == fn {
f.Fields[i].RenderType = rt
f.Fields[i].FieldAttr = fop
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.