Skip to content

Commit 5a065c1

Browse files
committed
graph, store: Do not panic in Entity::set when given an uninterned key
1 parent 7dc748b commit 5a065c1

File tree

7 files changed

+44
-33
lines changed

7 files changed

+44
-33
lines changed

graph/src/components/store/entity_cache.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ impl EntityCache {
192192
}
193193
None => {
194194
let value = self.schema.id_value(&key)?;
195-
entity.set("id", value);
195+
// unwrap: our AtomPool always has an id in it
196+
entity.set("id", value).unwrap();
196197
}
197198
}
198199

graph/src/data/store/mod.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -777,10 +777,12 @@ impl Entity {
777777
}
778778

779779
/// Convenience method to save having to `.into()` the arguments.
780-
pub fn set(&mut self, name: &str, value: impl Into<Value>) -> Option<Value> {
781-
self.0
782-
.insert(name, value.into())
783-
.expect("key is in AtomPool")
780+
pub fn set(
781+
&mut self,
782+
name: &str,
783+
value: impl Into<Value>,
784+
) -> Result<Option<Value>, InternError> {
785+
self.0.insert(name, value.into())
784786
}
785787

786788
/// Merges an entity update `update` into this entity.
@@ -1036,7 +1038,9 @@ fn entity_validation() {
10361038
}
10371039

10381040
let mut thing = make_thing("t1");
1039-
thing.set("things", Value::from(vec!["thing1", "thing2"]));
1041+
thing
1042+
.set("things", Value::from(vec!["thing1", "thing2"]))
1043+
.unwrap();
10401044
check(thing, "");
10411045

10421046
let thing = make_thing("t2");
@@ -1057,15 +1061,17 @@ fn entity_validation() {
10571061
);
10581062

10591063
let mut thing = make_thing("t5");
1060-
thing.set("name", Value::Int(32));
1064+
thing.set("name", Value::Int(32)).unwrap();
10611065
check(
10621066
thing,
10631067
"Entity Thing[t5]: the value `32` for field `name` must \
10641068
have type String! but has type Int",
10651069
);
10661070

10671071
let mut thing = make_thing("t6");
1068-
thing.set("things", Value::List(vec!["thing1".into(), 17.into()]));
1072+
thing
1073+
.set("things", Value::List(vec!["thing1".into(), 17.into()]))
1074+
.unwrap();
10691075
check(
10701076
thing,
10711077
"Entity Thing[t6]: field `things` is of type [Thing!]!, \
@@ -1078,7 +1084,7 @@ fn entity_validation() {
10781084
check(thing, "");
10791085

10801086
let mut thing = make_thing("t8");
1081-
thing.set("cruft", "wat");
1087+
thing.set("cruft", "wat").unwrap();
10821088
check(
10831089
thing,
10841090
"Entity Thing[t8]: field `cruft` is derived and can not be set",

graph/src/schema/ast.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,9 @@ fn entity_validation() {
457457
}
458458

459459
let mut thing = make_thing("t1");
460-
thing.set("things", store::Value::from(vec!["thing1", "thing2"]));
460+
thing
461+
.set("things", store::Value::from(vec!["thing1", "thing2"]))
462+
.unwrap();
461463
check(thing, "");
462464

463465
let thing = make_thing("t2");
@@ -478,18 +480,20 @@ fn entity_validation() {
478480
);
479481

480482
let mut thing = make_thing("t5");
481-
thing.set("name", store::Value::Int(32));
483+
thing.set("name", store::Value::Int(32)).unwrap();
482484
check(
483485
thing,
484486
"Entity Thing[t5]: the value `32` for field `name` must \
485487
have type String! but has type Int",
486488
);
487489

488490
let mut thing = make_thing("t6");
489-
thing.set(
490-
"things",
491-
store::Value::List(vec!["thing1".into(), 17.into()]),
492-
);
491+
thing
492+
.set(
493+
"things",
494+
store::Value::List(vec!["thing1".into(), 17.into()]),
495+
)
496+
.unwrap();
493497
check(
494498
thing,
495499
"Entity Thing[t6]: field `things` is of type [Thing!]!, \
@@ -502,7 +506,7 @@ fn entity_validation() {
502506
check(thing, "");
503507

504508
let mut thing = make_thing("t8");
505-
thing.set("cruft", "wat");
509+
thing.set("cruft", "wat").unwrap();
506510
check(
507511
thing,
508512
"Entity Thing[t8]: field `cruft` is derived and can not be set",

store/test-store/tests/graph/entity_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ fn scoped_get() {
727727
assert_eq!(None, act1);
728728
// But if it gets updated, it becomes visible with either scope
729729
let mut wallet1 = wallet1;
730-
wallet1.set("balance", 70);
730+
wallet1.set("balance", 70).unwrap();
731731
cache.set(key1.clone(), wallet1.clone()).unwrap();
732732
let act1 = cache.get(&key1, GetScope::InBlock).unwrap();
733733
assert_eq!(Some(&wallet1), act1.as_ref());

store/test-store/tests/postgres/graft.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ async fn check_graft(
316316
assert_eq!(Some(&Value::from("queensha@email.com")), shaq.get("email"));
317317

318318
// Make our own entries for block 2
319-
shaq.set("email", "shaq@gmail.com");
319+
shaq.set("email", "shaq@gmail.com").unwrap();
320320
let op = EntityOperation::Set {
321321
key: EntityKey::data(USER.to_owned(), "3"),
322322
data: shaq,

store/test-store/tests/postgres/relational.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,9 @@ fn update() {
580580

581581
// Update with overwrite
582582
let mut entity = SCALAR_ENTITY.clone();
583-
entity.set("string", "updated");
583+
entity.set("string", "updated").unwrap();
584584
entity.remove("strings");
585-
entity.set("bool", Value::Null);
585+
entity.set("bool", Value::Null).unwrap();
586586
let key = EntityKey::data("Scalar".to_owned(), entity.id().unwrap());
587587

588588
let entity_type = EntityType::from("Scalar");
@@ -608,9 +608,9 @@ fn update_many() {
608608
run_test(|conn, layout| {
609609
let mut one = SCALAR_ENTITY.clone();
610610
let mut two = SCALAR_ENTITY.clone();
611-
two.set("id", "two");
611+
two.set("id", "two").unwrap();
612612
let mut three = SCALAR_ENTITY.clone();
613-
three.set("id", "three");
613+
three.set("id", "three").unwrap();
614614
insert_entity(
615615
conn,
616616
layout,
@@ -622,15 +622,15 @@ fn update_many() {
622622
assert_eq!(3, count_scalar_entities(conn, layout));
623623

624624
// update with overwrite
625-
one.set("string", "updated");
625+
one.set("string", "updated").unwrap();
626626
one.remove("strings");
627627

628-
two.set("string", "updated too");
629-
two.set("bool", false);
628+
two.set("string", "updated too").unwrap();
629+
two.set("bool", false).unwrap();
630630

631-
three.set("string", "updated in a different way");
631+
three.set("string", "updated in a different way").unwrap();
632632
three.remove("strings");
633-
three.set("color", "red");
633+
three.set("color", "red").unwrap();
634634

635635
// generate keys
636636
let entity_type = EntityType::from("Scalar");
@@ -708,7 +708,7 @@ fn serialize_bigdecimal() {
708708

709709
for d in &["50", "50.00", "5000", "0.5000", "0.050", "0.5", "0.05"] {
710710
let d = BigDecimal::from_str(d).unwrap();
711-
entity.set("bigDecimal", d);
711+
entity.set("bigDecimal", d).unwrap();
712712

713713
let key = EntityKey::data("Scalar".to_owned(), entity.id().unwrap().clone());
714714
let entity_type = EntityType::from("Scalar");
@@ -757,7 +757,7 @@ fn delete() {
757757
run_test(|conn, layout| {
758758
insert_entity(conn, layout, "Scalar", vec![SCALAR_ENTITY.clone()]);
759759
let mut two = SCALAR_ENTITY.clone();
760-
two.set("id", "two");
760+
two.set("id", "two").unwrap();
761761
insert_entity(conn, layout, "Scalar", vec![two]);
762762

763763
// Delete where nothing is getting deleted
@@ -789,9 +789,9 @@ fn insert_many_and_delete_many() {
789789
run_test(|conn, layout| {
790790
let one = SCALAR_ENTITY.clone();
791791
let mut two = SCALAR_ENTITY.clone();
792-
two.set("id", "two");
792+
two.set("id", "two").unwrap();
793793
let mut three = SCALAR_ENTITY.clone();
794-
three.set("id", "three");
794+
three.set("id", "three").unwrap();
795795
insert_entity(conn, layout, "Scalar", vec![one, two, three]);
796796

797797
// confidence test: there should be 3 scalar entities in store right now

store/test-store/tests/postgres/relational_bytes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ fn update() {
295295

296296
// Update the entity
297297
let mut entity = BEEF_ENTITY.clone();
298-
entity.set("name", "Moo");
298+
entity.set("name", "Moo").unwrap();
299299
let key = EntityKey::data("Thing".to_owned(), entity.id().unwrap());
300300

301301
let entity_id = entity.id().unwrap();
@@ -325,7 +325,7 @@ fn delete() {
325325

326326
insert_entity(conn, layout, "Thing", BEEF_ENTITY.clone());
327327
let mut two = BEEF_ENTITY.clone();
328-
two.set("id", TWO_ID);
328+
two.set("id", TWO_ID).unwrap();
329329
insert_entity(conn, layout, "Thing", two);
330330

331331
// Delete where nothing is getting deleted

0 commit comments

Comments
 (0)