2019-04-01 13:49:56 -07:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// +build purego appengine
|
|
|
|
|
|
|
|
package impl
|
|
|
|
|
|
|
|
import (
|
2019-06-27 10:54:42 -07:00
|
|
|
"reflect"
|
|
|
|
|
2019-04-01 13:49:56 -07:00
|
|
|
"google.golang.org/protobuf/internal/encoding/wire"
|
|
|
|
)
|
|
|
|
|
|
|
|
func sizeEnum(p pointer, tagsize int, _ marshalOptions) (size int) {
|
|
|
|
v := p.v.Elem().Int()
|
|
|
|
return tagsize + wire.SizeVarint(uint64(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendEnum(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
|
|
|
|
v := p.v.Elem().Int()
|
|
|
|
b = wire.AppendVarint(b, wiretag)
|
|
|
|
b = wire.AppendVarint(b, uint64(v))
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2020-01-21 14:25:12 -08:00
|
|
|
func consumeEnum(b []byte, p pointer, wtyp wire.Type, _ unmarshalOptions) (out unmarshalOutput, err error) {
|
2019-06-27 10:54:42 -07:00
|
|
|
if wtyp != wire.VarintType {
|
2020-01-21 14:25:12 -08:00
|
|
|
return out, errUnknown
|
2019-06-27 10:54:42 -07:00
|
|
|
}
|
|
|
|
v, n := wire.ConsumeVarint(b)
|
|
|
|
if n < 0 {
|
2020-01-21 14:25:12 -08:00
|
|
|
return out, wire.ParseError(n)
|
2019-06-27 10:54:42 -07:00
|
|
|
}
|
|
|
|
p.v.Elem().SetInt(int64(v))
|
2020-01-21 14:25:12 -08:00
|
|
|
out.n = n
|
|
|
|
return out, nil
|
2019-06-27 10:54:42 -07:00
|
|
|
}
|
|
|
|
|
2019-04-09 15:57:05 -07:00
|
|
|
var coderEnum = pointerCoderFuncs{
|
2019-06-27 10:54:42 -07:00
|
|
|
size: sizeEnum,
|
|
|
|
marshal: appendEnum,
|
|
|
|
unmarshal: consumeEnum,
|
2019-04-09 15:57:05 -07:00
|
|
|
}
|
2019-04-01 13:49:56 -07:00
|
|
|
|
|
|
|
func sizeEnumNoZero(p pointer, tagsize int, opts marshalOptions) (size int) {
|
|
|
|
if p.v.Elem().Int() == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return sizeEnum(p, tagsize, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendEnumNoZero(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
|
|
|
|
if p.v.Elem().Int() == 0 {
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
return appendEnum(b, p, wiretag, opts)
|
|
|
|
}
|
|
|
|
|
2019-04-09 15:57:05 -07:00
|
|
|
var coderEnumNoZero = pointerCoderFuncs{
|
2019-06-27 10:54:42 -07:00
|
|
|
size: sizeEnumNoZero,
|
|
|
|
marshal: appendEnumNoZero,
|
|
|
|
unmarshal: consumeEnum,
|
2019-04-09 15:57:05 -07:00
|
|
|
}
|
2019-04-01 13:49:56 -07:00
|
|
|
|
|
|
|
func sizeEnumPtr(p pointer, tagsize int, opts marshalOptions) (size int) {
|
|
|
|
return sizeEnum(pointer{p.v.Elem()}, tagsize, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendEnumPtr(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
|
|
|
|
return appendEnum(b, pointer{p.v.Elem()}, wiretag, opts)
|
|
|
|
}
|
|
|
|
|
2020-01-21 14:25:12 -08:00
|
|
|
func consumeEnumPtr(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (out unmarshalOutput, err error) {
|
2019-06-27 10:54:42 -07:00
|
|
|
if wtyp != wire.VarintType {
|
2020-01-21 14:25:12 -08:00
|
|
|
return out, errUnknown
|
2019-06-27 10:54:42 -07:00
|
|
|
}
|
|
|
|
if p.v.Elem().IsNil() {
|
|
|
|
p.v.Elem().Set(reflect.New(p.v.Elem().Type().Elem()))
|
|
|
|
}
|
|
|
|
return consumeEnum(b, pointer{p.v.Elem()}, wtyp, opts)
|
|
|
|
}
|
|
|
|
|
2019-04-09 15:57:05 -07:00
|
|
|
var coderEnumPtr = pointerCoderFuncs{
|
2019-06-27 10:54:42 -07:00
|
|
|
size: sizeEnumPtr,
|
|
|
|
marshal: appendEnumPtr,
|
|
|
|
unmarshal: consumeEnumPtr,
|
2019-04-09 15:57:05 -07:00
|
|
|
}
|
2019-04-01 13:49:56 -07:00
|
|
|
|
|
|
|
func sizeEnumSlice(p pointer, tagsize int, opts marshalOptions) (size int) {
|
2019-08-22 11:41:32 -07:00
|
|
|
s := p.v.Elem()
|
|
|
|
for i, llen := 0, s.Len(); i < llen; i++ {
|
|
|
|
size += wire.SizeVarint(uint64(s.Index(i).Int())) + tagsize
|
|
|
|
}
|
|
|
|
return size
|
2019-04-01 13:49:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func appendEnumSlice(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
|
2019-08-22 11:41:32 -07:00
|
|
|
s := p.v.Elem()
|
|
|
|
for i, llen := 0, s.Len(); i < llen; i++ {
|
|
|
|
b = wire.AppendVarint(b, wiretag)
|
|
|
|
b = wire.AppendVarint(b, uint64(s.Index(i).Int()))
|
|
|
|
}
|
|
|
|
return b, nil
|
2019-04-01 13:49:56 -07:00
|
|
|
}
|
|
|
|
|
2020-01-21 14:25:12 -08:00
|
|
|
func consumeEnumSlice(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (out unmarshalOutput, err error) {
|
2019-08-22 11:41:32 -07:00
|
|
|
s := p.v.Elem()
|
|
|
|
if wtyp == wire.BytesType {
|
2020-01-21 14:25:12 -08:00
|
|
|
b, n := wire.ConsumeBytes(b)
|
2019-08-22 11:41:32 -07:00
|
|
|
if n < 0 {
|
2020-01-21 14:25:12 -08:00
|
|
|
return out, wire.ParseError(n)
|
2019-08-22 11:41:32 -07:00
|
|
|
}
|
|
|
|
for len(b) > 0 {
|
|
|
|
v, n := wire.ConsumeVarint(b)
|
|
|
|
if n < 0 {
|
2020-01-21 14:25:12 -08:00
|
|
|
return out, wire.ParseError(n)
|
2019-08-22 11:41:32 -07:00
|
|
|
}
|
|
|
|
rv := reflect.New(s.Type().Elem()).Elem()
|
|
|
|
rv.SetInt(int64(v))
|
|
|
|
s.Set(reflect.Append(s, rv))
|
|
|
|
b = b[n:]
|
|
|
|
}
|
2020-01-21 14:25:12 -08:00
|
|
|
out.n = n
|
|
|
|
return out, nil
|
2019-08-22 11:41:32 -07:00
|
|
|
}
|
|
|
|
if wtyp != wire.VarintType {
|
2020-01-21 14:25:12 -08:00
|
|
|
return out, errUnknown
|
2019-08-22 11:41:32 -07:00
|
|
|
}
|
|
|
|
v, n := wire.ConsumeVarint(b)
|
|
|
|
if n < 0 {
|
2020-01-21 14:25:12 -08:00
|
|
|
return out, wire.ParseError(n)
|
2019-08-22 11:41:32 -07:00
|
|
|
}
|
|
|
|
rv := reflect.New(s.Type().Elem()).Elem()
|
|
|
|
rv.SetInt(int64(v))
|
|
|
|
s.Set(reflect.Append(s, rv))
|
2020-01-21 14:25:12 -08:00
|
|
|
out.n = n
|
|
|
|
return out, nil
|
2019-06-27 10:54:42 -07:00
|
|
|
}
|
|
|
|
|
2019-04-09 15:57:05 -07:00
|
|
|
var coderEnumSlice = pointerCoderFuncs{
|
2019-06-27 10:54:42 -07:00
|
|
|
size: sizeEnumSlice,
|
|
|
|
marshal: appendEnumSlice,
|
|
|
|
unmarshal: consumeEnumSlice,
|
2019-04-09 15:57:05 -07:00
|
|
|
}
|
2019-04-01 13:49:56 -07:00
|
|
|
|
2019-07-10 15:23:29 -07:00
|
|
|
func sizeEnumPackedSlice(p pointer, tagsize int, opts marshalOptions) (size int) {
|
2019-08-22 11:41:32 -07:00
|
|
|
s := p.v.Elem()
|
|
|
|
llen := s.Len()
|
|
|
|
if llen == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
n := 0
|
|
|
|
for i := 0; i < llen; i++ {
|
|
|
|
n += wire.SizeVarint(uint64(s.Index(i).Int()))
|
|
|
|
}
|
|
|
|
return tagsize + wire.SizeBytes(n)
|
2019-04-01 13:49:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func appendEnumPackedSlice(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
|
2019-08-22 11:41:32 -07:00
|
|
|
s := p.v.Elem()
|
|
|
|
llen := s.Len()
|
|
|
|
if llen == 0 {
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
b = wire.AppendVarint(b, wiretag)
|
|
|
|
n := 0
|
|
|
|
for i := 0; i < llen; i++ {
|
|
|
|
n += wire.SizeVarint(uint64(s.Index(i).Int()))
|
|
|
|
}
|
|
|
|
b = wire.AppendVarint(b, uint64(n))
|
|
|
|
for i := 0; i < llen; i++ {
|
|
|
|
b = wire.AppendVarint(b, uint64(s.Index(i).Int()))
|
|
|
|
}
|
|
|
|
return b, nil
|
2019-04-01 13:49:56 -07:00
|
|
|
}
|
|
|
|
|
2019-04-09 15:57:05 -07:00
|
|
|
var coderEnumPackedSlice = pointerCoderFuncs{
|
2019-06-27 10:54:42 -07:00
|
|
|
size: sizeEnumPackedSlice,
|
|
|
|
marshal: appendEnumPackedSlice,
|
|
|
|
unmarshal: consumeEnumSlice,
|
2019-04-09 15:57:05 -07:00
|
|
|
}
|