github.com/keysonzzz/kmg@v0.0.0-20151121023212-05317bfd7d39/webTypeAdmin/mapType.go (about)

     1  package webTypeAdmin
     2  
     3  import (
     4  	"html/template"
     5  	"reflect"
     6  
     7  	"github.com/bronze1man/kmg/kmgType"
     8  )
     9  
    10  //path -> key(Key type)
    11  //key can be bool,string,stringEnum,int,float,
    12  type mapType struct {
    13  	kmgType.MapType
    14  	elemAdminType adminType
    15  	ctx           *context
    16  }
    17  
    18  //key is not string?
    19  func (t *mapType) init() (err error) {
    20  	if t.elemAdminType != nil {
    21  		return
    22  	}
    23  	if err = t.MapType.Init(); err != nil {
    24  		return
    25  	}
    26  	t.elemAdminType, err = t.ctx.typeOfFromKmgType(t.MapType.ElemType)
    27  	if err != nil {
    28  		return
    29  	}
    30  	return nil
    31  }
    32  func (t *mapType) HtmlView(v reflect.Value) (html template.HTML, err error) {
    33  	if err = t.init(); err != nil {
    34  		return
    35  	}
    36  	type templateRow struct {
    37  		Path string
    38  		Key  string
    39  		Html template.HTML
    40  	}
    41  	var templateData []templateRow
    42  	for _, key := range v.MapKeys() {
    43  		keyS := t.KeyStringConverter.ToString(key)
    44  		val := v.MapIndex(key)
    45  		if html, err = t.elemAdminType.HtmlView(val); err != nil {
    46  			return
    47  		}
    48  		templateData = append(templateData, templateRow{
    49  			Path: keyS,
    50  			Key:  keyS,
    51  			Html: html,
    52  		})
    53  	}
    54  	return theTemplate.ExecuteNameToHtml("Map", templateData)
    55  }