@Test public void testMissingNestedField() throws DataException { String topField = "nested"; String fieldName = "foo"; Map<String, Object> innerMap = createMapWithTimestampField(TIMESTAMP); Map<String, Object> map = new HashMap<>(); map.put(topField, innerMap); Exception e = assertThrows(DataException.class, () -> { DataUtils.getNestedFieldValue(map, topField + "." + fieldName); }); assertThat(e.getMessage(), startsWith(String.format("The field '%s.%s' does not exist in", topField, fieldName))); }
public static Object getNestedFieldValue(Object structOrMap, String fieldName) { validate(structOrMap, fieldName); try { Object innermost = structOrMap; for (String name : fieldName.split("\\.")) { innermost = getField(innermost, name); } return innermost; } catch (DataException e) { throw new DataException( String.format("The field '%s' does not exist in %s.", fieldName, structOrMap), e ); } }
DataUtils { public static Object getNestedFieldValue(Object structOrMap, String fieldName) { validate(structOrMap, fieldName); try { Object innermost = structOrMap; for (String name : fieldName.split("\\.")) { innermost = getField(innermost, name); } return innermost; } catch (DataException e) { throw new DataException( String.format("The field '%s' does not exist in %s.", fieldName, structOrMap), e ); } } }
DataUtils { public static Object getNestedFieldValue(Object structOrMap, String fieldName) { validate(structOrMap, fieldName); try { Object innermost = structOrMap; for (String name : fieldName.split("\\.")) { innermost = getField(innermost, name); } return innermost; } catch (DataException e) { throw new DataException( String.format("The field '%s' does not exist in %s.", fieldName, structOrMap), e ); } }  }
DataUtils { public static Object getNestedFieldValue(Object structOrMap, String fieldName) { validate(structOrMap, fieldName); try { Object innermost = structOrMap; for (String name : fieldName.split("\\.")) { innermost = getField(innermost, name); } return innermost; } catch (DataException e) { throw new DataException( String.format("The field '%s' does not exist in %s.", fieldName, structOrMap), e ); } }  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 getNestedFieldValue(Object structOrMap, String fieldName) { validate(structOrMap, fieldName); try { Object innermost = structOrMap; for (String name : fieldName.split("\\.")) { innermost = getField(innermost, name); } return innermost; } catch (DataException e) { throw new DataException( String.format("The field '%s' does not exist in %s.", fieldName, structOrMap), e ); } }  static Object getField(Object structOrMap, String fieldName); static Object getNestedFieldValue(Object structOrMap, String fieldName); static Field getNestedField(Schema schema, String fieldName);  }