static void toJson(Types.ListType list, JsonGenerator generator) throws IOException { generator.writeStartObject(); generator.writeStringField(TYPE, LIST); generator.writeNumberField(ELEMENT_ID, list.elementId()); generator.writeFieldName(ELEMENT); toJson(list.elementType(), generator); generator.writeBooleanField(ELEMENT_REQUIRED, !list.isElementOptional()); generator.writeEndObject(); }
@Override public Object list(Types.ListType list, Supplier<Object> elementResult) { int numElements = random.nextInt(20); List<Object> result = Lists.newArrayListWithExpectedSize(numElements); for (int i = 0; i < numElements; i += 1) { // return null 5% of the time when the value is optional if (list.isElementOptional() && random.nextInt(20) == 1) { result.add(null); } else { result.add(elementResult.get()); } } return result; }
@Override public List<String> list(Types.ListType readList, Supplier<List<String>> elementErrors) { if (!currentType.isListType()) { return ImmutableList.of(String.format(": %s cannot be read as a list", currentType)); } Types.ListType list = currentType.asNestedType().asListType(); List<String> errors = Lists.newArrayList(); this.currentType = list.elementType(); try { if (readList.isElementRequired() && list.isElementOptional()) { errors.add(": elements should be required, but are optional"); } errors.addAll(elementErrors.get()); return errors; } finally { this.currentType = list; } }
@Override public Type list(Types.ListType list, Supplier<Type> elementResult) { Preconditions.checkArgument(current instanceof ArrayType, "Not an array: %s", current); ArrayType array = (ArrayType) current; Preconditions.checkArgument(array.containsNull() || !list.isElementOptional(), "Cannot project an array of optional elements as required elements: %s", array); this.current = array.elementType(); try { Type elementType = elementResult.get(); if (list.elementType() == elementType) { return list; } // must be a projected element type, create a new list if (list.isElementOptional()) { return Types.ListType.ofOptional(list.elementId(), elementType); } else { return Types.ListType.ofRequired(list.elementId(), elementType); } } finally { this.current = array; } }
@Override public Type list(Types.ListType list, Supplier<Type> elementResult) { Preconditions.checkArgument(current instanceof ArrayType, "Not an array: %s", current); ArrayType array = (ArrayType) current; Preconditions.checkArgument(array.containsNull() || !list.isElementOptional(), "Cannot project an array of optional elements as required elements: %s", array); this.current = array.elementType(); try { Type elementType = elementResult.get(); if (list.elementType() == elementType) { return list; } // must be a projected element type, create a new list if (list.isElementOptional()) { return Types.ListType.ofOptional(list.elementId(), elementType); } else { return Types.ListType.ofRequired(list.elementId(), elementType); } } finally { this.current = array; } }
@Override public Type list(Types.ListType list, Supplier<Type> elementTypeFuture) { Preconditions.checkArgument(sourceType.isListType(), "Not a list: " + sourceType); Types.ListType sourceList = sourceType.asListType(); this.sourceType = sourceList.elementType(); try { Type elementType = elementTypeFuture.get(); if (list.elementType() == elementType) { return list; } if (list.isElementOptional()) { return Types.ListType.ofOptional(list.elementId(), elementType); } else { return Types.ListType.ofRequired(list.elementId(), elementType); } } finally { this.sourceType = sourceList; } }
@Override public Type list(Types.ListType list, Type result) { // use field to apply updates Type elementResult = field(list.fields().get(0), result); if (elementResult == null) { throw new IllegalArgumentException("Cannot delete element type from list: " + list); } if (list.elementType() == elementResult) { return list; } if (list.isElementOptional()) { return Types.ListType.ofOptional(list.elementId(), elementResult); } else { return Types.ListType.ofRequired(list.elementId(), elementResult); } }
@Override public GenericArrayData list(Types.ListType list, Supplier<Object> elementResult) { int numElements = random.nextInt(20); Object[] arr = new Object[numElements]; GenericArrayData result = new GenericArrayData(arr); for (int i = 0; i < numElements; i += 1) { // return null 5% of the time when the value is optional if (list.isElementOptional() && random.nextInt(20) == 1) { arr[i] = null; } else { arr[i] = elementResult.get(); } } return result; }
@Override public Schema list(Types.ListType list, Schema elementSchema) { Schema listSchema = results.get(list); if (listSchema != null) { return listSchema; } if (list.isElementOptional()) { listSchema = Schema.createArray(toOption(elementSchema)); } else { listSchema = Schema.createArray(elementSchema); } listSchema.addProp(AvroSchemaUtil.ELEMENT_ID_PROP, list.elementId()); results.put(list, listSchema); return listSchema; }
@Override public Type list(Types.ListType list, Supplier<Type> elementTypeFuture) { Preconditions.checkArgument(sourceType.isListType(), "Not a list: " + sourceType); Types.ListType sourceList = sourceType.asListType(); int sourceElementId = sourceList.elementId(); this.sourceType = sourceList.elementType(); try { if (list.isElementOptional()) { return Types.ListType.ofOptional(sourceElementId, elementTypeFuture.get()); } else { return Types.ListType.ofRequired(sourceElementId, elementTypeFuture.get()); } } finally { this.sourceType = sourceList; } }
@Override public Type list(Types.ListType list, Type elementResult) { if (selected.contains(list.elementId())) { return list; } else if (elementResult != null) { if (list.elementType() == elementResult) { return list; } else if (list.isElementOptional()) { return Types.ListType.ofOptional(list.elementId(), elementResult); } else { return Types.ListType.ofRequired(list.elementId(), elementResult); } } return null; }
@Override public Object list(Types.ListType list, Supplier<Object> elementResult) { int numElements = random.nextInt(20); List<Object> result = Lists.newArrayListWithExpectedSize(numElements); for (int i = 0; i < numElements; i += 1) { // return null 5% of the time when the value is optional if (list.isElementOptional() && random.nextInt(20) == 1) { result.add(null); } else { result.add(elementResult.get()); } } return result; }
@Override public Object list(Types.ListType list, Supplier<Object> elementResult) { int numElements = random.nextInt(20); List<Object> result = Lists.newArrayListWithExpectedSize(numElements); for (int i = 0; i < numElements; i += 1) { // return null 5% of the time when the value is optional if (list.isElementOptional() && random.nextInt(20) == 1) { result.add(null); } else { result.add(elementResult.get()); } } return result; }
@Override public DataType list(Types.ListType list, DataType elementResult) { return ArrayType$.MODULE$.apply(elementResult, list.isElementOptional()); }
@Override public Type list(Types.ListType list, Supplier<Type> future) { int newId = nextId.get(); if (list.isElementOptional()) { return Types.ListType.ofOptional(newId, future.get()); } else { return Types.ListType.ofRequired(newId, future.get()); } }