@@ -2,7 +2,7 @@ use std::collections::{BTreeMap, HashSet};
2
2
use std:: str:: FromStr ;
3
3
use std:: sync:: Arc ;
4
4
5
- use anyhow:: { anyhow, Error } ;
5
+ use anyhow:: { anyhow, Context , Error } ;
6
6
use store:: Entity ;
7
7
8
8
use crate :: cheap_clone:: CheapClone ;
@@ -182,41 +182,44 @@ impl InputSchema {
182
182
}
183
183
}
184
184
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 > {
187
186
let base_type = self
188
187
. inner
189
188
. schema
190
189
. 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) ) ?
200
192
. field ( "id" )
201
193
. unwrap ( )
202
194
. field_type
203
195
. get_base_type ( ) ;
204
196
205
197
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 ) ,
210
200
s => {
211
201
return Err ( anyhow ! (
212
202
"Entity type {} uses illegal type {} for id column" ,
213
- key . entity_type,
203
+ entity_type,
214
204
s
215
205
) )
216
206
}
217
207
}
218
208
}
219
209
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
+
220
223
pub fn is_immutable ( & self , entity_type : & EntityType ) -> bool {
221
224
self . inner . immutable_types . contains ( entity_type)
222
225
}
0 commit comments