33 package com.google.protobuf.jruby;
37 import org.jruby.anno.JRubyClass;
38 import org.jruby.anno.JRubyMethod;
39 import org.jruby.runtime.Binding;
40 import org.jruby.runtime.Block;
41 import org.jruby.runtime.ObjectAllocator;
42 import org.jruby.runtime.ThreadContext;
43 import org.jruby.runtime.builtin.IRubyObject;
45 @JRubyClass(
name =
"MessageBuilderContext")
48 RubyModule protobuf = runtime.getClassFromPath(
"Google::Protobuf");
49 RubyClass
cMessageBuilderContext = protobuf.defineClassUnder(
"MessageBuilderContext", runtime.getObject(),
new ObjectAllocator() {
51 public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
64 this.
cFieldDescriptor = (RubyClass) context.runtime.getClassFromPath(
"Google::Protobuf::FieldDescriptor");
65 this.
cDescriptor = (RubyClass) context.runtime.getClassFromPath(
"Google::Protobuf::Descriptor");
66 this.cOneofDescriptor = (RubyClass) context.runtime.getClassFromPath(
"Google::Protobuf::OneofDescriptor");
67 this.
cOneofBuilderContext = (RubyClass) context.runtime.getClassFromPath(
"Google::Protobuf::Internal::OneofBuilderContext");
82 @JRubyMethod(required = 3, optional = 1)
83 public IRubyObject optional(ThreadContext context, IRubyObject[]
args) {
84 Ruby runtime = context.runtime;
85 IRubyObject typeClass = runtime.getNil();
86 if (
args.length > 3) typeClass =
args[3];
87 msgdefAddField(context,
"optional",
args[0],
args[1],
args[2], typeClass);
88 return context.runtime.getNil();
104 @JRubyMethod(required = 3, optional = 1)
105 public IRubyObject required(ThreadContext context, IRubyObject[]
args) {
106 IRubyObject typeClass = context.runtime.getNil();
107 if (
args.length > 3) typeClass =
args[3];
108 msgdefAddField(context,
"required",
args[0],
args[1],
args[2], typeClass);
109 return context.runtime.getNil();
121 @JRubyMethod(required = 3, optional = 1)
122 public IRubyObject repeated(ThreadContext context, IRubyObject[]
args) {
123 IRubyObject typeClass = context.runtime.getNil();
124 if (
args.length > 3) typeClass =
args[3];
125 msgdefAddField(context,
"repeated",
args[0],
args[1],
args[2], typeClass);
126 return context.runtime.getNil();
141 @JRubyMethod(required = 4, optional = 1)
142 public IRubyObject
map(ThreadContext context, IRubyObject[]
args) {
143 Ruby runtime = context.runtime;
145 IRubyObject keyType =
args[1];
146 IRubyObject valueType =
args[2];
148 IRubyObject typeClass =
args.length > 4 ?
args[4] : context.runtime.getNil();
153 if (keyType.equals(RubySymbol.newSymbol(runtime,
"float")) ||
154 keyType.equals(RubySymbol.newSymbol(runtime,
"double")) ||
155 keyType.equals(RubySymbol.newSymbol(runtime,
"enum")) ||
156 keyType.equals(RubySymbol.newSymbol(runtime,
"message")))
157 throw runtime.newArgumentError(
"Cannot add a map field with a float, double, enum, or message type.");
162 IRubyObject mapentryDescName = RubySymbol.newSymbol(runtime,
name).id2name(context);
163 mapentryDesc.
setName(context, mapentryDescName);
168 keyField.
setName(context, runtime.newString(
"key"));
169 keyField.
setLabel(context, RubySymbol.newSymbol(runtime,
"optional"));
170 keyField.
setNumber(context, runtime.newFixnum(1));
171 keyField.
setType(context, keyType);
172 mapentryDesc.
addField(context, keyField);
176 valueField.
setName(context, runtime.newString(
"value"));
177 valueField.
setLabel(context, RubySymbol.newSymbol(runtime,
"optional"));
178 valueField.
setNumber(context, runtime.newFixnum(2));
179 valueField.
setType(context, valueType);
180 if (! typeClass.isNil()) valueField.
setSubmsgName(context, typeClass);
181 mapentryDesc.
addField(context, valueField);
185 this.builder.pendingList.add(mapentryDesc);
187 msgdefAddField(context,
"repeated",
name, runtime.newSymbol(
"message"),
number, mapentryDescName);
188 return runtime.getNil();
192 public IRubyObject
oneof(ThreadContext context, IRubyObject
name, Block block) {
194 cOneofDescriptor.newInstance(context, Block.NULL_BLOCK);
198 Binding binding = block.getBinding();
199 binding.setSelf(ctx);
200 block.yieldSpecific(context);
202 return context.runtime.getNil();
206 IRubyObject
type, IRubyObject
number, IRubyObject typeClass) {