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
|
encoderCache[string(t.Kind())] = f
|
||||||
case reflect.Struct:
|
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) {
|
f := func(e interface{}) (ret []byte, err error) {
|
||||||
n := v.NumField()
|
|
||||||
bs := make([][]byte, n)
|
bs := make([][]byte, n)
|
||||||
blen := 0
|
blen := 0
|
||||||
for i := 0; i < n; i++ {
|
for _, i := range mfields {
|
||||||
bs[i], err = marshal(v.Field(i).Interface())
|
bs[i], err = marshal(v.Field(i).Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
15
unmarshal.go
15
unmarshal.go
|
@ -269,6 +269,18 @@ func decRegister(e interface{}) (err error) {
|
||||||
}
|
}
|
||||||
decoderCache[string(t.Kind())] = f
|
decoderCache[string(t.Kind())] = f
|
||||||
case reflect.Struct:
|
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) {
|
f := func(e interface{}, in []byte) (n int, err error) {
|
||||||
t := reflect.TypeOf(e)
|
t := reflect.TypeOf(e)
|
||||||
v := reflect.ValueOf(e)
|
v := reflect.ValueOf(e)
|
||||||
|
@ -279,8 +291,7 @@ func decRegister(e interface{}) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
vi := reflect.Indirect(v)
|
vi := reflect.Indirect(v)
|
||||||
l := vi.NumField()
|
for _, i := range mfields {
|
||||||
for i := 0; i < l; i++ {
|
|
||||||
ni, err := unmarshal(vi.Field(i).Addr().Interface(), in[pos:])
|
ni, err := unmarshal(vi.Field(i).Addr().Interface(), in[pos:])
|
||||||
pos += ni
|
pos += ni
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -354,11 +354,22 @@ func encRegisterUnsafe(e interface{}) (err error) {
|
||||||
}
|
}
|
||||||
unsafeEncoderCache[string(t.Kind())] = f
|
unsafeEncoderCache[string(t.Kind())] = f
|
||||||
case reflect.Struct:
|
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) {
|
f := func(e interface{}) (ret []byte, err error) {
|
||||||
n := v.NumField()
|
|
||||||
bs := make([][]byte, n)
|
bs := make([][]byte, n)
|
||||||
blen := 0
|
blen := 0
|
||||||
for i := 0; i < n; i++ {
|
for _, i := range mfields {
|
||||||
bs[i], err = unsafeMarshal(v.Field(i).Interface())
|
bs[i], err = unsafeMarshal(v.Field(i).Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
@ -396,6 +396,18 @@ func decRegisterUnsafe(e interface{}) (err error) {
|
||||||
}
|
}
|
||||||
unsafeDecoderCache[string(t.Kind())] = f
|
unsafeDecoderCache[string(t.Kind())] = f
|
||||||
case reflect.Struct:
|
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) {
|
f := func(e interface{}, in []byte) (n int, err error) {
|
||||||
t := reflect.TypeOf(e)
|
t := reflect.TypeOf(e)
|
||||||
v := reflect.ValueOf(e)
|
v := reflect.ValueOf(e)
|
||||||
|
@ -406,8 +418,7 @@ func decRegisterUnsafe(e interface{}) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
vi := reflect.Indirect(v)
|
vi := reflect.Indirect(v)
|
||||||
l := vi.NumField()
|
for _, i := range mfields {
|
||||||
for i := 0; i < l; i++ {
|
|
||||||
ni, err := unsafeUnmarshal(vi.Field(i).Addr().Interface(), in[pos:])
|
ni, err := unsafeUnmarshal(vi.Field(i).Addr().Interface(), in[pos:])
|
||||||
pos += ni
|
pos += ni
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue