Skip to content

Commit

Permalink
HandyDebugPrints for schema and types.
Browse files Browse the repository at this point in the history
  • Loading branch information
markfarnan committed Dec 30, 2022
1 parent 9c77cea commit b3c4d20
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lang/rust/avro/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,11 @@ impl UnionSchema {
/// within this union.
pub fn find_schema(&self, value: &types::Value) -> Option<(usize, &Schema)> {
let schema_kind = SchemaKind::from(value);
// println!("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
// println!("{:?}", self);
// println!("{:?}", value);
// println!("{:?}", schema_kind);
// println!("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
if let Some(&i) = self.variant_index.get(&schema_kind) {
// fast path
Some((i, &self.schemas[i]))
Expand All @@ -709,6 +714,9 @@ impl UnionSchema {
self.schemas.iter().enumerate().find(|(_, schema)| {
let rs =
ResolvedSchema::try_from(*schema).expect("Schema didn't successfully parse");
// println!("{:?}", rs);
// println!("{:?}", schema);

value
.validate_internal(schema, rs.get_names(), &schema.namespace())
.is_none()
Expand Down
119 changes: 118 additions & 1 deletion lang/rust/avro/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ impl Value {
names: &HashMap<Name, S>,
enclosing_namespace: &Namespace,
) -> Option<String> {
// println!("-----------------------------------------------------------------------------------------------------");
// println!("{:?} :-: {:?}", self, schema);
match (self, schema) {
(_, &Schema::Ref { ref name }) => {
let name = name.fully_qualified_name(enclosing_namespace);
Expand Down Expand Up @@ -559,7 +561,13 @@ impl Value {
}
})
}
(_v, _s) => Some("Unsupported value-schema combination".to_string()),
(_v, _s) => {
println!(":-----------------------------------------------------------------------------------------------------");
println!("{:?} :-: {:?}", _v, _s);
println!("Unsupported value-schema combination");
println!(":-----------------------------------------------------------------------------------------------------");
Some("Unsupported value-schema combination".to_string())
}
}
}

Expand Down Expand Up @@ -2647,4 +2655,113 @@ Field with name '"b"' is not a member of the map items"#,
fn test_avro_3688_field_b_set() {
avro_3688_schema_resolution_panic(true);
}

#[test]
fn validate_record_union() {
// {
// "type": "record",
// "fields": [
// {"type": "long", "name": "a"},
// {"type": "string", "name": "b"},
// {
// "type": [ "int", "long"]
// "name": "c",
// "default": null
// }
// ]
// }
let schema = Schema::Record {
name: Name::new("some_record").unwrap(),
aliases: None,
doc: None,
fields: vec![
RecordField {
name: "a".to_string(),
doc: None,
default: None,
schema: Schema::Long,
order: RecordFieldOrder::Ascending,
position: 0,
custom_attributes: Default::default(),
},
RecordField {
name: "b".to_string(),
doc: None,
default: None,
schema: Schema::String,
order: RecordFieldOrder::Ascending,
position: 1,
custom_attributes: Default::default(),
},
RecordField {
name: "c".to_string(),
doc: None,
default: Some(JsonValue::Null),
schema: Schema::Union(
UnionSchema::new(vec![Schema::Int, Schema::Long]).unwrap(),
),
order: RecordFieldOrder::Ascending,
position: 2,
custom_attributes: Default::default(),
},
],
lookup: [
("a".to_string(), 0),
("b".to_string(), 1),
("c".to_string(), 2),
]
.iter()
.cloned()
.collect(),
attributes: Default::default(),
};

// assert!(Value::Map(
// vec![
// ("a".to_string(), Value::Long(42i64)),
// ("b".to_string(), Value::String("foo".to_string())),
// (
// "c".to_string(),
// Value::Union(2, Box::new(Value::Long(42i64)))
// ),
// ]
// .into_iter()
// .collect()
// )
// .validate(&schema));

assert!(Value::Record(vec![
("a".to_string(), Value::Long(42i64)),
("b".to_string(), Value::String("foo".to_string())),
(
"c".to_string(),
Value::Union(2, Box::new(Value::Long(24i64)))
),
])
.validate(&schema));

// assert!(Value::Map(
// vec![
// ("a".to_string(), Value::Long(42i64)),
// ("b".to_string(), Value::String("foo".to_string())),
// (
// "c".to_string(),
// Value::Union(2, Box::new(Value::Long(42i64)))
// ),
// ]
// .into_iter()
// .collect()
// )
// .validate(&schema));

// assert!(Value::Union(
// 1,
// Box::new(Value::Record(vec![
// ("a".to_string(), Value::Long(42i64)),
// ("b".to_string(), Value::String("foo".to_string())),
// ]))
// )

// assert!(Value::Union(2, Box::new(Value::Long(42i64)),).validate(&union_schema));
}
}

0 comments on commit b3c4d20

Please sign in to comment.