/** * Build the Schema using the current settings * @return the {@link Schema} */ public Schema build() { return new ConnectSchema(type, isOptional(), defaultValue, name, version, doc, parameters == null ? null : Collections.unmodifiableMap(parameters), fields == null ? null : Collections.unmodifiableList(new ArrayList<Field>(fields.values())), keySchema, valueSchema); }
/** * Validate that the value can be used with the schema, i.e. that its type matches the schema type and nullability * requirements. Throws a DataException if the value is invalid. * @param schema Schema to test * @param value value to test */ public static void validateValue(Schema schema, Object value) { validateValue(null, schema, value); }
public static <K, V> void validateFormat(Map<K, V> offsetData) { // Both keys and values for offsets may be null. For values, this is a useful way to delete offsets or indicate // that there's not usable concept of offsets in your source system. if (offsetData == null) return; for (Map.Entry<K, V> entry : offsetData.entrySet()) { if (!(entry.getKey() instanceof String)) throw new DataException("Offsets may only use String keys"); Object value = entry.getValue(); if (value == null) continue; Schema.Type schemaType = ConnectSchema.schemaType(value.getClass()); if (schemaType == null) throw new DataException("Offsets may only contain primitive types as values, but field " + entry.getKey() + " contains " + value.getClass()); if (!schemaType.isPrimitive()) throw new DataException("Offsets may only contain primitive types as values, but field " + entry.getKey() + " contains " + schemaType); } } }
List<Class> expectedClasses = expectedClassesFor(schema); List<?> array = (List<?>) value; for (Object entry : array) validateValue(schema.valueSchema(), entry); break; case MAP: Map<?, ?> map = (Map<?, ?>) value; for (Map.Entry<?, ?> entry : map.entrySet()) { validateValue(schema.keySchema(), entry.getKey()); validateValue(schema.valueSchema(), entry.getValue());
Schema.Type inferredType = ConnectSchema.schemaType(value.getClass()); if (inferredType == null) { throw new DataException("Flatten transformation was passed a value of type " + value.getClass()
final Schema.Type schemaType; if (schema == null) { schemaType = ConnectSchema.schemaType(value.getClass()); if (schemaType == null) throw new DataException("Java class " + value.getClass() + " does not have corresponding schema type.");
@Override public R apply(R record) { final Schema schema = operatingSchema(record); requireSchema(schema, "updating schema metadata"); final boolean isArray = schema.type() == Schema.Type.ARRAY; final boolean isMap = schema.type() == Schema.Type.MAP; final Schema updatedSchema = new ConnectSchema( schema.type(), schema.isOptional(), schema.defaultValue(), schemaName != null ? schemaName : schema.name(), schemaVersion != null ? schemaVersion : schema.version(), schema.doc(), schema.parameters(), schema.fields(), isMap ? schema.keySchema() : null, isMap || isArray ? schema.valueSchema() : null ); return newRecord(record, updatedSchema); }
/** * Validate that the value can be used for this schema, i.e. that its type matches the schema type and optional * requirements. Throws a DataException if the value is invalid. * @param value the value to validate */ public void validateValue(Object value) { validateValue(this, value); }
if (value == null) return null; Schema.Type inferredType = schema == null ? ConnectSchema.schemaType(value.getClass()) : schema.type(); if (inferredType == null) {
/** * Set the default value for this schema. The value is validated against the schema type, throwing a * {@link SchemaBuilderException} if it does not match. * @param value the default value * @return the SchemaBuilder */ public SchemaBuilder defaultValue(Object value) { checkCanSet(DEFAULT_FIELD, defaultValue, value); checkNotNull(TYPE_FIELD, type, DEFAULT_FIELD); try { ConnectSchema.validateValue(this, value); } catch (DataException e) { throw new SchemaBuilderException("Invalid default value", e); } defaultValue = value; return this; }
schemaType = ConnectSchema.schemaType(value.getClass()); if (schemaType == null) throw new DataException("Java class " + value.getClass() + " does not have corresponding schema type.");
/** * Set the value of a field. Validates the value, throwing a {@link DataException} if it does not match the field's * {@link Schema}. * @param field the field to set * @param value the value of the field * @return the Struct, to allow chaining of {@link #put(String, Object)} calls */ public Struct put(Field field, Object value) { if (null == field) throw new DataException("field cannot be null."); ConnectSchema.validateValue(field.name(), field.schema(), value); values[field.index()] = value; return this; }
/** * Validates that this struct has filled in all the necessary data with valid values. For required fields * without defaults, this validates that a value has been set and has matching types/schemas. If any validation * fails, throws a DataException. */ public void validate() { for (Field field : schema.fields()) { Schema fieldSchema = field.schema(); Object value = values[field.index()]; if (value == null && (fieldSchema.isOptional() || fieldSchema.defaultValue() != null)) continue; ConnectSchema.validateValue(field.name(), fieldSchema, value); } }