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

Function pcf_map

avro/src/schema/mod.rs:1076–1156  ·  view source on GitHub ↗
(schema: &Map<String, JsonValue>, defined_names: &mut HashSet<String>)

Source from the content-addressed store, hash-verified

1074 let mut map = fixed.serialize_to_map::<S>(map)?;
1075 map.serialize_entry("logicalType", "duration")?;
1076 map.end()
1077 }
1078 }
1079 }
1080}
1081
1082/// Parses a valid Avro schema into [the Parsing Canonical Form].
1083///
1084/// [the Parsing Canonical Form](https://avro.apache.org/docs/++version++/specification/#parsing-canonical-form-for-schemas)
1085fn parsing_canonical_form(schema: &JsonValue, defined_names: &mut HashSet<String>) -> String {
1086 match schema {
1087 JsonValue::Object(map) => pcf_map(map, defined_names),
1088 JsonValue::String(s) => pcf_string(s),
1089 JsonValue::Array(v) => pcf_array(v, defined_names),
1090 json => panic!("got invalid JSON value for canonical form of schema: {json}"),
1091 }
1092}
1093
1094fn pcf_map(schema: &Map<String, JsonValue>, defined_names: &mut HashSet<String>) -> String {
1095 let typ = schema.get("type").and_then(|v| v.as_str());
1096 let name = if is_named_type(typ) {
1097 let ns = schema.get("namespace").and_then(|v| v.as_str());
1098 let raw_name = schema.get("name").and_then(|v| v.as_str());
1099 Some(format!(
1100 "{}{}",
1101 ns.map_or("".to_string(), |n| { format!("{n}.") }),
1102 raw_name.unwrap_or_default()
1103 ))
1104 } else {
1105 None
1106 };
1107
1108 //if this is already a defined type, early return
1109 if let Some(ref n) = name {
1110 if defined_names.contains(n) {
1111 return pcf_string(n);
1112 } else {
1113 defined_names.insert(n.clone());
1114 }
1115 }
1116
1117 let mut fields = Vec::new();
1118 for (k, v) in schema {
1119 // Reduce primitive types to their simple form. ([PRIMITIVE] rule)
1120 if schema.len() == 1 && k == "type" {
1121 // Invariant: function is only callable from a valid schema, so this is acceptable.
1122 if let JsonValue::String(s) = v {
1123 return pcf_string(s);
1124 }
1125 }
1126
1127 // Strip out unused fields ([STRIP] rule)
1128 if field_ordering_position(k).is_none()
1129 || k == "default"
1130 || k == "doc"
1131 || k == "aliases"
1132 || k == "logicalType"
1133 {

Callers 1

parsing_canonical_formFunction · 0.85

Calls 8

is_named_typeFunction · 0.85
pcf_stringFunction · 0.85
field_ordering_positionFunction · 0.85
getMethod · 0.80
containsMethod · 0.80
cloneMethod · 0.45
lenMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected