private static <T> T getFromList(List<T> list, int index, FieldDescriptor fieldDescriptor) { if (index >= list.size() || index < 0) { throw new IllegalArgumentException(String.format("Illegal index field: %s, index %d", fieldDescriptor == null ? "<null>" : fieldDescriptor.getName(), index)); } return list.get(index); }
private String layoutInspect() { ThreadContext context = getRuntime().getCurrentContext(); StringBuilder sb = new StringBuilder(); for (Descriptors.FieldDescriptor fdef : descriptor.getFields()) { sb.append(Utils.unescapeIdentifier(fdef.getName())); sb.append(": "); sb.append(getField(context, fdef).inspect()); sb.append(", "); } return sb.substring(0, sb.length() - 2); }
@Override public JsonElement serialize(EntityData.GlobalStore src, Type typeOfSrc, JsonSerializationContext context) { JsonObject result = new JsonObject(); for (Map.Entry<Descriptors.FieldDescriptor, Object> field : src.getAllFields().entrySet()) { result.add(field.getKey().getName(), context.serialize(field.getValue())); } return result; }
private static void writeFieldValue(Descriptors.FieldDescriptor fieldDescriptor, Object value, JsonWriter writer) { switch (fieldDescriptor.getJavaType()) { case INT: writer.value((Integer) value); break; case LONG: writer.value((Long) value); break; case DOUBLE: writer.value((Double) value); break; case BOOLEAN: writer.value((Boolean) value); break; case STRING: writer.value((String) value); break; case ENUM: writer.value(((Descriptors.EnumValueDescriptor) value).getName()); break; case MESSAGE: writeMessageValue((Message) value, writer); break; default: throw new IllegalStateException(String.format("JSON format does not support type '%s' of field '%s'", fieldDescriptor.getJavaType(), fieldDescriptor.getName())); } }
private static String subMessagePrefix(final String prefix, final Descriptors.FieldDescriptor field, final int index) { final StringBuilder result = new StringBuilder(prefix); if (field.isExtension()) { result.append('(') .append(field.getFullName()) .append(')'); } else { result.append(field.getName()); } if (index != -1) { result.append('[') .append(index) .append(']'); } result.append('.'); return result.toString(); }
/** * Adds a field to the fieldsByNumber table. Throws an exception if a * field with the same containing type and number already exists. */ void addFieldByNumber(final FieldDescriptor field) throws DescriptorValidationException { final DescriptorIntPair key = new DescriptorIntPair(field.getContainingType(), field.getNumber()); final FieldDescriptor old = fieldsByNumber.put(key, field); if (old != null) { fieldsByNumber.put(key, old); throw new DescriptorValidationException(field, "Field number " + field.getNumber() + " has already been used in \"" + field.getContainingType().getFullName() + "\" by field \"" + old.getName() + "\"."); } }
private static String subMessagePrefix(final String prefix, final FieldDescriptor field, final int index) { final StringBuilder result = new StringBuilder(prefix); if (field.isExtension()) { result.append('(') .append(field.getFullName()) .append(')'); } else { result.append(field.getName()); } if (index != -1) { result.append('[') .append(index) .append(']'); } result.append('.'); return result.toString(); }
/** * Adds a field to the fieldsByNumber table. Throws an exception if a * field with the same containing type and number already exists. */ void addFieldByNumber(final FieldDescriptor field) throws DescriptorValidationException { final DescriptorIntPair key = new DescriptorIntPair(field.getContainingType(), field.getNumber()); final FieldDescriptor old = fieldsByNumber.put(key, field); if (old != null) { fieldsByNumber.put(key, old); throw new DescriptorValidationException(field, "Field number " + field.getNumber() + "has already been used in \"" + field.getContainingType().getFullName() + "\" by field \"" + old.getName() + "\"."); } }
throw runtime.newTypeError("Required fields are unsupported in proto3"); RubyFieldDescriptor rubyFieldDescriptor = rubyDescriptor.lookup(fieldDescriptor.getName()); rubyFieldDescriptor.setFieldDef(fieldDescriptor); if (fieldDescriptor.getType() == Descriptors.FieldDescriptor.Type.MESSAGE) {
private static String name(FieldDescriptor fieldDescriptor) { return fieldDescriptor.isExtension() ? "[" + fieldDescriptor + "]" : fieldDescriptor.getName(); }
private static void writeMessage(Message message, JsonWriter writer) { MessageType type = MessageType.of(message); for (Descriptors.FieldDescriptor fieldDescriptor : type.fieldDescriptors) { if (fieldDescriptor.isRepeated()) { writer.name(fieldDescriptor.getName()); if (fieldDescriptor.isMapField()) { writeMap((Collection<MapEntry>) message.getField(fieldDescriptor), writer); } else { writeArray(writer, fieldDescriptor, (Collection) message.getField(fieldDescriptor)); } } else if (message.hasField(fieldDescriptor)) { writer.name(fieldDescriptor.getName()); Object fieldValue = message.getField(fieldDescriptor); writeFieldValue(fieldDescriptor, fieldValue, writer); } } }
private static Set<String> getFieldNames(Class<? extends Message> clazz) { Set<String> resultSet = new HashSet<>(); for (Descriptors.FieldDescriptor descriptor : ProtoUtils.getFieldDescriptors(clazz)) { resultSet.add(ProtoUtils.toCamelCase(descriptor.getName())); } return resultSet; } }
@JRubyMethod(name = "method_missing", rest = true) public IRubyObject methodMissing(ThreadContext context, IRubyObject[] args) { if (args.length == 1) { RubyDescriptor rubyDescriptor = (RubyDescriptor) getDescriptor(context, metaClass); IRubyObject oneofDescriptor = rubyDescriptor.lookupOneof(context, args[0]); if (oneofDescriptor.isNil()) { if (!hasField(args[0])) { return Helpers.invokeSuper(context, this, metaClass, "method_missing", args, Block.NULL_BLOCK); } return index(context, args[0]); } RubyOneofDescriptor rubyOneofDescriptor = (RubyOneofDescriptor) oneofDescriptor; Descriptors.FieldDescriptor fieldDescriptor = oneofCases.get(rubyOneofDescriptor.getOneofDescriptor()); if (fieldDescriptor == null) return context.runtime.getNil(); return context.runtime.newSymbol(fieldDescriptor.getName()); } else { // fieldName is RubySymbol RubyString field = args[0].asString(); RubyString equalSign = context.runtime.newString(Utils.EQUAL_SIGN); if (field.end_with_p(context, equalSign).isTrue()) { field.chomp_bang(context, equalSign); } if (!hasField(field)) { return Helpers.invokeSuper(context, this, metaClass, "method_missing", args, Block.NULL_BLOCK); } return indexSet(context, field, args[1]); } }
@Override final void printContents(boolean includeMatches, String fieldPrefix, StringBuilder sb) { fieldPrefix = newFieldPrefix(fieldPrefix, fieldDescriptor().getName()); for (PairResult pairResult : pairResults()) { pairResult.printContentsForRepeatedField( /* includeSelfAlways = */ !isMatched(), includeMatches, fieldPrefix, sb); } }
private static FieldInfo newFieldInfo(FieldDescriptor fieldDescriptor) { return new FieldInfo( fieldDescriptor.getName(), fieldDescriptor.isRequired() ? FieldRequirement.REQUIRED : FieldRequirement.OPTIONAL, newFieldTypeInfo(fieldDescriptor)); }
@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; }
/** Returns a short, human-readable version of this identifier. */ final String shortName() { if (fieldDescriptor().isPresent()) { return fieldDescriptor().get().isExtension() ? "[" + fieldDescriptor().get() + "]" : fieldDescriptor().get().getName(); } else { return String.valueOf(unknownFieldDescriptor().get().fieldNumber()); } } }
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()); }
@Override public void accept(Descriptors.FieldDescriptor fieldDescriptor) { protoBufParsingInfos.add(new ProtoBufParsingInfo(fieldDescriptor.getNumber(), fieldDescriptor.getName(), fieldDescriptor.getType().name(), fieldDescriptor.toProto().getLabel().name(), null)); } });
private IRubyObject getDescriptorForField(ThreadContext context, Descriptors.FieldDescriptor fieldDescriptor) { RubyDescriptor thisRbDescriptor = (RubyDescriptor) getDescriptor(context, metaClass); return thisRbDescriptor.lookup(fieldDescriptor.getName()).getSubType(context); }