Skip to content

Commit

Permalink
AVRO-3847: [Rust] Support default value of pre-defined name for Union…
Browse files Browse the repository at this point in the history
… type field (#2468)
  • Loading branch information
sarutak authored Aug 31, 2023
1 parent f3ab785 commit b9cbf9a
Show file tree
Hide file tree
Showing 3 changed files with 645 additions and 15 deletions.
14 changes: 11 additions & 3 deletions lang/rust/avro/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1610,9 +1610,17 @@ impl Parser {
.and_then(|schemas| {
if let Some(default_value) = default.cloned() {
let avro_value = types::Value::from(default_value);
let resolved = schemas
.iter()
.any(|schema| avro_value.to_owned().resolve(schema).is_ok());
let resolved = schemas.iter().any(|schema| {
avro_value
.to_owned()
.resolve_internal(
schema,
&self.parsed_schemas,
&schema.namespace(),
&None,
)
.is_ok()
});

if !resolved {
let schema: Option<&Schema> = schemas.get(0);
Expand Down
25 changes: 13 additions & 12 deletions lang/rust/avro/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ use crate::{
decimal::Decimal,
duration::Duration,
schema::{
DecimalSchema, EnumSchema, FixedSchema, Name, NamesRef, Namespace, Precision, RecordField,
DecimalSchema, EnumSchema, FixedSchema, Name, Namespace, Precision, RecordField,
RecordSchema, ResolvedSchema, Scale, Schema, SchemaKind, UnionSchema,
},
AvroResult, Error,
};
use serde_json::{Number, Value as JsonValue};
use std::{
borrow::Borrow,
collections::{BTreeMap, HashMap},
convert::TryFrom,
fmt::Debug,
Expand Down Expand Up @@ -605,10 +606,10 @@ impl Value {
self.resolve_internal(schema, rs.get_names(), &enclosing_namespace, &None)
}

pub(crate) fn resolve_internal(
pub(crate) fn resolve_internal<S: Borrow<Schema> + Debug>(
mut self,
schema: &Schema,
names: &NamesRef,
names: &HashMap<Name, S>,
enclosing_namespace: &Namespace,
field_default: &Option<JsonValue>,
) -> AvroResult<Self> {
Expand All @@ -630,7 +631,7 @@ impl Value {

if let Some(resolved) = names.get(&name) {
debug!("Resolved {:?}", name);
self.resolve_internal(resolved, names, &name.namespace, field_default)
self.resolve_internal(resolved.borrow(), names, &name.namespace, field_default)
} else {
error!("Failed to resolve schema {:?}", name);
Err(Error::SchemaResolutionError(name.clone()))
Expand Down Expand Up @@ -907,10 +908,10 @@ impl Value {
}
}

fn resolve_union(
fn resolve_union<S: Borrow<Schema> + Debug>(
self,
schema: &UnionSchema,
names: &NamesRef,
names: &HashMap<Name, S>,
enclosing_namespace: &Namespace,
field_default: &Option<JsonValue>,
) -> Result<Self, Error> {
Expand All @@ -930,10 +931,10 @@ impl Value {
))
}

fn resolve_array(
fn resolve_array<S: Borrow<Schema> + Debug>(
self,
schema: &Schema,
names: &NamesRef,
names: &HashMap<Name, S>,
enclosing_namespace: &Namespace,
) -> Result<Self, Error> {
match self {
Expand All @@ -950,10 +951,10 @@ impl Value {
}
}

fn resolve_map(
fn resolve_map<S: Borrow<Schema> + Debug>(
self,
schema: &Schema,
names: &NamesRef,
names: &HashMap<Name, S>,
enclosing_namespace: &Namespace,
) -> Result<Self, Error> {
match self {
Expand All @@ -974,10 +975,10 @@ impl Value {
}
}

fn resolve_record(
fn resolve_record<S: Borrow<Schema> + Debug>(
self,
fields: &[RecordField],
names: &NamesRef,
names: &HashMap<Name, S>,
enclosing_namespace: &Namespace,
) -> Result<Self, Error> {
let mut items = match self {
Expand Down
Loading

0 comments on commit b9cbf9a

Please sign in to comment.