Skip to content

Commit

Permalink
get from a Record
Browse files Browse the repository at this point in the history
  • Loading branch information
splix committed Feb 20, 2025
1 parent 07a6211 commit 1ae15cc
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions avro/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ impl Record<'_> {
self.fields[position].1 = value.into()
}
}

///
/// Get the value for a given field name. Returns `None` if the field is not present in the schema
pub fn get(&self, field: &str) -> Option<&Value> {
self.schema_lookup.get(field)
.map(|&position| &self.fields[position].1)
}
}

impl<'a> From<Record<'a>> for Value {
Expand Down Expand Up @@ -3224,4 +3231,36 @@ Field with name '"b"' is not a member of the map items"#,
}
Ok(())
}

#[test]
fn get_from_record() {
let schema = r#"
{
"type": "record",
"name": "NamespacedMessage",
"namespace": "space",
"fields": [
{
"name": "foo",
"type": "string"
},
{
"name": "bar",
"type": "long"
}
]
}
"#;

let schema = Schema::parse_str(schema).unwrap();
let mut record = Record::new(&schema).unwrap();
record.put("foo", "hello");
record.put("bar", 123_i64);

assert_eq!(record.get("foo").unwrap().clone(), Value::String("hello".to_string()));
assert_eq!(record.get("bar").unwrap().clone(), Value::Long(123));

// also make sure it doesn't fail but return None for non-existing field
assert_eq!(record.get("baz"), None);
}
}

0 comments on commit 1ae15cc

Please sign in to comment.