editions.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.
  4. package filedesc
  5. import (
  6. "fmt"
  7. "google.golang.org/protobuf/encoding/protowire"
  8. "google.golang.org/protobuf/internal/editiondefaults"
  9. "google.golang.org/protobuf/internal/genid"
  10. "google.golang.org/protobuf/reflect/protoreflect"
  11. )
  12. var (
  13. defaultsCache = make(map[Edition]EditionFeatures)
  14. defaultsKeys = []Edition{}
  15. )
  16. func init() {
  17. unmarshalEditionDefaults(editiondefaults.Defaults)
  18. SurrogateProto2.L1.EditionFeatures = getFeaturesFor(EditionProto2)
  19. SurrogateProto3.L1.EditionFeatures = getFeaturesFor(EditionProto3)
  20. SurrogateEdition2023.L1.EditionFeatures = getFeaturesFor(Edition2023)
  21. }
  22. func unmarshalGoFeature(b []byte, parent EditionFeatures) EditionFeatures {
  23. for len(b) > 0 {
  24. num, _, n := protowire.ConsumeTag(b)
  25. b = b[n:]
  26. switch num {
  27. case genid.GoFeatures_LegacyUnmarshalJsonEnum_field_number:
  28. v, m := protowire.ConsumeVarint(b)
  29. b = b[m:]
  30. parent.GenerateLegacyUnmarshalJSON = protowire.DecodeBool(v)
  31. case genid.GoFeatures_ApiLevel_field_number:
  32. v, m := protowire.ConsumeVarint(b)
  33. b = b[m:]
  34. parent.APILevel = int(v)
  35. case genid.GoFeatures_StripEnumPrefix_field_number:
  36. v, m := protowire.ConsumeVarint(b)
  37. b = b[m:]
  38. parent.StripEnumPrefix = int(v)
  39. default:
  40. panic(fmt.Sprintf("unknown field number %d while unmarshalling GoFeatures", num))
  41. }
  42. }
  43. return parent
  44. }
  45. func unmarshalFeatureSet(b []byte, parent EditionFeatures) EditionFeatures {
  46. for len(b) > 0 {
  47. num, typ, n := protowire.ConsumeTag(b)
  48. b = b[n:]
  49. switch typ {
  50. case protowire.VarintType:
  51. v, m := protowire.ConsumeVarint(b)
  52. b = b[m:]
  53. switch num {
  54. case genid.FeatureSet_FieldPresence_field_number:
  55. parent.IsFieldPresence = v == genid.FeatureSet_EXPLICIT_enum_value || v == genid.FeatureSet_LEGACY_REQUIRED_enum_value
  56. parent.IsLegacyRequired = v == genid.FeatureSet_LEGACY_REQUIRED_enum_value
  57. case genid.FeatureSet_EnumType_field_number:
  58. parent.IsOpenEnum = v == genid.FeatureSet_OPEN_enum_value
  59. case genid.FeatureSet_RepeatedFieldEncoding_field_number:
  60. parent.IsPacked = v == genid.FeatureSet_PACKED_enum_value
  61. case genid.FeatureSet_Utf8Validation_field_number:
  62. parent.IsUTF8Validated = v == genid.FeatureSet_VERIFY_enum_value
  63. case genid.FeatureSet_MessageEncoding_field_number:
  64. parent.IsDelimitedEncoded = v == genid.FeatureSet_DELIMITED_enum_value
  65. case genid.FeatureSet_JsonFormat_field_number:
  66. parent.IsJSONCompliant = v == genid.FeatureSet_ALLOW_enum_value
  67. case genid.FeatureSet_EnforceNamingStyle_field_number:
  68. // EnforceNamingStyle is enforced in protoc, languages other than C++
  69. // are not supposed to do anything with this feature.
  70. case genid.FeatureSet_DefaultSymbolVisibility_field_number:
  71. // DefaultSymbolVisibility is enforced in protoc, runtimes should not
  72. // inspect this value.
  73. default:
  74. panic(fmt.Sprintf("unknown field number %d while unmarshalling FeatureSet", num))
  75. }
  76. case protowire.BytesType:
  77. v, m := protowire.ConsumeBytes(b)
  78. b = b[m:]
  79. switch num {
  80. case genid.FeatureSet_Go_ext_number:
  81. parent = unmarshalGoFeature(v, parent)
  82. }
  83. }
  84. }
  85. return parent
  86. }
  87. func featuresFromParentDesc(parentDesc protoreflect.Descriptor) EditionFeatures {
  88. var parentFS EditionFeatures
  89. switch p := parentDesc.(type) {
  90. case *File:
  91. parentFS = p.L1.EditionFeatures
  92. case *Message:
  93. parentFS = p.L1.EditionFeatures
  94. default:
  95. panic(fmt.Sprintf("unknown parent type %T", parentDesc))
  96. }
  97. return parentFS
  98. }
  99. func unmarshalEditionDefault(b []byte) {
  100. var ed Edition
  101. var fs EditionFeatures
  102. for len(b) > 0 {
  103. num, typ, n := protowire.ConsumeTag(b)
  104. b = b[n:]
  105. switch typ {
  106. case protowire.VarintType:
  107. v, m := protowire.ConsumeVarint(b)
  108. b = b[m:]
  109. switch num {
  110. case genid.FeatureSetDefaults_FeatureSetEditionDefault_Edition_field_number:
  111. ed = Edition(v)
  112. }
  113. case protowire.BytesType:
  114. v, m := protowire.ConsumeBytes(b)
  115. b = b[m:]
  116. switch num {
  117. case genid.FeatureSetDefaults_FeatureSetEditionDefault_FixedFeatures_field_number:
  118. fs = unmarshalFeatureSet(v, fs)
  119. case genid.FeatureSetDefaults_FeatureSetEditionDefault_OverridableFeatures_field_number:
  120. fs = unmarshalFeatureSet(v, fs)
  121. }
  122. }
  123. }
  124. defaultsCache[ed] = fs
  125. defaultsKeys = append(defaultsKeys, ed)
  126. }
  127. func unmarshalEditionDefaults(b []byte) {
  128. for len(b) > 0 {
  129. num, _, n := protowire.ConsumeTag(b)
  130. b = b[n:]
  131. switch num {
  132. case genid.FeatureSetDefaults_Defaults_field_number:
  133. def, m := protowire.ConsumeBytes(b)
  134. b = b[m:]
  135. unmarshalEditionDefault(def)
  136. case genid.FeatureSetDefaults_MinimumEdition_field_number,
  137. genid.FeatureSetDefaults_MaximumEdition_field_number:
  138. // We don't care about the minimum and maximum editions. If the
  139. // edition we are looking for later on is not in the cache we know
  140. // it is outside of the range between minimum and maximum edition.
  141. _, m := protowire.ConsumeVarint(b)
  142. b = b[m:]
  143. default:
  144. panic(fmt.Sprintf("unknown field number %d while unmarshalling EditionDefault", num))
  145. }
  146. }
  147. }
  148. func getFeaturesFor(ed Edition) EditionFeatures {
  149. match := EditionUnknown
  150. for _, key := range defaultsKeys {
  151. if key > ed {
  152. break
  153. }
  154. match = key
  155. }
  156. if match == EditionUnknown {
  157. panic(fmt.Sprintf("unsupported edition: %v", ed))
  158. }
  159. return defaultsCache[match]
  160. }