static FieldValue fromPb(Object cellPb) { return fromPb(cellPb, null); }
static FieldValueList fromPb(List<?> rowPb, FieldList schema) { List<FieldValue> row = new ArrayList<>(rowPb.size()); if (schema != null) { if (schema.size() != rowPb.size()) { throw new IllegalArgumentException("Row size and fields schema sizes should match"); } Iterator<Field> schemaIter = schema.iterator(); Iterator<?> rowPbIter = rowPb.iterator(); while (rowPbIter.hasNext() && schemaIter.hasNext()) { row.add(FieldValue.fromPb(rowPbIter.next(), schemaIter.next())); } } else { for (Object cellPb : rowPb) { row.add(FieldValue.fromPb(cellPb, null)); } } return FieldValueList.of(row, schema); } }
@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"); } }
@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()); assertEquals(integerValue, FieldValue.fromPb(INTEGER_FIELD)); assertEquals(integerValue.hashCode(), FieldValue.fromPb(INTEGER_FIELD).hashCode()); assertEquals(floatValue, FieldValue.fromPb(FLOAT_FIELD)); assertEquals(floatValue.hashCode(), FieldValue.fromPb(FLOAT_FIELD).hashCode()); assertEquals(numericValue, FieldValue.fromPb(NUMERIC_FIELD)); assertEquals(numericValue.hashCode(), FieldValue.fromPb(NUMERIC_FIELD).hashCode()); assertEquals(stringValue, FieldValue.fromPb(STRING_FIELD)); assertEquals(stringValue.hashCode(), FieldValue.fromPb(STRING_FIELD).hashCode()); assertEquals(timestampValue, FieldValue.fromPb(TIMESTAMP_FIELD)); assertEquals(timestampValue.hashCode(), FieldValue.fromPb(TIMESTAMP_FIELD).hashCode()); assertEquals(bytesValue, FieldValue.fromPb(BYTES_FIELD)); assertEquals(bytesValue.hashCode(), FieldValue.fromPb(BYTES_FIELD).hashCode()); assertEquals(nullValue, FieldValue.fromPb(NULL_FIELD)); assertEquals(nullValue.hashCode(), FieldValue.fromPb(NULL_FIELD).hashCode()); assertEquals(repeatedValue, FieldValue.fromPb(REPEATED_FIELD)); assertEquals(repeatedValue.hashCode(), FieldValue.fromPb(REPEATED_FIELD).hashCode());
@Test public void testFromPb() { FieldValue value = FieldValue.fromPb(BOOLEAN_FIELD); assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute()); assertFalse(value.getBooleanValue()); value = FieldValue.fromPb(INTEGER_FIELD); assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute()); assertEquals(1, value.getLongValue()); value = FieldValue.fromPb(FLOAT_FIELD); assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute()); assertEquals(1.5, value.getDoubleValue(), 0); value = FieldValue.fromPb(NUMERIC_FIELD); assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute()); assertEquals(new BigDecimal("123456789.123456789"), value.getNumericValue()); value = FieldValue.fromPb(STRING_FIELD); assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute()); assertEquals("string", value.getStringValue()); value = FieldValue.fromPb(TIMESTAMP_FIELD); assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute()); assertEquals(42000000, value.getTimestampValue()); value = FieldValue.fromPb(BYTES_FIELD); assertEquals(FieldValue.Attribute.PRIMITIVE, value.getAttribute()); assertArrayEquals(BYTES, value.getBytesValue()); value = FieldValue.fromPb(NULL_FIELD); assertNull(value.getValue()); value = FieldValue.fromPb(REPEATED_FIELD); assertEquals(FieldValue.Attribute.REPEATED, value.getAttribute()); assertEquals(FieldValue.fromPb(INTEGER_FIELD), value.getRepeatedValue().get(0)); assertEquals(FieldValue.fromPb(INTEGER_FIELD), value.getRepeatedValue().get(1)); value = FieldValue.fromPb(RECORD_FIELD);
@Override public FieldValue apply(Object pb) { return FieldValue.fromPb(pb); } };
static FieldValue fromPb(Object cellPb) { return fromPb(cellPb, null); }
static FieldValueList fromPb(List<?> rowPb, FieldList schema) { List<FieldValue> row = new ArrayList<>(rowPb.size()); if (schema != null) { if (schema.size() != rowPb.size()) { throw new IllegalArgumentException("Row size and fields schema sizes should match"); } Iterator<Field> schemaIter = schema.iterator(); Iterator<?> rowPbIter = rowPb.iterator(); while (rowPbIter.hasNext() && schemaIter.hasNext()) { row.add(FieldValue.fromPb(rowPbIter.next(), schemaIter.next())); } } else { for (Object cellPb : rowPb) { row.add(FieldValue.fromPb(cellPb, null)); } } return FieldValueList.of(row, schema); } }
List<FieldValue> repeatedCells = Lists.newArrayListWithCapacity(cellsListPb.size()); for (Object repeatedCellPb : cellsListPb) { repeatedCells.add(FieldValue.fromPb(repeatedCellPb)); List<FieldValue> recordCells = Lists.newArrayListWithCapacity(cellsListPb.size()); for (Object repeatedCellPb : cellsListPb) { recordCells.add(FieldValue.fromPb(repeatedCellPb)); return FieldValue.fromPb(cellMapPb.get("v"));
@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"); } }