MCPcopy Create free account
hub / github.com/apache/avro-rs / parse_enum

Method parse_enum

avro/src/schema/parser.rs:614–688  ·  view source on GitHub ↗

Parse a `serde_json::Value` representing a Avro enum type into a `Schema`.

(
        &mut self,
        complex: &Map<String, Value>,
        enclosing_namespace: NamespaceRef,
    )

Source from the content-addressed store, hash-verified

612
613 let fully_qualified_name = Name::parse(&mut complex, enclosing_namespace)?;
614 let aliases =
615 self.fix_aliases_namespace(complex.aliases()?, fully_qualified_name.namespace())?;
616
617 let symbols = match symbols_opt {
618 Some(Value::Array(array)) => array
619 .into_iter()
620 .map(|v| match v {
621 Value::String(s) => Ok(s),
622 _ => Err(Error::new(Details::GetEnumSymbolsFieldArrayInvalidType(
623 v.description(),
624 ))),
625 })
626 .collect::<Result<Vec<_>, _>>(),
627 Some(value) => Err(Details::GetEnumSymbolsFieldInvalidType(value.description()).into()),
628 None => Err(Details::GetEnumSymbolsField.into()),
629 }?;
630
631 let mut existing_symbols: HashSet<&String> = HashSet::with_capacity(symbols.len());
632 for symbol in symbols.iter() {
633 validate_enum_symbol_name(symbol)?;
634
635 // Ensure there are no duplicate symbols
636 if existing_symbols.contains(&symbol) {
637 return Err(Details::EnumSymbolDuplicate(symbol.to_string()).into());
638 }
639
640 existing_symbols.insert(symbol);
641 }
642
643 let default = match complex.remove("default") {
644 Some(Value::String(s)) => {
645 if !symbols.contains(&s) {
646 return Err(Details::GetEnumDefault { symbol: s, symbols }.into());
647 }
648 Some(s)
649 }
650 Some(v) => return Err(Details::EnumDefaultWrongType(v).into()),
651 None => None,
652 };
653
654 let schema = Schema::Enum(EnumSchema {
655 name: fully_qualified_name.clone(),
656 aliases: aliases.clone(),
657 doc: complex.doc()?,
658 symbols,
659 default,
660 attributes: self.get_custom_attributes(complex),
661 });
662
663 self.register_parsed_schema(&fully_qualified_name, &schema, &aliases);
664
665 Ok(schema)
666 }
667
668 /// Parse a `serde_json::Value` representing a Avro array type into a `Schema`.
669 fn parse_array(
670 &mut self,
671 mut complex: Map<String, Value>,

Callers 1

parse_complexMethod · 0.80

Calls 15

EnumEnum · 0.85
getMethod · 0.80
fix_aliases_namespaceMethod · 0.80
containsMethod · 0.80
resolve_enumMethod · 0.80
get_custom_attributesMethod · 0.80
cloneMethod · 0.45
aliasesMethod · 0.45
namespaceMethod · 0.45

Tested by

no test coverage detected