ProtobufUtils { static Object getFieldValue(Descriptors.FieldDescriptor field, Object value, DataType dataType) { Object val; if (value == null) { val = null; } else { if (field.isMapField()) { LOG.trace("FieldDescriptor[{}] is a Map, unrolling", field.getFullName()); MapType mapType = (MapType) dataType; @SuppressWarnings("unchecked") List<DynamicMessage> mapList = (List<DynamicMessage>) value; HashMap<Object, Object> convertedMap = new HashMap<>(); Descriptors.FieldDescriptor keyField = field.getMessageType().findFieldByName(KEY_TYPE); Descriptors.FieldDescriptor valueField = field.getMessageType().findFieldByName(VALUE_TYPE); for (DynamicMessage mapEntry : mapList) { Object k = RowUtils.toRowValue(mapEntry.getField(keyField), mapType.keyType()); Object v = getFieldValue(valueField, mapEntry.getField(valueField), mapType.valueType()); convertedMap.put(k, v); } val = JavaConversions.mapAsScalaMap(convertedMap); } else if (field.isRepeated()) { LOG.trace("FieldDescriptor[{}] is List, unrolling", field.getFullName()); ArrayType arrayType = (ArrayType) dataType; @SuppressWarnings("unchecked") List<Object> children = (List<Object>) value; List<Object> childValues = new ArrayList<>(); for (Object child : children) { switch (field.getType()) { case MESSAGE: Row childRow = RowFactory.create(buildRowValues(field.getMessageType(), (Message) child, (StructType) arrayType.elementType()).toArray()); childValues.add(childRow); break; case ENUM: childValues.add(RowUtils.toRowValue(((Descriptors.EnumValueDescriptor) child).getName(), arrayType.elementType())); break; case GROUP: throw new IllegalStateException("GROUP type not permitted"); default: childValues.add(RowUtils.toRowValue(child, arrayType.elementType())); } } val = JavaConverters.asScalaBufferConverter(childValues).asScala().toSeq(); } else if (field.getType().equals(Descriptors.FieldDescriptor.Type.MESSAGE)) { LOG.trace("FieldDescriptor[{}] is a Message, unrolling", field.getFullName()); val = RowFactory.create(buildRowValues(field.getMessageType(), (DynamicMessage) value, (StructType) dataType).toArray()); } else if (field.getType().equals(Descriptors.FieldDescriptor.Type.ENUM)) { val = RowUtils.toRowValue(((Descriptors.EnumValueDescriptor) value).getName(), dataType); } else if (field.getType().equals(Descriptors.FieldDescriptor.Type.BYTES)) { val = RowUtils.toRowValue(((ByteString) value).toByteArray(), dataType); } else { val = RowUtils.toRowValue(value, dataType); } } LOG.trace("FieldDescriptor[{}] => Value[{}]", field.getFullName(), val); return val; }  static List<Object> buildRowValues(Descriptors.Descriptor dsc, Message msg, StructType schema); static StructType buildSchema(Descriptors.Descriptor descriptor); static Descriptors.Descriptor buildDescriptor(String filePath, String messageName); static Descriptors.Descriptor buildDescriptor(String filePath); static boolean isGzipped(InputStream inputStream);  }
@Test public void getFieldValueNull() { Assert.assertThat(ProtobufUtils.getFieldValue(SINGLE_DESC.findFieldByName("string"), null, SINGLE_SCHEMA.apply("string").dataType()), nullValue()); }