Skip to content

Commit

Permalink
AVRO-3927: [Rust] fix array/map deserilaize with custom attribute (#2708
Browse files Browse the repository at this point in the history
)

AVRO-3927: [Rust] fix array/map serilaize with custom attribute

* Rename method arguments: custom_attributes -> attributes

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>

---------

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Co-authored-by: ZENOTME <[email protected]>
Co-authored-by: Martin Tzvetanov Grigorov <[email protected]>
  • Loading branch information
3 people authored Jan 29, 2024
1 parent c27fa40 commit b9927dd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
59 changes: 39 additions & 20 deletions lang/rust/avro/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ pub enum Schema {
#[derive(Clone, Debug, PartialEq)]
pub struct MapSchema {
pub types: Box<Schema>,
pub custom_attributes: BTreeMap<String, Value>,
pub attributes: BTreeMap<String, Value>,
}

#[derive(Clone, Debug, PartialEq)]
pub struct ArraySchema {
pub items: Box<Schema>,
pub custom_attributes: BTreeMap<String, Value>,
pub attributes: BTreeMap<String, Value>,
}

impl PartialEq for Schema {
Expand Down Expand Up @@ -1101,7 +1101,9 @@ impl Schema {
match self {
Schema::Record(RecordSchema { attributes, .. })
| Schema::Enum(EnumSchema { attributes, .. })
| Schema::Fixed(FixedSchema { attributes, .. }) => Some(attributes),
| Schema::Fixed(FixedSchema { attributes, .. })
| Schema::Array(ArraySchema { attributes, .. })
| Schema::Map(MapSchema { attributes, .. }) => Some(attributes),
_ => None,
}
}
Expand Down Expand Up @@ -1146,34 +1148,31 @@ impl Schema {
pub fn map(types: Schema) -> Self {
Schema::Map(MapSchema {
types: Box::new(types),
custom_attributes: Default::default(),
attributes: Default::default(),
})
}

/// Returns a Schema::Map with the given types and custom attributes.
pub fn map_with_attributes(types: Schema, custom_attributes: BTreeMap<String, Value>) -> Self {
pub fn map_with_attributes(types: Schema, attributes: BTreeMap<String, Value>) -> Self {
Schema::Map(MapSchema {
types: Box::new(types),
custom_attributes,
attributes,
})
}

/// Returns a Schema::Array with the given items.
pub fn array(items: Schema) -> Self {
Schema::Array(ArraySchema {
items: Box::new(items),
custom_attributes: Default::default(),
attributes: Default::default(),
})
}

/// Returns a Schema::Array with the given items and custom attributes.
pub fn array_with_attributes(
items: Schema,
custom_attributes: BTreeMap<String, Value>,
) -> Self {
pub fn array_with_attributes(items: Schema, attributes: BTreeMap<String, Value>) -> Self {
Schema::Array(ArraySchema {
items: Box::new(items),
custom_attributes,
attributes,
})
}
}
Expand Down Expand Up @@ -1736,7 +1735,12 @@ impl Parser {
.get("items")
.ok_or(Error::GetArrayItemsField)
.and_then(|items| self.parse(items, enclosing_namespace))
.map(Schema::array)
.map(|items| {
Schema::array_with_attributes(
items,
self.get_custom_attributes(complex, vec!["items"]),
)
})
}

/// Parse a `serde_json::Value` representing a Avro map type into a
Expand All @@ -1750,7 +1754,12 @@ impl Parser {
.get("values")
.ok_or(Error::GetMapValuesField)
.and_then(|items| self.parse(items, enclosing_namespace))
.map(Schema::map)
.map(|items| {
Schema::map_with_attributes(
items,
self.get_custom_attributes(complex, vec!["values"]),
)
})
}

/// Parse a `serde_json::Value` representing a Avro union type into a
Expand Down Expand Up @@ -1860,19 +1869,19 @@ impl Serialize for Schema {
Schema::Bytes => serializer.serialize_str("bytes"),
Schema::String => serializer.serialize_str("string"),
Schema::Array(ref inner) => {
let mut map = serializer.serialize_map(Some(2 + inner.custom_attributes.len()))?;
let mut map = serializer.serialize_map(Some(2 + inner.attributes.len()))?;
map.serialize_entry("type", "array")?;
map.serialize_entry("items", &*inner.items.clone())?;
for attr in &inner.custom_attributes {
for attr in &inner.attributes {
map.serialize_entry(attr.0, attr.1)?;
}
map.end()
}
Schema::Map(ref inner) => {
let mut map = serializer.serialize_map(Some(2 + inner.custom_attributes.len()))?;
let mut map = serializer.serialize_map(Some(2 + inner.attributes.len()))?;
map.serialize_entry("type", "map")?;
map.serialize_entry("values", &*inner.types.clone())?;
for attr in &inner.custom_attributes {
for attr in &inner.attributes {
map.serialize_entry(attr.0, attr.1)?;
}
map.end()
Expand Down Expand Up @@ -6533,7 +6542,12 @@ mod tests {
r#"{"field-id":"1","items":"long","type":"array"}"#,
&serialized
);
assert_eq!(expected, Schema::parse_str(&serialized)?);
let actual_schema = Schema::parse_str(&serialized)?;
assert_eq!(expected, actual_schema);
assert_eq!(
expected.custom_attributes(),
actual_schema.custom_attributes()
);

Ok(())
}
Expand All @@ -6551,7 +6565,12 @@ mod tests {
r#"{"field-id":"1","type":"map","values":"long"}"#,
&serialized
);
assert_eq!(expected, Schema::parse_str(&serialized)?);
let actual_schema = Schema::parse_str(&serialized)?;
assert_eq!(expected, actual_schema);
assert_eq!(
expected.custom_attributes(),
actual_schema.custom_attributes()
);

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion lang/rust/avro/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ mod tests {
Value::Array(vec![Value::Boolean(true)]),
Schema::array(Schema::Long),
false,
"Invalid value: Array([Boolean(true)]) for schema: Array(ArraySchema { items: Long, custom_attributes: {} }). Reason: Unsupported value-schema combination",
"Invalid value: Array([Boolean(true)]) for schema: Array(ArraySchema { items: Long, attributes: {} }). Reason: Unsupported value-schema combination",
),
(Value::Record(vec![]), Schema::Null, false, "Invalid value: Record([]) for schema: Null. Reason: Unsupported value-schema combination"),
(
Expand Down

0 comments on commit b9927dd

Please sign in to comment.