Given a name, tries to retrieve the parsed schema from `parsed_schemas`. If a parsed schema is not found, it checks if a currently resolving schema with that name exists. If a resolving schema is not found, it checks if a JSON with that name exists in `input_schemas` and then parses it (removing it from `input_schemas`) and adds the parsed schema to `parsed_schemas`. This method allows schemas d
(
&mut self,
name: &str,
enclosing_namespace: NamespaceRef,
)
| 145 | /// This method allows schemas definitions that depend on other types to |
| 146 | /// parse their dependencies (or look them up if already parsed). |
| 147 | pub(super) fn fetch_schema_ref( |
| 148 | &mut self, |
| 149 | name: &str, |
| 150 | enclosing_namespace: NamespaceRef, |
| 151 | ) -> AvroResult<Schema> { |
| 152 | let fully_qualified_name = Name::new_with_enclosing_namespace(name, enclosing_namespace)?; |
| 153 | |
| 154 | if self.parsed_schemas.contains_key(&fully_qualified_name) { |
| 155 | return Ok(Schema::Ref { |
| 156 | name: fully_qualified_name, |
| 157 | }); |
| 158 | } |
| 159 | if let Some(resolving_schema) = self.resolving_schemas.get(&fully_qualified_name) { |
| 160 | return Ok(resolving_schema.clone()); |
| 161 | } |
| 162 | |
| 163 | // For good error reporting we add this check |
| 164 | match fully_qualified_name.name() { |
| 165 | "record" | "enum" | "fixed" => { |
| 166 | return Err( |
| 167 | Details::InvalidSchemaRecord(fully_qualified_name.name().to_string()).into(), |
| 168 | ); |
| 169 | } |
| 170 | _ => (), |
| 171 | } |
| 172 | |
| 173 | let value = self |
| 174 | .input_schemas |
| 175 | .remove(&fully_qualified_name) |
| 176 | // TODO make a better descriptive error message here that conveys that a named schema cannot be found |
| 177 | .ok_or_else(|| { |
| 178 | let full_name = fully_qualified_name.fullname(None); |
| 179 | if full_name == "bool" { |
| 180 | Details::ParsePrimitiveSimilar(full_name, "boolean") |
| 181 | } else { |
| 182 | Details::ParsePrimitive(full_name) |
| 183 | } |
| 184 | })?; |
| 185 | |
| 186 | // parsing a full schema from inside another schema. Other full schema will not inherit namespace |
| 187 | let full_name = self.get_schema_type_name(fully_qualified_name, &value)?; |
| 188 | let parsed = self.parse(value, None)?; |
| 189 | self.parsed_schemas.insert(full_name.clone(), parsed); |
| 190 | |
| 191 | Ok(Schema::Ref { name: full_name }) |
| 192 | } |
| 193 | |
| 194 | fn parse_precision_and_scale( |
| 195 | &self, |
| 196 | complex: &Map<String, Value>, |
| 197 | ) -> AvroResult<(Precision, Scale)> { |
| 198 | let precision = match complex.get("precision") { |
| 199 | Some(Value::Number(value)) if value.is_u64() => { |
| 200 | let value = value.as_u64().expect("Is u64"); |
| 201 | let value = |
| 202 | usize::try_from(value).map_err(|e| Details::ConvertU64ToUsize(e, value))?; |
| 203 | NonZero::new(value) |
| 204 | .ok_or(Details::DecimalPrecisionMuBePositive { precision: value })? |
no test coverage detected