irregular.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2019 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 irregular
  5. import (
  6. "google.golang.org/protobuf/encoding/prototext"
  7. "google.golang.org/protobuf/reflect/protodesc"
  8. pref "google.golang.org/protobuf/reflect/protoreflect"
  9. "google.golang.org/protobuf/types/descriptorpb"
  10. )
  11. type IrregularMessage struct {
  12. set bool
  13. value string
  14. }
  15. func (m *IrregularMessage) ProtoReflect() pref.Message { return (*message)(m) }
  16. type message IrregularMessage
  17. func (m *message) Descriptor() pref.MessageDescriptor { return fileDesc.Messages().Get(0) }
  18. func (m *message) Type() pref.MessageType { return m }
  19. func (m *message) New() pref.Message { return &message{} }
  20. func (m *message) Zero() pref.Message { return (*message)(nil) }
  21. func (m *message) Interface() pref.ProtoMessage { return (*IrregularMessage)(m) }
  22. var fieldDescS = fileDesc.Messages().Get(0).Fields().Get(0)
  23. func (m *message) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
  24. if m.set {
  25. f(fieldDescS, pref.ValueOf(m.value))
  26. }
  27. }
  28. func (m *message) Has(fd pref.FieldDescriptor) bool {
  29. if fd == fieldDescS {
  30. return m.set
  31. }
  32. panic("invalid field descriptor")
  33. }
  34. func (m *message) Clear(fd pref.FieldDescriptor) {
  35. if fd == fieldDescS {
  36. m.value = ""
  37. m.set = false
  38. return
  39. }
  40. panic("invalid field descriptor")
  41. }
  42. func (m *message) Get(fd pref.FieldDescriptor) pref.Value {
  43. if fd == fieldDescS {
  44. return pref.ValueOf(m.value)
  45. }
  46. panic("invalid field descriptor")
  47. }
  48. func (m *message) Set(fd pref.FieldDescriptor, v pref.Value) {
  49. if fd == fieldDescS {
  50. m.value = v.String()
  51. m.set = true
  52. return
  53. }
  54. panic("invalid field descriptor")
  55. }
  56. func (m *message) Mutable(pref.FieldDescriptor) pref.Value {
  57. panic("invalid field descriptor")
  58. }
  59. func (m *message) NewField(pref.FieldDescriptor) pref.Value {
  60. panic("invalid field descriptor")
  61. }
  62. func (m *message) WhichOneof(pref.OneofDescriptor) pref.FieldDescriptor {
  63. panic("invalid oneof descriptor")
  64. }
  65. func (m *message) GetUnknown() pref.RawFields { return nil }
  66. func (m *message) SetUnknown(pref.RawFields) { return }
  67. var fileDesc = func() pref.FileDescriptor {
  68. p := &descriptorpb.FileDescriptorProto{}
  69. if err := prototext.Unmarshal([]byte(descriptorText), p); err != nil {
  70. panic(err)
  71. }
  72. file, err := protodesc.NewFile(p, nil)
  73. if err != nil {
  74. panic(err)
  75. }
  76. return file
  77. }()
  78. func file_irregular_irregular_proto_init() { _ = fileDesc }
  79. const descriptorText = `
  80. name: "internal/testprotos/irregular/irregular.proto"
  81. package: "goproto.proto.thirdparty"
  82. message_type {
  83. name: "IrregularMessage"
  84. field {
  85. name: "s"
  86. number: 1
  87. label: LABEL_OPTIONAL
  88. type: TYPE_STRING
  89. json_name: "s"
  90. }
  91. }
  92. options {
  93. go_package: "google.golang.org/protobuf/internal/testprotos/irregular"
  94. }
  95. `
  96. type AberrantMessage int
  97. func (m AberrantMessage) ProtoMessage() {}
  98. func (m AberrantMessage) Reset() {}
  99. func (m AberrantMessage) String() string { return "" }
  100. func (m AberrantMessage) Marshal() ([]byte, error) { return nil, nil }
  101. func (m AberrantMessage) Unmarshal([]byte) error { return nil }