1
// Copyright 2024 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.
10
"google.golang.org/protobuf/encoding/protowire"
11
"google.golang.org/protobuf/internal/editiondefaults"
12
"google.golang.org/protobuf/internal/genid"
13
"google.golang.org/protobuf/reflect/protoreflect"
16
var defaultsCache = make(map[Edition]EditionFeatures)
19
unmarshalEditionDefaults(editiondefaults.Defaults)
22
func unmarshalGoFeature(b []byte, parent EditionFeatures) EditionFeatures {
24
num, _, n := protowire.ConsumeTag(b)
27
case genid.GoFeatures_LegacyUnmarshalJsonEnum_field_number:
28
v, m := protowire.ConsumeVarint(b)
30
parent.GenerateLegacyUnmarshalJSON = protowire.DecodeBool(v)
32
panic(fmt.Sprintf("unkown field number %d while unmarshalling GoFeatures", num))
38
func unmarshalFeatureSet(b []byte, parent EditionFeatures) EditionFeatures {
40
num, typ, n := protowire.ConsumeTag(b)
43
case protowire.VarintType:
44
v, m := protowire.ConsumeVarint(b)
47
case genid.FeatureSet_FieldPresence_field_number:
48
parent.IsFieldPresence = v == genid.FeatureSet_EXPLICIT_enum_value || v == genid.FeatureSet_LEGACY_REQUIRED_enum_value
49
parent.IsLegacyRequired = v == genid.FeatureSet_LEGACY_REQUIRED_enum_value
50
case genid.FeatureSet_EnumType_field_number:
51
parent.IsOpenEnum = v == genid.FeatureSet_OPEN_enum_value
52
case genid.FeatureSet_RepeatedFieldEncoding_field_number:
53
parent.IsPacked = v == genid.FeatureSet_PACKED_enum_value
54
case genid.FeatureSet_Utf8Validation_field_number:
55
parent.IsUTF8Validated = v == genid.FeatureSet_VERIFY_enum_value
56
case genid.FeatureSet_MessageEncoding_field_number:
57
parent.IsDelimitedEncoded = v == genid.FeatureSet_DELIMITED_enum_value
58
case genid.FeatureSet_JsonFormat_field_number:
59
parent.IsJSONCompliant = v == genid.FeatureSet_ALLOW_enum_value
61
panic(fmt.Sprintf("unkown field number %d while unmarshalling FeatureSet", num))
63
case protowire.BytesType:
64
v, m := protowire.ConsumeBytes(b)
67
case genid.GoFeatures_LegacyUnmarshalJsonEnum_field_number:
68
parent = unmarshalGoFeature(v, parent)
76
func featuresFromParentDesc(parentDesc protoreflect.Descriptor) EditionFeatures {
77
var parentFS EditionFeatures
78
switch p := parentDesc.(type) {
80
parentFS = p.L1.EditionFeatures
82
parentFS = p.L1.EditionFeatures
84
panic(fmt.Sprintf("unknown parent type %T", parentDesc))
89
func unmarshalEditionDefault(b []byte) {
91
var fs EditionFeatures
93
num, typ, n := protowire.ConsumeTag(b)
96
case protowire.VarintType:
97
v, m := protowire.ConsumeVarint(b)
100
case genid.FeatureSetDefaults_FeatureSetEditionDefault_Edition_field_number:
103
case protowire.BytesType:
104
v, m := protowire.ConsumeBytes(b)
107
case genid.FeatureSetDefaults_FeatureSetEditionDefault_Features_field_number:
108
fs = unmarshalFeatureSet(v, fs)
112
defaultsCache[ed] = fs
115
func unmarshalEditionDefaults(b []byte) {
117
num, _, n := protowire.ConsumeTag(b)
120
case genid.FeatureSetDefaults_Defaults_field_number:
121
def, m := protowire.ConsumeBytes(b)
123
unmarshalEditionDefault(def)
124
case genid.FeatureSetDefaults_MinimumEdition_field_number,
125
genid.FeatureSetDefaults_MaximumEdition_field_number:
126
// We don't care about the minimum and maximum editions. If the
127
// edition we are looking for later on is not in the cache we know
128
// it is outside of the range between minimum and maximum edition.
129
_, m := protowire.ConsumeVarint(b)
132
panic(fmt.Sprintf("unkown field number %d while unmarshalling EditionDefault", num))
137
func getFeaturesFor(ed Edition) EditionFeatures {
138
if def, ok := defaultsCache[ed]; ok {
141
panic(fmt.Sprintf("unsupported edition: %v", ed))