editions.go 5.3 KB

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