Skip to content

Commit aa9db72

Browse files
committed
graph: Create an explicit IdType for entity ids
1 parent 8bb3775 commit aa9db72

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

graph/src/data/store/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ impl ValueType {
172172
}
173173
}
174174

175+
/// The types that can be used for the `id` of an entity
176+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
177+
pub enum IdType {
178+
String,
179+
Bytes,
180+
}
181+
175182
// Note: Do not modify fields without also making a backward compatible change to the StableHash impl (below)
176183
/// An attribute value is represented as an enum with variants for all supported value types.
177184
#[derive(Clone, Deserialize, Serialize, PartialEq, Eq)]

graph/src/schema/input_schema.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::{BTreeMap, HashSet};
22
use std::str::FromStr;
33
use std::sync::Arc;
44

5-
use anyhow::{anyhow, Error};
5+
use anyhow::{anyhow, Context, Error};
66
use store::Entity;
77

88
use crate::cheap_clone::CheapClone;
@@ -182,41 +182,44 @@ impl InputSchema {
182182
}
183183
}
184184

185-
/// Construct a value for the entity type's id attribute
186-
pub fn id_value(&self, key: &EntityKey) -> Result<store::Value, Error> {
185+
pub fn id_type(&self, entity_type: &EntityType) -> Result<store::IdType, Error> {
187186
let base_type = self
188187
.inner
189188
.schema
190189
.document
191-
.get_object_type_definition(key.entity_type.as_str())
192-
.ok_or_else(|| {
193-
anyhow!(
194-
"Entity {}[{}]: unknown entity type `{}`",
195-
key.entity_type,
196-
key.entity_id,
197-
key.entity_type
198-
)
199-
})?
190+
.get_object_type_definition(entity_type.as_str())
191+
.ok_or_else(|| anyhow!("unknown entity type `{}`", entity_type))?
200192
.field("id")
201193
.unwrap()
202194
.field_type
203195
.get_base_type();
204196

205197
match base_type {
206-
"ID" | "String" => Ok(store::Value::String(key.entity_id.to_string())),
207-
"Bytes" => Ok(store::Value::Bytes(scalar::Bytes::from_str(
208-
&key.entity_id,
209-
)?)),
198+
"ID" | "String" => Ok(store::IdType::String),
199+
"Bytes" => Ok(store::IdType::Bytes),
210200
s => {
211201
return Err(anyhow!(
212202
"Entity type {} uses illegal type {} for id column",
213-
key.entity_type,
203+
entity_type,
214204
s
215205
))
216206
}
217207
}
218208
}
219209

210+
/// Construct a value for the entity type's id attribute
211+
pub fn id_value(&self, key: &EntityKey) -> Result<store::Value, Error> {
212+
let id_type = self
213+
.id_type(&key.entity_type)
214+
.with_context(|| format!("error determining id_type for {:?}", key))?;
215+
match id_type {
216+
store::IdType::String => Ok(store::Value::String(key.entity_id.to_string())),
217+
store::IdType::Bytes => Ok(store::Value::Bytes(scalar::Bytes::from_str(
218+
&key.entity_id,
219+
)?)),
220+
}
221+
}
222+
220223
pub fn is_immutable(&self, entity_type: &EntityType) -> bool {
221224
self.inner.immutable_types.contains(entity_type)
222225
}

0 commit comments

Comments
 (0)