Skip to content

Commit

Permalink
refine some unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
odysa committed Feb 18, 2024
1 parent 30d5c3c commit 46890f5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 33 deletions.
42 changes: 15 additions & 27 deletions crates/iceberg/src/avro/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ impl SchemaVisitor for SchemaToAvroSchema {
type T = AvroSchemaOrField;

fn schema(&mut self, _schema: &Schema, value: AvroSchemaOrField) -> Result<AvroSchemaOrField> {
let mut avro_schema = value.left().ok_or(Error::new(
ErrorKind::Unexpected,
"value expected to be schema",
))?;
// value should never be None
let mut avro_schema = value.unwrap_left();

if let AvroSchema::Record(record) = &mut avro_schema {
record.name = Name::from(self.schema.as_str());
Expand Down Expand Up @@ -242,13 +240,12 @@ pub(crate) fn schema_to_avro_schema(name: impl ToString, schema: &Schema) -> Res
schema: name.to_string(),
};

match visit_schema(schema, &mut converter) {
Ok(s) => Ok(s.left().ok_or(Error::new(
visit_schema(schema, &mut converter).and_then(|s| {
s.left().ok_or(Error::new(
ErrorKind::Unexpected,
"value expected to be schema",
))?),
Err(e) => Err(e),
}
))
})
}

fn avro_record_schema(name: &str, fields: Vec<AvroRecordField>) -> Result<AvroSchema> {
Expand Down Expand Up @@ -390,7 +387,10 @@ impl AvroSchemaVisitor for AvroSchemaToSchema {

let optional = is_avro_optional(&avro_field.schema);

let typ = typ.ok_or(Error::new(ErrorKind::Unexpected, "field type None"))?;
let typ = typ.ok_or(Error::new(
ErrorKind::Unexpected,
"Field type should not be None",
))?;

let mut field = if optional {
NestedField::optional(field_id as i32, &avro_field.name, typ)
Expand Down Expand Up @@ -428,23 +428,17 @@ impl AvroSchemaVisitor for AvroSchemaToSchema {
}

if options.len() == 1 {
Ok(Some(options.remove(0).ok_or(Error::new(
ErrorKind::Unexpected,
"type removed is None",
))?))
Ok(Some(options.remove(0).unwrap()))
} else {
Ok(Some(options.remove(1).ok_or(Error::new(
ErrorKind::Unexpected,
"type removed is None",
))?))
Ok(Some(options.remove(1).unwrap()))
}
}

fn array(&mut self, array: &AvroSchema, item: Option<Type>) -> Result<Self::T> {
if let AvroSchema::Array(item_schema) = array {
let element_field = NestedField::list_element(
self.next_field_id(),
item.ok_or(Error::new(ErrorKind::Unexpected, "item should not be None"))?,
item.unwrap(),
!is_avro_optional(item_schema),
)
.into();
Expand All @@ -467,10 +461,7 @@ impl AvroSchemaVisitor for AvroSchemaToSchema {
);
let value_field = NestedField::map_value_element(
self.next_field_id(),
value.ok_or(Error::new(
ErrorKind::Unexpected,
"valud should not be None",
))?,
value.unwrap(),
!is_avro_optional(value_schema),
);
Ok(Some(Type::Map(MapType {
Expand Down Expand Up @@ -540,10 +531,7 @@ impl AvroSchemaVisitor for AvroSchemaToSchema {
pub(crate) fn avro_schema_to_schema(avro_schema: &AvroSchema) -> Result<Schema> {
if let AvroSchema::Record(_) = avro_schema {
let mut converter = AvroSchemaToSchema { next_id: 0 };
let typ = visit(avro_schema, &mut converter)?.ok_or(Error::new(
ErrorKind::Unexpected,
"Iceberg schema should not be none.",
))?;
let typ = visit(avro_schema, &mut converter)?.expect("Iceberg schema should not be none.");
if let Type::Struct(s) = typ {
Schema::builder()
.with_fields(s.fields().iter().cloned())
Expand Down
7 changes: 3 additions & 4 deletions crates/iceberg/src/spec/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,9 @@ impl SchemaBuilder {

let mut cur_field_id = identifier_field_id;
while let Some(parent) = id_to_parent.get(&cur_field_id) {
let parent_field = id_to_field.get(parent).ok_or(Error::new(
ErrorKind::FeatureUnsupported,
"Field id should not disappear.",
))?;
let parent_field = id_to_field
.get(parent)
.expect("Field id should not disappear.");
ensure_data_valid!(
parent_field.field_type.is_struct(),
"Cannot add field {} as an identifier field: must not be nested in {:?}",
Expand Down
6 changes: 4 additions & 2 deletions crates/iceberg/src/spec/table_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,10 @@ impl TableMetadata {
/// Get current snapshot
#[inline]
pub fn current_snapshot(&self) -> Option<&SnapshotRef> {
self.current_snapshot_id
.and_then(|s| self.snapshot_by_id(s))
self.current_snapshot_id.map(|s| {
self.snapshot_by_id(s)
.expect("Current snapshot id has been set, but doesn't exist in metadata")
})
}

/// Return all sort orders.
Expand Down

0 comments on commit 46890f5

Please sign in to comment.