@@ -2,7 +2,7 @@ use std::collections::{BTreeMap, HashSet};
22use std:: str:: FromStr ;
33use std:: sync:: Arc ;
44
5- use anyhow:: { anyhow, Error } ;
5+ use anyhow:: { anyhow, Context , Error } ;
66use store:: Entity ;
77
88use 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