Fix: Handling of exported struct fields
This commit is contained in:
parent
e822dd173a
commit
0f098d731c
15
marshal.go
15
marshal.go
|
@ -244,11 +244,22 @@ func encRegister(e interface{}) (err error) {
|
|||
}
|
||||
encoderCache[string(t.Kind())] = f
|
||||
case reflect.Struct:
|
||||
zt := reflect.New(t)
|
||||
n := zt.Elem().NumField()
|
||||
mfields := []int{}
|
||||
for i := 0; i < n; i++ {
|
||||
if zt.Elem().Field(i).CanSet() {
|
||||
mfields = append(mfields, i)
|
||||
}
|
||||
}
|
||||
if len(mfields) == 0 {
|
||||
return errors.New("ssob: No exported fields for " + string(t.Name()))
|
||||
}
|
||||
|
||||
f := func(e interface{}) (ret []byte, err error) {
|
||||
n := v.NumField()
|
||||
bs := make([][]byte, n)
|
||||
blen := 0
|
||||
for i := 0; i < n; i++ {
|
||||
for _, i := range mfields {
|
||||
bs[i], err = marshal(v.Field(i).Interface())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
15
unmarshal.go
15
unmarshal.go
|
@ -269,6 +269,18 @@ func decRegister(e interface{}) (err error) {
|
|||
}
|
||||
decoderCache[string(t.Kind())] = f
|
||||
case reflect.Struct:
|
||||
zt := reflect.New(t)
|
||||
n := zt.Elem().NumField()
|
||||
mfields := []int{}
|
||||
for i := 0; i < n; i++ {
|
||||
if zt.Elem().Field(i).CanSet() {
|
||||
mfields = append(mfields, i)
|
||||
}
|
||||
}
|
||||
if len(mfields) == 0 {
|
||||
return errors.New("ssob: No exported fields for " + string(t.Name()))
|
||||
}
|
||||
|
||||
f := func(e interface{}, in []byte) (n int, err error) {
|
||||
t := reflect.TypeOf(e)
|
||||
v := reflect.ValueOf(e)
|
||||
|
@ -279,8 +291,7 @@ func decRegister(e interface{}) (err error) {
|
|||
}
|
||||
|
||||
vi := reflect.Indirect(v)
|
||||
l := vi.NumField()
|
||||
for i := 0; i < l; i++ {
|
||||
for _, i := range mfields {
|
||||
ni, err := unmarshal(vi.Field(i).Addr().Interface(), in[pos:])
|
||||
pos += ni
|
||||
if err != nil {
|
||||
|
|
|
@ -354,11 +354,22 @@ func encRegisterUnsafe(e interface{}) (err error) {
|
|||
}
|
||||
unsafeEncoderCache[string(t.Kind())] = f
|
||||
case reflect.Struct:
|
||||
zt := reflect.New(t)
|
||||
n := zt.Elem().NumField()
|
||||
mfields := []int{}
|
||||
for i := 0; i < n; i++ {
|
||||
if zt.Elem().Field(i).CanSet() {
|
||||
mfields = append(mfields, i)
|
||||
}
|
||||
}
|
||||
if len(mfields) == 0 {
|
||||
return errors.New("ssob: No exported fields for " + string(t.Name()))
|
||||
}
|
||||
|
||||
f := func(e interface{}) (ret []byte, err error) {
|
||||
n := v.NumField()
|
||||
bs := make([][]byte, n)
|
||||
blen := 0
|
||||
for i := 0; i < n; i++ {
|
||||
for _, i := range mfields {
|
||||
bs[i], err = unsafeMarshal(v.Field(i).Interface())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -396,6 +396,18 @@ func decRegisterUnsafe(e interface{}) (err error) {
|
|||
}
|
||||
unsafeDecoderCache[string(t.Kind())] = f
|
||||
case reflect.Struct:
|
||||
zt := reflect.New(t)
|
||||
n := zt.Elem().NumField()
|
||||
mfields := []int{}
|
||||
for i := 0; i < n; i++ {
|
||||
if zt.Elem().Field(i).CanSet() {
|
||||
mfields = append(mfields, i)
|
||||
}
|
||||
}
|
||||
if len(mfields) == 0 {
|
||||
return errors.New("ssob: No exported fields for " + string(t.Name()))
|
||||
}
|
||||
|
||||
f := func(e interface{}, in []byte) (n int, err error) {
|
||||
t := reflect.TypeOf(e)
|
||||
v := reflect.ValueOf(e)
|
||||
|
@ -406,8 +418,7 @@ func decRegisterUnsafe(e interface{}) (err error) {
|
|||
}
|
||||
|
||||
vi := reflect.Indirect(v)
|
||||
l := vi.NumField()
|
||||
for i := 0; i < l; i++ {
|
||||
for _, i := range mfields {
|
||||
ni, err := unsafeUnmarshal(vi.Field(i).Addr().Interface(), in[pos:])
|
||||
pos += ni
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue