message_opaque_gen.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2018 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. // Code generated by generate-types. DO NOT EDIT.
  5. package impl
  6. import (
  7. "reflect"
  8. "google.golang.org/protobuf/reflect/protoreflect"
  9. )
  10. func getterForOpaqueNullableScalar(mi *MessageInfo, index uint32, fd protoreflect.FieldDescriptor, fs reflect.StructField, conv Converter, fieldOffset offset) func(p pointer) protoreflect.Value {
  11. ft := fs.Type
  12. if ft.Kind() == reflect.Ptr {
  13. ft = ft.Elem()
  14. }
  15. if fd.Kind() == protoreflect.EnumKind {
  16. // Enums for nullable opaque types.
  17. return func(p pointer) protoreflect.Value {
  18. if p.IsNil() || !mi.present(p, index) {
  19. return conv.Zero()
  20. }
  21. rv := p.Apply(fieldOffset).AsValueOf(fs.Type).Elem()
  22. return conv.PBValueOf(rv)
  23. }
  24. }
  25. switch ft.Kind() {
  26. case reflect.Bool:
  27. return func(p pointer) protoreflect.Value {
  28. if p.IsNil() || !mi.present(p, index) {
  29. return conv.Zero()
  30. }
  31. x := p.Apply(fieldOffset).Bool()
  32. return protoreflect.ValueOfBool(*x)
  33. }
  34. case reflect.Int32:
  35. return func(p pointer) protoreflect.Value {
  36. if p.IsNil() || !mi.present(p, index) {
  37. return conv.Zero()
  38. }
  39. x := p.Apply(fieldOffset).Int32()
  40. return protoreflect.ValueOfInt32(*x)
  41. }
  42. case reflect.Uint32:
  43. return func(p pointer) protoreflect.Value {
  44. if p.IsNil() || !mi.present(p, index) {
  45. return conv.Zero()
  46. }
  47. x := p.Apply(fieldOffset).Uint32()
  48. return protoreflect.ValueOfUint32(*x)
  49. }
  50. case reflect.Int64:
  51. return func(p pointer) protoreflect.Value {
  52. if p.IsNil() || !mi.present(p, index) {
  53. return conv.Zero()
  54. }
  55. x := p.Apply(fieldOffset).Int64()
  56. return protoreflect.ValueOfInt64(*x)
  57. }
  58. case reflect.Uint64:
  59. return func(p pointer) protoreflect.Value {
  60. if p.IsNil() || !mi.present(p, index) {
  61. return conv.Zero()
  62. }
  63. x := p.Apply(fieldOffset).Uint64()
  64. return protoreflect.ValueOfUint64(*x)
  65. }
  66. case reflect.Float32:
  67. return func(p pointer) protoreflect.Value {
  68. if p.IsNil() || !mi.present(p, index) {
  69. return conv.Zero()
  70. }
  71. x := p.Apply(fieldOffset).Float32()
  72. return protoreflect.ValueOfFloat32(*x)
  73. }
  74. case reflect.Float64:
  75. return func(p pointer) protoreflect.Value {
  76. if p.IsNil() || !mi.present(p, index) {
  77. return conv.Zero()
  78. }
  79. x := p.Apply(fieldOffset).Float64()
  80. return protoreflect.ValueOfFloat64(*x)
  81. }
  82. case reflect.String:
  83. if fd.Kind() == protoreflect.BytesKind {
  84. return func(p pointer) protoreflect.Value {
  85. if p.IsNil() || !mi.present(p, index) {
  86. return conv.Zero()
  87. }
  88. x := p.Apply(fieldOffset).StringPtr()
  89. if *x == nil {
  90. return conv.Zero()
  91. }
  92. if len(**x) == 0 {
  93. return protoreflect.ValueOfBytes(nil)
  94. }
  95. return protoreflect.ValueOfBytes([]byte(**x))
  96. }
  97. }
  98. return func(p pointer) protoreflect.Value {
  99. if p.IsNil() || !mi.present(p, index) {
  100. return conv.Zero()
  101. }
  102. x := p.Apply(fieldOffset).StringPtr()
  103. if *x == nil {
  104. return conv.Zero()
  105. }
  106. return protoreflect.ValueOfString(**x)
  107. }
  108. case reflect.Slice:
  109. if fd.Kind() == protoreflect.StringKind {
  110. return func(p pointer) protoreflect.Value {
  111. if p.IsNil() || !mi.present(p, index) {
  112. return conv.Zero()
  113. }
  114. x := p.Apply(fieldOffset).Bytes()
  115. return protoreflect.ValueOfString(string(*x))
  116. }
  117. }
  118. return func(p pointer) protoreflect.Value {
  119. if p.IsNil() || !mi.present(p, index) {
  120. return conv.Zero()
  121. }
  122. x := p.Apply(fieldOffset).Bytes()
  123. return protoreflect.ValueOfBytes(*x)
  124. }
  125. }
  126. panic("unexpected protobuf kind: " + ft.Kind().String())
  127. }