Improved reflection in editform

This commit is contained in:
2019-05-27 18:08:42 +09:00
parent ecfb9eea04
commit da3c064496
6 changed files with 46 additions and 45 deletions

View File

@ -1,24 +0,0 @@
package utils
import "github.com/OneOfOne/xxhash"
// HashString returns a hash of the string.
func HashString(item string) uint64 {
h := xxhash.NewS64(0)
h.Write([]byte(item))
return h.Sum64()
}
// HashStringsNoOrder returns a hash of the string slice contents, ignoring order.
func HashStringsNoOrder(items []string) uint64 {
sum := uint64(0)
for _, item := range items {
h := xxhash.NewS64(0)
h.Write([]byte(item))
numHash := h.Sum64()
sum += numHash
}
return sum
}

View File

@ -0,0 +1,14 @@
package utils
import "github.com/akyoto/hash"
// HashStringsNoOrder returns a hash of the string slice contents, ignoring order.
func HashStringsNoOrder(items []string) uint64 {
sum := uint64(0)
for _, item := range items {
sum += hash.String(item)
}
return sum
}

View File

@ -106,7 +106,20 @@ func RenderField(b *strings.Builder, v *reflect.Value, field reflect.StructField
// Embedded fields
if field.Anonymous {
RenderObject(b, fieldValue.Interface(), idPrefix)
// If it's exported, render it normally.
if field.PkgPath == "" {
RenderObject(b, fieldValue.Interface(), idPrefix)
}
// If it's an unexported struct, we try to access the field names
// given to us from the anonymous struct on the actual object.
if field.Type.Kind() == reflect.Struct {
for i := 0; i < field.Type.NumField(); i++ {
f := field.Type.Field(i)
RenderField(b, v, f, idPrefix)
}
}
return
}