internal/impl: change unmarshal func return to unmarshalOptions
The fast-path unmarshal funcs return the number of bytes consumed.
Change these functions to return an unmarshalOutput struct instead, to
make it easier to add to the results. This is groundwork for allowing
the fast-path unmarshaler to indicate when the unmarshaled message is
known to be initialized.
Change-Id: Ia8c44731a88f5be969a55cd98ea26282f412c7ae
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/215720
Reviewed-by: Joe Tsai <joetsai@google.com>
diff --git a/internal/impl/codec_reflect.go b/internal/impl/codec_reflect.go
index 5884ee4..75420d9 100644
--- a/internal/impl/codec_reflect.go
+++ b/internal/impl/codec_reflect.go
@@ -24,16 +24,17 @@
return b, nil
}
-func consumeEnum(b []byte, p pointer, wtyp wire.Type, _ unmarshalOptions) (n int, err error) {
+func consumeEnum(b []byte, p pointer, wtyp wire.Type, _ unmarshalOptions) (out unmarshalOutput, err error) {
if wtyp != wire.VarintType {
- return 0, errUnknown
+ return out, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
- return 0, wire.ParseError(n)
+ return out, wire.ParseError(n)
}
p.v.Elem().SetInt(int64(v))
- return n, nil
+ out.n = n
+ return out, nil
}
var coderEnum = pointerCoderFuncs{
@@ -70,9 +71,9 @@
return appendEnum(b, pointer{p.v.Elem()}, wiretag, opts)
}
-func consumeEnumPtr(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (n int, err error) {
+func consumeEnumPtr(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (out unmarshalOutput, err error) {
if wtyp != wire.VarintType {
- return 0, errUnknown
+ return out, errUnknown
}
if p.v.Elem().IsNil() {
p.v.Elem().Set(reflect.New(p.v.Elem().Type().Elem()))
@@ -103,36 +104,38 @@
return b, nil
}
-func consumeEnumSlice(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (n int, err error) {
+func consumeEnumSlice(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (out unmarshalOutput, err error) {
s := p.v.Elem()
if wtyp == wire.BytesType {
- b, n = wire.ConsumeBytes(b)
+ b, n := wire.ConsumeBytes(b)
if n < 0 {
- return 0, wire.ParseError(n)
+ return out, wire.ParseError(n)
}
for len(b) > 0 {
v, n := wire.ConsumeVarint(b)
if n < 0 {
- return 0, wire.ParseError(n)
+ return out, wire.ParseError(n)
}
rv := reflect.New(s.Type().Elem()).Elem()
rv.SetInt(int64(v))
s.Set(reflect.Append(s, rv))
b = b[n:]
}
- return n, nil
+ out.n = n
+ return out, nil
}
if wtyp != wire.VarintType {
- return 0, errUnknown
+ return out, errUnknown
}
v, n := wire.ConsumeVarint(b)
if n < 0 {
- return 0, wire.ParseError(n)
+ return out, wire.ParseError(n)
}
rv := reflect.New(s.Type().Elem()).Elem()
rv.SetInt(int64(v))
s.Set(reflect.Append(s, rv))
- return n, nil
+ out.n = n
+ return out, nil
}
var coderEnumSlice = pointerCoderFuncs{