@SuppressWarnings("unchecked") static FieldValue fromPb(Object cellPb, Field recordSchema) { if (Data.isNull(cellPb)) { return FieldValue.of(Attribute.PRIMITIVE, null); } if (cellPb instanceof String) { return FieldValue.of(Attribute.PRIMITIVE, cellPb); } if (cellPb instanceof List) { return FieldValue.of(Attribute.REPEATED, FieldValueList.fromPb((List<Object>) cellPb, null)); } if (cellPb instanceof Map) { Map<String, Object> cellMapPb = (Map<String, Object>) cellPb; if (cellMapPb.containsKey("f")) { FieldList subFieldsSchema = recordSchema != null ? recordSchema.getSubFields() : null; return FieldValue.of( Attribute.RECORD, FieldValueList.fromPb((List<Object>) cellMapPb.get("f"), subFieldsSchema)); } // This should never be the case when we are processing a first level table field (i.e. a // row's field, not a record sub-field) if (cellMapPb.containsKey("v")) { return FieldValue.fromPb(cellMapPb.get("v"), recordSchema); } } throw new IllegalArgumentException("Unexpected table cell format"); } }
private static FieldValueList newFieldValueList(String s) { return FieldValueList.of(ImmutableList.of(FieldValue.of(PRIMITIVE, s))); }
@Test public void testEquals() { FieldValue booleanValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, "false"); assertEquals(booleanValue, FieldValue.fromPb(BOOLEAN_FIELD)); assertEquals(booleanValue.hashCode(), FieldValue.fromPb(BOOLEAN_FIELD).hashCode()); FieldValue integerValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, "1"); assertEquals(integerValue, FieldValue.fromPb(INTEGER_FIELD)); assertEquals(integerValue.hashCode(), FieldValue.fromPb(INTEGER_FIELD).hashCode()); FieldValue floatValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, "1.5"); assertEquals(floatValue, FieldValue.fromPb(FLOAT_FIELD)); assertEquals(floatValue.hashCode(), FieldValue.fromPb(FLOAT_FIELD).hashCode()); FieldValue numericValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, "123456789.123456789"); assertEquals(numericValue, FieldValue.fromPb(NUMERIC_FIELD)); assertEquals(numericValue.hashCode(), FieldValue.fromPb(NUMERIC_FIELD).hashCode()); FieldValue stringValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, "string"); assertEquals(stringValue, FieldValue.fromPb(STRING_FIELD)); assertEquals(stringValue.hashCode(), FieldValue.fromPb(STRING_FIELD).hashCode()); FieldValue timestampValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, "42"); assertEquals(timestampValue, FieldValue.fromPb(TIMESTAMP_FIELD)); assertEquals(timestampValue.hashCode(), FieldValue.fromPb(TIMESTAMP_FIELD).hashCode()); FieldValue bytesValue = FieldValue.of(FieldValue.Attribute.PRIMITIVE, BYTES_BASE64); assertEquals(bytesValue, FieldValue.fromPb(BYTES_FIELD)); assertEquals(bytesValue.hashCode(), FieldValue.fromPb(BYTES_FIELD).hashCode());
@SuppressWarnings("unchecked") static FieldValue fromPb(Object cellPb, Field recordSchema) { if (Data.isNull(cellPb)) { return FieldValue.of(Attribute.PRIMITIVE, null); } if (cellPb instanceof String) { return FieldValue.of(Attribute.PRIMITIVE, cellPb); } if (cellPb instanceof List) { return FieldValue.of(Attribute.REPEATED, FieldValueList.fromPb((List<Object>) cellPb, null)); } if (cellPb instanceof Map) { Map<String, Object> cellMapPb = (Map<String, Object>) cellPb; if (cellMapPb.containsKey("f")) { FieldList subFieldsSchema = recordSchema != null ? recordSchema.getSubFields() : null; return FieldValue.of( Attribute.RECORD, FieldValueList.fromPb((List<Object>) cellMapPb.get("f"), subFieldsSchema)); } // This should never be the case when we are processing a first level table field (i.e. a // row's field, not a record sub-field) if (cellMapPb.containsKey("v")) { return FieldValue.fromPb(cellMapPb.get("v"), recordSchema); } } throw new IllegalArgumentException("Unexpected table cell format"); } }