editions.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. default:
  30. panic(fmt.Sprintf("unkown field number %d while unmarshalling GoFeatures", num))
  31. }
  32. }
  33. return parent
  34. }
  35. func unmarshalFeatureSet(b []byte, parent EditionFeatures) EditionFeatures {
  36. for len(b) > 0 {
  37. num, typ, n := protowire.ConsumeTag(b)
  38. b = b[n:]
  39. switch typ {
  40. case protowire.VarintType:
  41. v, m := protowire.ConsumeVarint(b)
  42. b = b[m:]
  43. switch num {
  44. case genid.FeatureSet_FieldPresence_field_number:
  45. parent.IsFieldPresence = v == genid.FeatureSet_EXPLICIT_enum_value || v == genid.FeatureSet_LEGACY_REQUIRED_enum_value
  46. parent.IsLegacyRequired = v == genid.FeatureSet_LEGACY_REQUIRED_enum_value
  47. case genid.FeatureSet_EnumType_field_number:
  48. parent.IsOpenEnum = v == genid.FeatureSet_OPEN_enum_value
  49. case genid.FeatureSet_RepeatedFieldEncoding_field_number:
  50. parent.IsPacked = v == genid.FeatureSet_PACKED_enum_value
  51. case genid.FeatureSet_Utf8Validation_field_number:
  52. parent.IsUTF8Validated = v == genid.FeatureSet_VERIFY_enum_value
  53. case genid.FeatureSet_MessageEncoding_field_number:
  54. parent.IsDelimitedEncoded = v == genid.FeatureSet_DELIMITED_enum_value
  55. case genid.FeatureSet_JsonFormat_field_number:
  56. parent.IsJSONCompliant = v == genid.FeatureSet_ALLOW_enum_value
  57. default:
  58. panic(fmt.Sprintf("unkown field number %d while unmarshalling FeatureSet", num))
  59. }
  60. case protowire.BytesType:
  61. v, m := protowire.ConsumeBytes(b)
  62. b = b[m:]
  63. switch num {
  64. case genid.GoFeatures_LegacyUnmarshalJsonEnum_field_number:
  65. parent = unmarshalGoFeature(v, parent)
  66. }
  67. }
  68. }
  69. return parent
  70. }
  71. func featuresFromParentDesc(parentDesc protoreflect.Descriptor) EditionFeatures {
  72. var parentFS EditionFeatures
  73. switch p := parentDesc.(type) {
  74. case *File:
  75. parentFS = p.L1.EditionFeatures
  76. case *Message:
  77. parentFS = p.L1.EditionFeatures
  78. default:
  79. panic(fmt.Sprintf("unknown parent type %T", parentDesc))
  80. }
  81. return parentFS
  82. }
  83. func unmarshalEditionDefault(b []byte) {
  84. var ed Edition
  85. var fs EditionFeatures
  86. for len(b) > 0 {
  87. num, typ, n := protowire.ConsumeTag(b)
  88. b = b[n:]
  89. switch typ {
  90. case protowire.VarintType:
  91. v, m := protowire.ConsumeVarint(b)
  92. b = b[m:]
  93. switch num {
  94. case genid.FeatureSetDefaults_FeatureSetEditionDefault_Edition_field_number:
  95. ed = Edition(v)
  96. }
  97. case protowire.BytesType:
  98. v, m := protowire.ConsumeBytes(b)
  99. b = b[m:]
  100. switch num {
  101. case genid.FeatureSetDefaults_FeatureSetEditionDefault_FixedFeatures_field_number:
  102. fs = unmarshalFeatureSet(v, fs)
  103. case genid.FeatureSetDefaults_FeatureSetEditionDefault_OverridableFeatures_field_number:
  104. fs = unmarshalFeatureSet(v, fs)
  105. }
  106. }
  107. }
  108. defaultsCache[ed] = fs
  109. defaultsKeys = append(defaultsKeys, ed)
  110. }
  111. func unmarshalEditionDefaults(b []byte) {
  112. for len(b) > 0 {
  113. num, _, n := protowire.ConsumeTag(b)
  114. b = b[n:]
  115. switch num {
  116. case genid.FeatureSetDefaults_Defaults_field_number:
  117. def, m := protowire.ConsumeBytes(b)
  118. b = b[m:]
  119. unmarshalEditionDefault(def)
  120. case genid.FeatureSetDefaults_MinimumEdition_field_number,
  121. genid.FeatureSetDefaults_MaximumEdition_field_number:
  122. // We don't care about the minimum and maximum editions. If the
  123. // edition we are looking for later on is not in the cache we know
  124. // it is outside of the range between minimum and maximum edition.
  125. _, m := protowire.ConsumeVarint(b)
  126. b = b[m:]
  127. default:
  128. panic(fmt.Sprintf("unkown field number %d while unmarshalling EditionDefault", num))
  129. }
  130. }
  131. }
  132. func getFeaturesFor(ed Edition) EditionFeatures {
  133. match := EditionUnknown
  134. for _, key := range defaultsKeys {
  135. if key > ed {
  136. break
  137. }
  138. match = key
  139. }
  140. if match == EditionUnknown {
  141. panic(fmt.Sprintf("unsupported edition: %v", ed))
  142. }
  143. return defaultsCache[match]
  144. }