aboutsummaryrefslogtreecommitdiffhomepage
path: root/ruby/src/main/java/com/google/protobuf/jruby/RubyFieldDescriptor.java
blob: f3c488bc98666a2a99d45359a1a6e7fd01a84c9e (plain)
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
/*
 * 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.DescriptorProtos;
import com.google.protobuf.Descriptors;
import org.jruby.*;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
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 createRubyFileDescriptor(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.new => field
     *
     * Returns a new field descriptor. Its name, type, etc. must be set before it is
     * added to a message type.
     */
    @JRubyMethod
    public IRubyObject initialize(ThreadContext context) {
        builder = DescriptorProtos.FieldDescriptorProto.newBuilder();
        return this;
    }

    /*
     * call-seq:
     *     FieldDescriptor.label
     *
     * Return the label of this field.
     */
    @JRubyMethod(name = "label")
    public IRubyObject getLabel(ThreadContext context) {
        return this.label;
    }

    /*
     * call-seq:
     *     FieldDescriptor.label = label
     *
     * Sets the label on this field. Cannot be called if field is part of a message
     * type already in a pool.
     */
    @JRubyMethod(name = "label=")
    public IRubyObject setLabel(ThreadContext context, IRubyObject value) {
        String labelName = value.asJavaString();
        this.label = context.runtime.newSymbol(labelName.toLowerCase());
        this.builder.setLabel(
                DescriptorProtos.FieldDescriptorProto.Label.valueOf("LABEL_" + labelName.toUpperCase()));
        return context.runtime.getNil();
    }

    /*
     * 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;
    }

    /*
     * call-seq:
     *     FieldDescriptor.name = name
     *
     * Sets the name of this field. Cannot be called once the containing message
     * type, if any, is added to a pool.
     */
    @JRubyMethod(name = "name=")
    public IRubyObject setName(ThreadContext context, IRubyObject value) {
        String nameStr = value.asJavaString();
        this.name = context.runtime.newString(nameStr);
        this.builder.setName(Utils.escapeIdentifier(nameStr));
        return context.runtime.getNil();
    }


    @JRubyMethod(name = "subtype")
    public IRubyObject getSubType(ThreadContext 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, this.builder.getType());
    }

    /*
     * call-seq:
     *     FieldDescriptor.type = type
     *
     * Sets this field's type. Cannot be called if field is part of a message type
     * already in a pool.
     */
    @JRubyMethod(name = "type=")
    public IRubyObject setType(ThreadContext context, IRubyObject value) {
        this.builder.setType(DescriptorProtos.FieldDescriptorProto.Type.valueOf("TYPE_" + value.asJavaString().toUpperCase()));
        return context.runtime.getNil();
    }

    /*
     * call-seq:
     *     FieldDescriptor.number => number
     *
     * Returns this field's number, as a Ruby Integer, or nil if not yet set.
     *
     */
    @JRubyMethod(name = "number")
    public IRubyObject getnumber(ThreadContext context) {
        return this.number;
    }

    /*
     * call-seq:
     *     FieldDescriptor.number = number
     *
     * Sets the tag number for this field. Cannot be called if field is part of a
     * message type already in a pool.
     */
    @JRubyMethod(name = "number=")
    public IRubyObject setNumber(ThreadContext context, IRubyObject value) {
        this.number = value;
        this.builder.setNumber(RubyNumeric.num2int(value));
        return context.runtime.getNil();
    }

    /*
     * 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.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 msgRb) {
        RubyMessage message = (RubyMessage) msgRb;
        if (message.getDescriptor() != fieldDef.getContainingType()) {
            throw context.runtime.newTypeError("set method called on wrong message type");
        }
        return message.getField(context, fieldDef);
    }

    /*
     * 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 msgRb, IRubyObject value) {
        RubyMessage message = (RubyMessage) msgRb;
        if (message.getDescriptor() != fieldDef.getContainingType()) {
            throw context.runtime.newTypeError("set method called on wrong message type");
        }
        message.setField(context, fieldDef, value);
        return context.runtime.getNil();
    }

    protected void setSubType(IRubyObject rubyDescriptor) {
        this.subType = rubyDescriptor;
    }

    protected void setFieldDef(Descriptors.FieldDescriptor fieldDescriptor) {
        this.fieldDef = fieldDescriptor;
    }

    protected void setOneofName(IRubyObject name) {
        oneofName = name;
    }

    protected void setOneofIndex(int index) {
        hasOneofIndex = true;
        oneofIndex = index;
    }

    protected IRubyObject getOneofName() {
        return oneofName;
    }

    protected Descriptors.FieldDescriptor getFieldDef() {
        return fieldDef;
    }

    protected DescriptorProtos.FieldDescriptorProto build() {
        if (hasOneofIndex)
            builder.setOneofIndex(oneofIndex);
        return this.builder.build();
    }

    private DescriptorProtos.FieldDescriptorProto.Builder builder;
    private IRubyObject name;
    private IRubyObject label;
    private IRubyObject number;
    private IRubyObject subType;
    private IRubyObject oneofName;
    private Descriptors.FieldDescriptor fieldDef;
    private int oneofIndex;
    private boolean hasOneofIndex = false;
}