public boolean compare()

in gobblin-data-management/src/main/java/org/apache/gobblin/util/schema_check/AvroSchemaCheckDefaultStrategy.java [36:121]


  public boolean compare(Schema expected, Schema toValidate)
  {
    if (toValidate.getType() != expected.getType() || !toValidate.getName().equals(expected.getName())) {return false;}
    else {
      switch (toValidate.getType()) {
        case NULL:
        case BOOLEAN:
        case INT:
        case LONG:
        case FLOAT:
        case DOUBLE:
        case BYTES:
        case STRING: {
          return true;
        }
        case ARRAY: {
          return compare(toValidate.getElementType(), expected.getElementType());
        }
        case MAP: {
          return compare(toValidate.getValueType(), expected.getValueType());
        }
        case FIXED: {
          // fixed size and name must match:
          if (toValidate.getFixedSize() != expected.getFixedSize()) {
            return false;
          }
          return true;
        }
        case ENUM: {
          // expected symbols must contain all toValidate symbols:
          final Set<String> expectedSymbols = new HashSet<>(expected.getEnumSymbols());
          final Set<String> toValidateSymbols = new HashSet<String>(toValidate.getEnumSymbols());
          if (expectedSymbols.size() != toValidateSymbols.size()) {
            return false;
          }
          if (!expectedSymbols.containsAll(toValidateSymbols)) {
            return false;
          }
          return true;
        }

        case RECORD: {
          // Check that each field of toValidate schema is in expected schema
          if (toValidate.getFields().size() != expected.getFields().size()) {
            return false;
          }
          for (final Schema.Field expectedFiled : expected.getFields()) {
            final Schema.Field toValidateField = toValidate.getField(expectedFiled.name());
            if (toValidateField == null) {
              // expected field does not correspond to any field in the toValidate record schema
              return false;
            } else {
              if (!compare(toValidateField.schema(), expectedFiled.schema())) {
                return false;
              }
            }
          }
          return true;
        }
        case UNION: {
          // Check existing schema contains all the type in toValidate schema
          if (toValidate.getTypes().size() != expected.getTypes().size()) {
            return false;
          }
          HashSet<Schema> types = new HashSet<Schema>(expected.getTypes());
          for (Schema toValidateType : toValidate.getTypes()) {
            Schema equalSchema = null;
            for (Schema type : types) {
              if (compare(type, toValidateType)) {
                equalSchema = type;
                break;
              }
            }
            if (equalSchema == null) {
              return false;
            }
            types.remove(equalSchema);
          }
          return true;
        }
        default: {
          throw new AvroRuntimeException("Unknown schema type: " + toValidate.getType());
        }
      }
    }
  }