@@ -2,18 +2,34 @@ package form
22
33import (
44 "reflect"
5+ "sort"
56 "strings"
67 "sync"
78 "sync/atomic"
89)
910
11+ type cacheFields []cachedField
12+
13+ func (s cacheFields ) Len () int {
14+ return len (s )
15+ }
16+
17+ func (s cacheFields ) Less (i , j int ) bool {
18+ return ! s [i ].isAnonymous
19+ }
20+
21+ func (s cacheFields ) Swap (i , j int ) {
22+ s [i ], s [j ] = s [j ], s [i ]
23+ }
24+
1025type cachedField struct {
11- idx int
12- name string
26+ idx int
27+ name string
28+ isAnonymous bool
1329}
1430
1531type cachedStruct struct {
16- fields [] cachedField
32+ fields cacheFields
1733}
1834
1935type structCacheMap struct {
@@ -74,6 +90,7 @@ func (s *structCacheMap) parseStruct(mode Mode, current reflect.Value, key refle
7490
7591 fld = typ .Field (i )
7692
93+ // fmt.Println("PkgPath:", fld.PkgPath, " Anonymous:", fld.Anonymous, " Name:", fld.Name)
7794 if fld .PkgPath != blank && ! fld .Anonymous {
7895 continue
7996 }
@@ -88,6 +105,7 @@ func (s *structCacheMap) parseStruct(mode Mode, current reflect.Value, key refle
88105 }
89106 }
90107
108+ // fmt.Println("Ignore:", name == ignore)
91109 if name == ignore {
92110 continue
93111 }
@@ -100,9 +118,10 @@ func (s *structCacheMap) parseStruct(mode Mode, current reflect.Value, key refle
100118 name = fld .Name
101119 }
102120
103- cs .fields = append (cs .fields , cachedField {idx : i , name : name })
121+ cs .fields = append (cs .fields , cachedField {idx : i , name : name , isAnonymous : fld . Anonymous })
104122 }
105123
124+ sort .Sort (cs .fields )
106125 s .Set (typ , cs )
107126
108127 s .lock .Unlock ()
0 commit comments