blob: 2a7318b571106dc833ad71ff538370bd000aaf4a [file] [log] [blame]
Damien Neil5b6d0472019-06-14 11:54:07 -07001// 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
5package irregular
6
7import (
8 "google.golang.org/protobuf/encoding/prototext"
9 "google.golang.org/protobuf/reflect/protodesc"
10 pref "google.golang.org/protobuf/reflect/protoreflect"
11 "google.golang.org/protobuf/types/descriptorpb"
12)
13
14type IrregularMessage struct {
15 set bool
16 value string
17}
18
19func (m *IrregularMessage) ProtoReflect() pref.Message { return (*message)(m) }
20
21type message IrregularMessage
22
23func (m *message) Descriptor() pref.MessageDescriptor { return descriptor.Messages().Get(0) }
Damien Neil5b6d0472019-06-14 11:54:07 -070024func (m *message) New() pref.Message { return &message{} }
25func (m *message) Interface() pref.ProtoMessage { return (*IrregularMessage)(m) }
26
Joe Tsai378c1322019-04-25 23:48:08 -070027var fieldDescS = descriptor.Messages().Get(0).Fields().Get(0)
Damien Neil5b6d0472019-06-14 11:54:07 -070028
Joe Tsai378c1322019-04-25 23:48:08 -070029func (m *message) Len() int {
Damien Neil5b6d0472019-06-14 11:54:07 -070030 if m.set {
31 return 1
32 }
33 return 0
34}
35
Joe Tsai378c1322019-04-25 23:48:08 -070036func (m *message) Range(f func(pref.FieldDescriptor, pref.Value) bool) {
37 if m.set {
38 f(fieldDescS, pref.ValueOf(m.value))
39 }
40}
41
42func (m *message) Has(fd pref.FieldDescriptor) bool {
43 if fd == fieldDescS {
Damien Neil5b6d0472019-06-14 11:54:07 -070044 return m.set
45 }
Joe Tsai378c1322019-04-25 23:48:08 -070046 panic("invalid field descriptor")
Damien Neil5b6d0472019-06-14 11:54:07 -070047}
48
Joe Tsai378c1322019-04-25 23:48:08 -070049func (m *message) Clear(fd pref.FieldDescriptor) {
50 if fd == fieldDescS {
Damien Neil5b6d0472019-06-14 11:54:07 -070051 m.value = ""
52 m.set = false
Joe Tsai378c1322019-04-25 23:48:08 -070053 return
Damien Neil5b6d0472019-06-14 11:54:07 -070054 }
Joe Tsai378c1322019-04-25 23:48:08 -070055 panic("invalid field descriptor")
Damien Neil5b6d0472019-06-14 11:54:07 -070056}
57
Joe Tsai378c1322019-04-25 23:48:08 -070058func (m *message) Get(fd pref.FieldDescriptor) pref.Value {
59 if fd == fieldDescS {
60 return pref.ValueOf(m.value)
Damien Neil5b6d0472019-06-14 11:54:07 -070061 }
Joe Tsai378c1322019-04-25 23:48:08 -070062 panic("invalid field descriptor")
Damien Neil5b6d0472019-06-14 11:54:07 -070063}
64
Joe Tsai378c1322019-04-25 23:48:08 -070065func (m *message) Set(fd pref.FieldDescriptor, v pref.Value) {
66 if fd == fieldDescS {
67 m.value = v.String()
68 m.set = true
69 return
70 }
71 panic("invalid field descriptor")
Damien Neil5b6d0472019-06-14 11:54:07 -070072}
73
Joe Tsai378c1322019-04-25 23:48:08 -070074func (m *message) Mutable(pref.FieldDescriptor) pref.Value {
75 panic("invalid field descriptor")
Damien Neil5b6d0472019-06-14 11:54:07 -070076}
77
Joe Tsai378c1322019-04-25 23:48:08 -070078func (m *message) NewMessage(pref.FieldDescriptor) pref.Message {
79 panic("invalid field descriptor")
80}
Damien Neil5b6d0472019-06-14 11:54:07 -070081
Joe Tsai378c1322019-04-25 23:48:08 -070082func (m *message) WhichOneof(pref.OneofDescriptor) pref.FieldDescriptor {
83 panic("invalid oneof descriptor")
84}
Damien Neil5b6d0472019-06-14 11:54:07 -070085
Joe Tsai378c1322019-04-25 23:48:08 -070086func (m *message) GetUnknown() pref.RawFields { return nil }
87func (m *message) SetUnknown(pref.RawFields) { return }
Damien Neil5b6d0472019-06-14 11:54:07 -070088
89var descriptor = func() pref.FileDescriptor {
90 p := &descriptorpb.FileDescriptorProto{}
91 if err := prototext.Unmarshal([]byte(descriptorText), p); err != nil {
92 panic(err)
93 }
94 file, err := protodesc.NewFile(p, nil)
95 if err != nil {
96 panic(err)
97 }
98 return file
99}()
100
101func file_irregular_irregular_proto_init() { _ = descriptor }
102
103const descriptorText = `
104 name: "internal/testprotos/irregular/irregular.proto"
105 package: "goproto.proto.thirdparty"
106 message_type {
107 name: "IrregularMessage"
108 field {
109 name: "s"
110 number: 1
111 label: LABEL_OPTIONAL
112 type: TYPE_STRING
113 json_name: "s"
114 }
115 }
116 options {
117 go_package: "google.golang.org/protobuf/internal/testprotos/irregular"
118 }
119`