@Override void validate(FieldDescriptor fieldDescriptor) { checkArgument( !fieldDescriptor.isMapField(), "%s is a map field; it has no order", fieldDescriptor); checkArgument( fieldDescriptor.isRepeated(), "%s is not a repeated field; it has no order", fieldDescriptor); } },
@Override void validate(FieldDescriptor fieldDescriptor) { checkArgument( fieldDescriptor.isRepeated(), "%s is not a repeated field or a map field; it cannot contain extra elements", fieldDescriptor); } },
/** * call-seq: * Message.dup => new_message * Performs a shallow copy of this message and returns the new copy. */ @JRubyMethod public IRubyObject dup(ThreadContext context) { RubyMessage dup = (RubyMessage) metaClass.newInstance(context, Block.NULL_BLOCK); IRubyObject value; for (Descriptors.FieldDescriptor fieldDescriptor : this.descriptor.getFields()) { if (fieldDescriptor.isRepeated()) { dup.addRepeatedField(fieldDescriptor, this.getRepeatedField(context, fieldDescriptor)); } else if (fields.containsKey(fieldDescriptor)) { dup.fields.put(fieldDescriptor, fields.get(fieldDescriptor)); } else if (this.builder.hasField(fieldDescriptor)) { dup.fields.put(fieldDescriptor, wrapField(context, fieldDescriptor, this.builder.getField(fieldDescriptor))); } } for (Descriptors.FieldDescriptor fieldDescriptor : maps.keySet()) { dup.maps.put(fieldDescriptor, maps.get(fieldDescriptor)); } return dup; }
/** Can this field be packed? i.e. is it a repeated primitive field? */ public boolean isPackable() { return isRepeated() && getLiteType().isPackable(); }
@Override public boolean isRepeated() { return getDescriptor().isRepeated(); }
@Override protected Object getField(Object record, String name, int pos, Object state) { Message m = (Message)record; FieldDescriptor f = ((FieldDescriptor[])state)[pos]; switch (f.getType()) { case MESSAGE: if (!f.isRepeated() && !m.hasField(f)) return null; default: return m.getField(f); } }
@Override public void visit(IRubyObject key, IRubyObject value) { if (!(key instanceof RubySymbol) && !(key instanceof RubyString)) throw runtime.newTypeError("Expected string or symbols as hash keys in initialization map."); final Descriptors.FieldDescriptor fieldDescriptor = findField(context, key); if (Utils.isMapEntry(fieldDescriptor)) { if (!(value instanceof RubyHash)) throw runtime.newArgumentError("Expected Hash object as initializer value for map field '" + key.asJavaString() + "'."); final RubyMap map = newMapForField(context, fieldDescriptor); map.mergeIntoSelf(context, value); maps.put(fieldDescriptor, map); } else if (fieldDescriptor.isRepeated()) { if (!(value instanceof RubyArray)) throw runtime.newArgumentError("Expected array as initializer value for repeated field '" + key.asJavaString() + "'."); RubyRepeatedField repeatedField = rubyToRepeatedField(context, fieldDescriptor, value); addRepeatedField(fieldDescriptor, repeatedField); } else { Descriptors.OneofDescriptor oneof = fieldDescriptor.getContainingOneof(); if (oneof != null) { oneofCases.put(oneof, fieldDescriptor); } if (value instanceof RubyHash && fieldDescriptor.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) { RubyDescriptor descriptor = (RubyDescriptor) getDescriptorForField(context, fieldDescriptor); RubyClass typeClass = (RubyClass) descriptor.msgclass(context); value = (IRubyObject) typeClass.newInstance(context, value, Block.NULL_BLOCK); } fields.put(fieldDescriptor, value); } } });
private Schema getSchema(FieldDescriptor f) { Schema s = getNonRepeatedSchema(f); if (f.isRepeated()) s = Schema.createArray(s); return s; }
public boolean isMapField() { return getType() == Type.MESSAGE && isRepeated() && getMessageType().getOptions().getMapEntry(); }
/** Verifies the value for an enum field. */ private void ensureEnumValueDescriptor( FieldDescriptor field, Object value) { if (field.isRepeated()) { for (Object item : (List) value) { ensureSingularEnumValueDescriptor(field, item); } } else { ensureSingularEnumValueDescriptor(field, value); } }
@Override public Object getField(final FieldDescriptor field) { Object object = internalGetFieldAccessorTable().getField(field).get(this); if (field.isRepeated()) { // The underlying list object is still modifiable at this point. // Make sure not to expose the modifiable list to the caller. return Collections.unmodifiableList((List) object); } else { return object; } }
@Override public Object getField(final FieldDescriptor field) { Object object = internalGetFieldAccessorTable().getField(field).get(this); if (field.isRepeated()) { // The underlying list object is still modifiable at this point. // Make sure not to expose the modifiable list to the caller. return Collections.unmodifiableList((List) object); } else { return object; } }
private void printField(final FieldDescriptor field, final Object value, final TextGenerator generator) throws IOException { if (field.isRepeated()) { // Repeated field. Print each element. for (Object element : (List<?>) value) { printSingleField(field, element, generator); } } else { printSingleField(field, value, generator); } }
@Override public WireFormat.Utf8Validation getUtf8Validation(Descriptors.FieldDescriptor descriptor) { if (descriptor.needsUtf8Check()) { return WireFormat.Utf8Validation.STRICT; } // TODO(liujisi): support lazy strings for repeated fields. if (!descriptor.isRepeated() && builder instanceof GeneratedMessage.Builder) { return WireFormat.Utf8Validation.LAZY; } return WireFormat.Utf8Validation.LOOSE; }
@JRubyMethod(name = {"to_h", "to_hash"}) public IRubyObject toHash(ThreadContext context) { Ruby runtime = context.runtime; RubyHash ret = RubyHash.newHash(runtime); for (Descriptors.FieldDescriptor fdef : this.descriptor.getFields()) { IRubyObject value = getField(context, fdef); if (!value.isNil()) { if (fdef.isRepeated() && !fdef.isMapField()) { if (fdef.getType() != Descriptors.FieldDescriptor.Type.MESSAGE) { value = Helpers.invoke(context, value, "to_a"); } else { RubyArray ary = value.convertToArray(); for (int i = 0; i < ary.size(); i++) { IRubyObject submsg = Helpers.invoke(context, ary.eltInternal(i), "to_h"); ary.eltInternalSet(i, submsg); } value = ary.to_ary(); } } else if (value.respondsTo("to_h")) { value = Helpers.invoke(context, value, "to_h"); } else if (value.respondsTo("to_a")) { value = Helpers.invoke(context, value, "to_a"); } } ret.fastASet(runtime.newSymbol(fdef.getName()), value); } return ret; }
private void printField(final FieldDescriptor field, final Object value, final TextGenerator generator) throws IOException { if (field.isRepeated()) { // Repeated field. Print each element. for (Object element : (List<?>) value) { printSingleField(field, element, generator); } } else { printSingleField(field, value, generator); } }
public Object getField(final FieldDescriptor field) { Object object = internalGetFieldAccessorTable().getField(field).get(this); if (field.isRepeated()) { // The underlying list object is still modifiable at this point. // Make sure not to expose the modifiable list to the caller. return Collections.unmodifiableList((List) object); } else { return object; } }
private MessageType(Descriptors.Descriptor descriptor) { this.fieldDescriptors = descriptor.getFields().toArray(new Descriptors.FieldDescriptor[descriptor.getFields().size()]); this.doesWrapRepeated = fieldDescriptors.length == 1 && fieldDescriptors[0].isRepeated() && descriptor.getName().equalsIgnoreCase(fieldDescriptors[0].getName()); }
protected IRubyObject deepCopy(ThreadContext context) { RubyMessage copy = (RubyMessage) metaClass.newInstance(context, Block.NULL_BLOCK); for (Descriptors.FieldDescriptor fdef : this.descriptor.getFields()) { if (fdef.isRepeated()) { copy.addRepeatedField(fdef, this.getRepeatedField(context, fdef).deepCopy(context)); } else if (fields.containsKey(fdef)) { copy.fields.put(fdef, fields.get(fdef)); } else if (this.builder.hasField(fdef)) { copy.fields.put(fdef, wrapField(context, fdef, this.builder.getField(fdef))); } } return copy; }
public static boolean isMapEntry(Descriptors.FieldDescriptor fieldDescriptor) { return fieldDescriptor.getType() == Descriptors.FieldDescriptor.Type.MESSAGE && fieldDescriptor.isRepeated() && fieldDescriptor.getMessageType().getOptions().getMapEntry(); }