@Test public void testWrongDataStructure() throws DataException { List<Integer> x = Arrays.asList(1, 2, 3); Exception e = assertThrows(DataException.class, () -> { DataUtils.getField(x, "foo"); }); assertThat(e.getMessage(), startsWith("Argument not a Struct or Map")); }
public static Object getField(Object structOrMap, String fieldName) { validate(structOrMap, fieldName); Object field; if (structOrMap instanceof Struct) { field = ((Struct) structOrMap).get(fieldName); } else if (structOrMap instanceof Map) { field = ((Map<?, ?>) structOrMap).get(fieldName); if (field == null) { throw new DataException(String.format("Unable to find nested field '%s'", fieldName)); } return field; } else { throw new DataException(String.format( "Argument not a Struct or Map. Cannot get field '%s' from %s.", fieldName, structOrMap )); } if (field == null) { throw new DataException( String.format("The field '%s' does not exist in %s.", fieldName, structOrMap)); } return field; }
DataUtils { public static Object getField(Object structOrMap, String fieldName) { validate(structOrMap, fieldName); Object field; if (structOrMap instanceof Struct) { field = ((Struct) structOrMap).get(fieldName); } else if (structOrMap instanceof Map) { field = ((Map<?, ?>) structOrMap).get(fieldName); if (field == null) { throw new DataException(String.format("Unable to find nested field '%s'", fieldName)); } return field; } else { throw new DataException(String.format( "Argument not a Struct or Map. Cannot get field '%s' from %s.", fieldName, structOrMap )); } if (field == null) { throw new DataException( String.format("The field '%s' does not exist in %s.", fieldName, structOrMap)); } return field; } }
DataUtils { public static Object getField(Object structOrMap, String fieldName) { validate(structOrMap, fieldName); Object field; if (structOrMap instanceof Struct) { field = ((Struct) structOrMap).get(fieldName); } else if (structOrMap instanceof Map) { field = ((Map<?, ?>) structOrMap).get(fieldName); if (field == null) { throw new DataException(String.format("Unable to find nested field '%s'", fieldName)); } return field; } else { throw new DataException(String.format( "Argument not a Struct or Map. Cannot get field '%s' from %s.", fieldName, structOrMap )); } if (field == null) { throw new DataException( String.format("The field '%s' does not exist in %s.", fieldName, structOrMap)); } return field; }  }
DataUtils { public static Object getField(Object structOrMap, String fieldName) { validate(structOrMap, fieldName); Object field; if (structOrMap instanceof Struct) { field = ((Struct) structOrMap).get(fieldName); } else if (structOrMap instanceof Map) { field = ((Map<?, ?>) structOrMap).get(fieldName); if (field == null) { throw new DataException(String.format("Unable to find nested field '%s'", fieldName)); } return field; } else { throw new DataException(String.format( "Argument not a Struct or Map. Cannot get field '%s' from %s.", fieldName, structOrMap )); } if (field == null) { throw new DataException( String.format("The field '%s' does not exist in %s.", fieldName, structOrMap)); } return field; }  static Object getField(Object structOrMap, String fieldName); static Object getNestedFieldValue(Object structOrMap, String fieldName); static Field getNestedField(Schema schema, String fieldName); }
DataUtils { public static Object getField(Object structOrMap, String fieldName) { validate(structOrMap, fieldName); Object field; if (structOrMap instanceof Struct) { field = ((Struct) structOrMap).get(fieldName); } else if (structOrMap instanceof Map) { field = ((Map<?, ?>) structOrMap).get(fieldName); if (field == null) { throw new DataException(String.format("Unable to find nested field '%s'", fieldName)); } return field; } else { throw new DataException(String.format( "Argument not a Struct or Map. Cannot get field '%s' from %s.", fieldName, structOrMap )); } if (field == null) { throw new DataException( String.format("The field '%s' does not exist in %s.", fieldName, structOrMap)); } return field; }  static Object getField(Object structOrMap, String fieldName); static Object getNestedFieldValue(Object structOrMap, String fieldName); static Field getNestedField(Schema schema, String fieldName);  }