-
Notifications
You must be signed in to change notification settings - Fork 15.3k
/
RubyFieldDescriptor.java
305 lines (282 loc) · 10.4 KB
/
RubyFieldDescriptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* Protocol Buffers - Google's data interchange format
* Copyright 2014 Google Inc. All rights reserved.
* https://developers.google.com/protocol-buffers/
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.protobuf.jruby;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.LegacyDescriptorsUtil.LegacyFileDescriptor;
import org.jruby.*;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.Block;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
@JRubyClass(name = "FieldDescriptor")
public class RubyFieldDescriptor extends RubyObject {
public static void createRubyFieldDescriptor(Ruby runtime) {
RubyModule mProtobuf = runtime.getClassFromPath("Google::Protobuf");
RubyClass cFieldDescriptor =
mProtobuf.defineClassUnder(
"FieldDescriptor",
runtime.getObject(),
new ObjectAllocator() {
@Override
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
return new RubyFieldDescriptor(runtime, klazz);
}
});
cFieldDescriptor.defineAnnotatedMethods(RubyFieldDescriptor.class);
}
public RubyFieldDescriptor(Ruby runtime, RubyClass klazz) {
super(runtime, klazz);
}
/*
* call-seq:
* FieldDescriptor.default => default
*
* Returns this field's default, as a Ruby object, or nil if not yet set.
*/
// VALUE FieldDescriptor_default(VALUE _self) {
// DEFINE_SELF(FieldDescriptor, self, _self);
// return layout_get_default(self->fielddef);
// }
/*
* call-seq:
* FieldDescriptor.label => label
*
* Returns this field's label (i.e., plurality), as a Ruby symbol.
*
* Valid field labels are:
* :optional, :repeated
*/
@JRubyMethod(name = "label")
public IRubyObject getLabel(ThreadContext context) {
if (label == null) {
calculateLabel(context);
}
return label;
}
/*
* call-seq:
* FieldDescriptor.name => name
*
* Returns the name of this field as a Ruby String, or nil if it is not set.
*/
@JRubyMethod(name = "name")
public IRubyObject getName(ThreadContext context) {
return this.name;
}
protected void setName(IRubyObject name) {
this.name = name;
}
/*
* call-seq:
* FieldDescriptor.subtype => message_or_enum_descriptor
*
* Returns the message or enum descriptor corresponding to this field's type if
* it is a message or enum field, respectively, or nil otherwise. Cannot be
* called *until* the containing message type is added to a pool (and thus
* resolved).
*/
@JRubyMethod(name = "subtype")
public IRubyObject getSubtype(ThreadContext context) {
if (subtype == null) {
calculateSubtype(context);
}
return subtype;
}
/*
* call-seq:
* FieldDescriptor.type => type
*
* Returns this field's type, as a Ruby symbol, or nil if not yet set.
*
* Valid field types are:
* :int32, :int64, :uint32, :uint64, :float, :double, :bool, :string,
* :bytes, :message.
*/
@JRubyMethod(name = "type")
public IRubyObject getType(ThreadContext context) {
return Utils.fieldTypeToRuby(context, descriptor.getType());
}
/*
* call-seq:
* FieldDescriptor.number => number
*
* Returns the tag number for this field.
*/
@JRubyMethod(name = "number")
public IRubyObject getNumber(ThreadContext context) {
return this.number;
}
/*
* call-seq:
* FieldDescriptor.submsg_name => submsg_name
*
* Returns the name of the message or enum type corresponding to this field, if
* it is a message or enum field (respectively), or nil otherwise. This type
* name will be resolved within the context of the pool to which the containing
* message type is added.
*/
// VALUE FieldDescriptor_submsg_name(VALUE _self) {
// DEFINE_SELF(FieldDescriptor, self, _self);
// switch (upb_fielddef_type(self->fielddef)) {
// case UPB_TYPE_ENUM:
// return rb_str_new2(
// upb_enumdef_fullname(upb_fielddef_enumsubdef(self->fielddef)));
// case UPB_TYPE_MESSAGE:
// return rb_str_new2(
// upb_msgdef_fullname(upb_fielddef_msgsubdef(self->fielddef)));
// default:
// return Qnil;
// }
// }
/*
* call-seq:
* FieldDescriptor.submsg_name = submsg_name
*
* Sets the name of the message or enum type corresponding to this field, if it
* is a message or enum field (respectively). This type name will be resolved
* within the context of the pool to which the containing message type is added.
* Cannot be called on field that are not of message or enum type, or on fields
* that are part of a message type already added to a pool.
*/
// @JRubyMethod(name = "submsg_name=")
// public IRubyObject setSubmsgName(ThreadContext context, IRubyObject name) {
// this.builder.setTypeName("." + Utils.escapeIdentifier(name.asJavaString()));
// return context.runtime.getNil();
// }
/*
* call-seq:
* FieldDescriptor.clear(message)
*
* Clears the field from the message if it's set.
*/
@JRubyMethod(name = "clear")
public IRubyObject clearValue(ThreadContext context, IRubyObject message) {
return ((RubyMessage) message).clearField(context, descriptor);
}
/*
* call-seq:
* FieldDescriptor.get(message) => value
*
* Returns the value set for this field on the given message. Raises an
* exception if message is of the wrong type.
*/
@JRubyMethod(name = "get")
public IRubyObject getValue(ThreadContext context, IRubyObject message) {
return ((RubyMessage) message).getField(context, descriptor);
}
/*
* call-seq:
* FieldDescriptor.has?(message) => boolean
*
* Returns whether the value is set on the given message. Raises an
* exception when calling for fields that do not have presence.
*/
@JRubyMethod(name = "has?")
public IRubyObject has(ThreadContext context, IRubyObject message) {
return ((RubyMessage) message).hasField(context, descriptor);
}
/*
* call-seq:
* FieldDescriptor.set(message, value)
*
* Sets the value corresponding to this field to the given value on the given
* message. Raises an exception if message is of the wrong type. Performs the
* ordinary type-checks for field setting.
*/
@JRubyMethod(name = "set")
public IRubyObject setValue(ThreadContext context, IRubyObject message, IRubyObject value) {
((RubyMessage) message).setField(context, this, value);
return context.nil;
}
@JRubyMethod
public IRubyObject options(ThreadContext context) {
RubyDescriptor fieldOptionsDescriptor =
(RubyDescriptor)
pool.lookup(context, context.runtime.newString("google.protobuf.FieldOptions"));
RubyClass fieldOptionsClass = (RubyClass) fieldOptionsDescriptor.msgclass(context);
RubyMessage msg = (RubyMessage) fieldOptionsClass.newInstance(context, Block.NULL_BLOCK);
return msg.decodeBytes(
context,
msg,
CodedInputStream.newInstance(
descriptor.getOptions().toByteString().toByteArray()), /*freeze*/
true);
}
protected void setDescriptor(
ThreadContext context, FieldDescriptor descriptor, RubyDescriptorPool pool) {
if (descriptor.isRequired()
&& LegacyFileDescriptor.getSyntax(descriptor.getFile())
== LegacyFileDescriptor.Syntax.PROTO3) {
throw Utils.createTypeError(
context,
descriptor.getName()
+ " is labeled required but required fields are unsupported in proto3");
}
this.descriptor = descriptor;
this.name = context.runtime.newString(descriptor.getName());
this.pool = pool;
}
protected FieldDescriptor getDescriptor() {
return descriptor;
}
private void calculateLabel(ThreadContext context) {
if (descriptor.isRepeated()) {
this.label = context.runtime.newSymbol("repeated");
} else if (descriptor.isOptional()) {
this.label = context.runtime.newSymbol("optional");
} else {
this.label = context.nil;
}
}
private void calculateSubtype(ThreadContext context) {
FieldDescriptor.Type fdType = descriptor.getType();
if (fdType == FieldDescriptor.Type.MESSAGE) {
RubyString messageName = context.runtime.newString(descriptor.getMessageType().getFullName());
this.subtype = pool.lookup(context, messageName);
} else if (fdType == FieldDescriptor.Type.ENUM) {
RubyString enumName = context.runtime.newString(descriptor.getEnumType().getFullName());
this.subtype = pool.lookup(context, enumName);
} else {
this.subtype = context.nil;
}
}
private static final String DOT = ".";
private FieldDescriptor descriptor;
private IRubyObject name;
private IRubyObject label;
private IRubyObject number;
private IRubyObject subtype;
private RubyDescriptorPool pool;
}