Search in sources :

Example 1 with SchemaValidator

use of com.linkedin.avroutil1.compatibility.SchemaValidator in project avro-util by linkedin.

the class Avro15Adapter method parse.

@Override
public SchemaParseResult parse(String schemaJson, SchemaParseConfiguration desiredConf, Collection<Schema> known) {
    Schema.Parser parser = new Schema.Parser();
    boolean validateNames = true;
    boolean validateDefaults = false;
    if (desiredConf != null) {
        validateNames = desiredConf.validateNames();
        validateDefaults = desiredConf.validateDefaultValues();
    }
    SchemaParseConfiguration configUsed = new SchemaParseConfiguration(validateNames, validateDefaults);
    parser.setValidate(validateNames);
    if (known != null && !known.isEmpty()) {
        Map<String, Schema> knownByFullName = new HashMap<>(known.size());
        for (Schema s : known) {
            knownByFullName.put(s.getFullName(), s);
        }
        parser.addTypes(knownByFullName);
    }
    Schema mainSchema = parser.parse(schemaJson);
    if (validateDefaults) {
        // avro 1.5 doesnt properly validate default values, so we have to do it ourselves
        SchemaValidator validator = new SchemaValidator(configUsed, known);
        // will throw on issues
        AvroSchemaUtil.traverseSchema(mainSchema, validator);
    }
    Map<String, Schema> knownByFullName = parser.getTypes();
    return new SchemaParseResult(mainSchema, knownByFullName, configUsed);
}
Also used : SchemaParseResult(com.linkedin.avroutil1.compatibility.SchemaParseResult) SchemaParseConfiguration(com.linkedin.avroutil1.compatibility.SchemaParseConfiguration) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) SchemaValidator(com.linkedin.avroutil1.compatibility.SchemaValidator)

Example 2 with SchemaValidator

use of com.linkedin.avroutil1.compatibility.SchemaValidator in project avro-util by linkedin.

the class Avro16Adapter method parse.

@Override
public SchemaParseResult parse(String schemaJson, SchemaParseConfiguration desiredConf, Collection<Schema> known) {
    Schema.Parser parser = new Schema.Parser();
    boolean validateNames = true;
    boolean validateDefaults = false;
    if (desiredConf != null) {
        validateNames = desiredConf.validateNames();
        validateDefaults = desiredConf.validateDefaultValues();
    }
    SchemaParseConfiguration configUsed = new SchemaParseConfiguration(validateNames, validateDefaults);
    parser.setValidate(validateNames);
    if (known != null && !known.isEmpty()) {
        Map<String, Schema> knownByFullName = new HashMap<>(known.size());
        for (Schema s : known) {
            knownByFullName.put(s.getFullName(), s);
        }
        parser.addTypes(knownByFullName);
    }
    Schema mainSchema = parser.parse(schemaJson);
    if (validateDefaults) {
        // avro 1.6 doesnt properly validate default values, so we have to do it ourselves
        SchemaValidator validator = new SchemaValidator(configUsed, known);
        // will throw on issues
        AvroSchemaUtil.traverseSchema(mainSchema, validator);
    }
    Map<String, Schema> knownByFullName = parser.getTypes();
    return new SchemaParseResult(mainSchema, knownByFullName, configUsed);
}
Also used : SchemaParseResult(com.linkedin.avroutil1.compatibility.SchemaParseResult) SchemaParseConfiguration(com.linkedin.avroutil1.compatibility.SchemaParseConfiguration) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) SchemaValidator(com.linkedin.avroutil1.compatibility.SchemaValidator)

Example 3 with SchemaValidator

use of com.linkedin.avroutil1.compatibility.SchemaValidator in project avro-util by linkedin.

the class Avro17Adapter method parse.

@Override
public SchemaParseResult parse(String schemaJson, SchemaParseConfiguration desiredConf, Collection<Schema> known) {
    Schema.Parser parser = new Schema.Parser();
    boolean validateNames = true;
    boolean validateDefaults = true;
    if (desiredConf != null) {
        validateNames = desiredConf.validateNames();
        validateDefaults = desiredConf.validateDefaultValues();
    }
    SchemaParseConfiguration configUsed = new SchemaParseConfiguration(validateNames, validateDefaults);
    parser.setValidate(validateNames);
    // must check the method exists before trying to call it
    if (setValidateDefaultsMethod != null) {
        parser.setValidateDefaults(validateDefaults);
    }
    if (known != null && !known.isEmpty()) {
        Map<String, Schema> knownByFullName = new HashMap<>(known.size());
        for (Schema s : known) {
            knownByFullName.put(s.getFullName(), s);
        }
        parser.addTypes(knownByFullName);
    }
    Schema mainSchema = parser.parse(schemaJson);
    if (validateDefaults && setValidateDefaultsMethod == null) {
        // older avro 1.7 doesnt support validating default values, so we have to do it ourselves
        SchemaValidator validator = new SchemaValidator(configUsed, known);
        // will throw on issues
        AvroSchemaUtil.traverseSchema(mainSchema, validator);
    }
    Map<String, Schema> knownByFullName = parser.getTypes();
    return new SchemaParseResult(mainSchema, knownByFullName, configUsed);
}
Also used : SchemaParseResult(com.linkedin.avroutil1.compatibility.SchemaParseResult) SchemaParseConfiguration(com.linkedin.avroutil1.compatibility.SchemaParseConfiguration) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) SchemaValidator(com.linkedin.avroutil1.compatibility.SchemaValidator)

Example 4 with SchemaValidator

use of com.linkedin.avroutil1.compatibility.SchemaValidator in project avro-util by linkedin.

the class Avro14Adapter method parse.

@Override
public SchemaParseResult parse(String schemaJson, SchemaParseConfiguration desiredConf, Collection<Schema> known) {
    SchemaParseResult result = Avro14SchemaAccessUtil.parse(schemaJson, known);
    if (desiredConf != null && !desiredConf.equals(result.getConfigUsed())) {
        // avro 1.4 doesnt validate anything, so if user wants anything stricter we have to do it ourselves
        Schema schema = result.getMainSchema();
        SchemaValidator validator = new SchemaValidator(desiredConf, known);
        // will throw on issues
        AvroSchemaUtil.traverseSchema(schema, validator);
        return new SchemaParseResult(result.getMainSchema(), result.getAllSchemas(), desiredConf);
    } else {
        return result;
    }
}
Also used : SchemaParseResult(com.linkedin.avroutil1.compatibility.SchemaParseResult) Schema(org.apache.avro.Schema) SchemaValidator(com.linkedin.avroutil1.compatibility.SchemaValidator)

Aggregations

SchemaParseResult (com.linkedin.avroutil1.compatibility.SchemaParseResult)4 SchemaValidator (com.linkedin.avroutil1.compatibility.SchemaValidator)4 Schema (org.apache.avro.Schema)4 SchemaParseConfiguration (com.linkedin.avroutil1.compatibility.SchemaParseConfiguration)3 HashMap (java.util.HashMap)3