@@ -7,6 +7,19 @@ import (
77 "strings"
88)
99
10+ const (
11+ tagName = "name"
12+ tagLabel = "label"
13+ tagPlaceholder = "placeholder"
14+ tagRequired = "required"
15+ tagInputType = "inputType"
16+ tagLegend = "legend"
17+ tagType = "type"
18+ tagStep = "step"
19+ tagRows = "rows"
20+ tagCols = "cols"
21+ )
22+
1023type (
1124 Enumerator interface { Enum () []any }
1225 Mapper interface {
@@ -56,6 +69,8 @@ func NewTransformer(model interface{}) (*Transformer, error) {
5669
5770 tr .Fields = fields
5871
72+ fmt .Printf ("tr: %+v\n " , tr .Fields )
73+
5974 return tr , nil
6075}
6176
@@ -72,16 +87,16 @@ func (t *Transformer) scanModel(rValue reflect.Value, rType reflect.Type, names
7287 for i := 0 ; i < rType .NumField (); i ++ {
7388 tags := rType .Field (i ).Tag
7489
75- name := tags .Get ("name" )
90+ name := tags .Get (tagName )
7691 if name == "" {
7792 name = rType .Field (i ).Name
7893 }
7994
8095 nname := append (names , name )
8196
8297 field := FormField {
83- Label : tags .Get ("label" ),
84- Placeholder : tags .Get ("placeholder" ),
98+ Label : tags .Get (tagLabel ),
99+ Placeholder : tags .Get (tagPlaceholder ),
85100 Name : strings .Join (nname , "." ),
86101 Value : rValue .Field (i ).Interface (),
87102 }
@@ -90,7 +105,7 @@ func (t *Transformer) scanModel(rValue reflect.Value, rType reflect.Type, names
90105 field .Label = name
91106 }
92107
93- if tags .Get ("required" ) == "true" {
108+ if tags .Get (tagRequired ) == "true" {
94109 field .Required = true
95110 }
96111
@@ -150,7 +165,7 @@ func (t *Transformer) scanModel(rValue reflect.Value, rType reflect.Type, names
150165 continue
151166 }
152167
153- inputType := InputFieldType (tags .Get ("inputType" ))
168+ inputType := InputFieldType (tags .Get (tagInputType ))
154169
155170 fType := rType .Field (i ).Type
156171 fValue := rValue .Field (i )
@@ -167,14 +182,23 @@ func (t *Transformer) scanModel(rValue reflect.Value, rType reflect.Type, names
167182
168183 switch fType .Kind () {
169184 case reflect .String :
170-
171185 if inputType == "" {
172186 inputType = InputFieldTypeText
173187 }
174188
175- field .Type = FieldTypeInput
176- field .InputType = inputType
189+ typ := FieldType (tags .Get (tagType ))
190+ if typ == "" {
191+ typ = FieldTypeInput
192+ }
177193
194+ field .Type = typ
195+ field .InputType = inputType
196+ if tags .Get (tagRows ) != "" {
197+ field .Rows = tags .Get (tagRows )
198+ }
199+ if tags .Get (tagCols ) != "" {
200+ field .Cols = tags .Get (tagCols )
201+ }
178202 case reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 ,
179203 reflect .Uint , reflect .Uint8 , reflect .Uint16 , reflect .Uint32 , reflect .Uint64 :
180204
@@ -184,36 +208,40 @@ func (t *Transformer) scanModel(rValue reflect.Value, rType reflect.Type, names
184208
185209 field .Type = FieldTypeInput
186210 field .InputType = inputType
187- field .Step = "1"
188211
212+ if tags .Get (tagStep ) != "" {
213+ field .Step = tags .Get (tagStep )
214+ } else {
215+ field .Step = "1"
216+ }
189217 case reflect .Float32 , reflect .Float64 :
190218 if inputType == "" {
191219 inputType = InputFieldTypeNumber
192220 }
193221
194222 field .Type = FieldTypeInput
195223 field .InputType = inputType
196- field .Step = "any"
197224
198- case reflect .Bool :
199- fieldType := FieldTypeCheckbox
200- if len (names ) > 0 && names [len (names )- 1 ] == name {
201- // radio-options use the same 'name' as their parent for grouping
202- fieldType = FieldTypeRadios
225+ if tags .Get (tagStep ) != "" {
226+ field .Step = tags .Get (tagStep )
227+ } else {
228+ field .Step = "any"
203229 }
204-
205- field .Type = fieldType
230+ case reflect . Bool :
231+ field .Type = FieldTypeCheckbox
206232 case reflect .Slice , reflect .Array :
207233 case reflect .Map :
208234 case reflect .Struct :
209235 field .Type = FieldTypeGroup
210- field .Legend = tags .Get ("legend" )
236+ field .Legend = tags .Get (tagLegend )
211237
212238 var err error
213239 field .Fields , err = t .scanModel (fValue , fType , nname ... )
214240 if err != nil {
215241 return nil , err
216242 }
243+ default :
244+ return nil , fmt .Errorf ("unsupported type: %s" , fType .Kind ())
217245 }
218246
219247 fields = append (fields , field )
0 commit comments