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