encode_gen.go raw

   1  // Copyright 2018 The Go Authors. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  // Code generated by generate-types. DO NOT EDIT.
   6  
   7  package proto
   8  
   9  import (
  10  	"math"
  11  	"unicode/utf8"
  12  
  13  	"google.golang.org/protobuf/encoding/protowire"
  14  	"google.golang.org/protobuf/internal/errors"
  15  	"google.golang.org/protobuf/internal/strs"
  16  	"google.golang.org/protobuf/reflect/protoreflect"
  17  )
  18  
  19  var wireTypes = map[protoreflect.Kind]protowire.Type{
  20  	protoreflect.BoolKind:     protowire.VarintType,
  21  	protoreflect.EnumKind:     protowire.VarintType,
  22  	protoreflect.Int32Kind:    protowire.VarintType,
  23  	protoreflect.Sint32Kind:   protowire.VarintType,
  24  	protoreflect.Uint32Kind:   protowire.VarintType,
  25  	protoreflect.Int64Kind:    protowire.VarintType,
  26  	protoreflect.Sint64Kind:   protowire.VarintType,
  27  	protoreflect.Uint64Kind:   protowire.VarintType,
  28  	protoreflect.Sfixed32Kind: protowire.Fixed32Type,
  29  	protoreflect.Fixed32Kind:  protowire.Fixed32Type,
  30  	protoreflect.FloatKind:    protowire.Fixed32Type,
  31  	protoreflect.Sfixed64Kind: protowire.Fixed64Type,
  32  	protoreflect.Fixed64Kind:  protowire.Fixed64Type,
  33  	protoreflect.DoubleKind:   protowire.Fixed64Type,
  34  	protoreflect.StringKind:   protowire.BytesType,
  35  	protoreflect.BytesKind:    protowire.BytesType,
  36  	protoreflect.MessageKind:  protowire.BytesType,
  37  	protoreflect.GroupKind:    protowire.StartGroupType,
  38  }
  39  
  40  func (o MarshalOptions) marshalSingular(b []byte, fd protoreflect.FieldDescriptor, v protoreflect.Value) ([]byte, error) {
  41  	switch fd.Kind() {
  42  	case protoreflect.BoolKind:
  43  		b = protowire.AppendVarint(b, protowire.EncodeBool(v.Bool()))
  44  	case protoreflect.EnumKind:
  45  		b = protowire.AppendVarint(b, uint64(v.Enum()))
  46  	case protoreflect.Int32Kind:
  47  		b = protowire.AppendVarint(b, uint64(int32(v.Int())))
  48  	case protoreflect.Sint32Kind:
  49  		b = protowire.AppendVarint(b, protowire.EncodeZigZag(int64(int32(v.Int()))))
  50  	case protoreflect.Uint32Kind:
  51  		b = protowire.AppendVarint(b, uint64(uint32(v.Uint())))
  52  	case protoreflect.Int64Kind:
  53  		b = protowire.AppendVarint(b, uint64(v.Int()))
  54  	case protoreflect.Sint64Kind:
  55  		b = protowire.AppendVarint(b, protowire.EncodeZigZag(v.Int()))
  56  	case protoreflect.Uint64Kind:
  57  		b = protowire.AppendVarint(b, v.Uint())
  58  	case protoreflect.Sfixed32Kind:
  59  		b = protowire.AppendFixed32(b, uint32(v.Int()))
  60  	case protoreflect.Fixed32Kind:
  61  		b = protowire.AppendFixed32(b, uint32(v.Uint()))
  62  	case protoreflect.FloatKind:
  63  		b = protowire.AppendFixed32(b, math.Float32bits(float32(v.Float())))
  64  	case protoreflect.Sfixed64Kind:
  65  		b = protowire.AppendFixed64(b, uint64(v.Int()))
  66  	case protoreflect.Fixed64Kind:
  67  		b = protowire.AppendFixed64(b, v.Uint())
  68  	case protoreflect.DoubleKind:
  69  		b = protowire.AppendFixed64(b, math.Float64bits(v.Float()))
  70  	case protoreflect.StringKind:
  71  		if strs.EnforceUTF8(fd) && !utf8.ValidString(v.String()) {
  72  			return b, errors.InvalidUTF8(string(fd.FullName()))
  73  		}
  74  		b = protowire.AppendString(b, v.String())
  75  	case protoreflect.BytesKind:
  76  		b = protowire.AppendBytes(b, v.Bytes())
  77  	case protoreflect.MessageKind:
  78  		var pos int
  79  		var err error
  80  		b, pos = appendSpeculativeLength(b)
  81  		b, err = o.marshalMessage(b, v.Message())
  82  		if err != nil {
  83  			return b, err
  84  		}
  85  		b = finishSpeculativeLength(b, pos)
  86  	case protoreflect.GroupKind:
  87  		var err error
  88  		b, err = o.marshalMessage(b, v.Message())
  89  		if err != nil {
  90  			return b, err
  91  		}
  92  		b = protowire.AppendVarint(b, protowire.EncodeTag(fd.Number(), protowire.EndGroupType))
  93  	default:
  94  		return b, errors.New("invalid kind %v", fd.Kind())
  95  	}
  96  	return b, nil
  97  }
  98