1+ from  collections .abc  import  Hashable 
2+ from  random  import  getrandbits 
13from  typing  import  Any , Dict , Optional 
24
35from  pydantic .config  import  ConfigDict 
46from  pydantic .fields  import  Field 
57from  pydantic .functional_validators  import  model_validator 
68from  pydantic .main  import  BaseModel 
79
10+ from  jsonapi_pydantic .utils  import  _generate_resource_hash 
811from  jsonapi_pydantic .v1_0 .links  import  Links  as  LinksObject 
912from  jsonapi_pydantic .v1_0 .meta  import  Meta  as  MetaObject 
1013from  jsonapi_pydantic .v1_0 .resource .relationship  import  Relationship 
1619Meta  =  Optional [MetaObject ]
1720
1821
19- class  Resource (BaseModel ):
22+ class  Resource (BaseModel ,  Hashable ):
2023    type : str  =  Field (title = "Type" )
2124    id : Id  =  Field (None , title = "Id" )
2225    attributes : Attributes  =  Field (None , title = "Attributes" )
@@ -26,6 +29,21 @@ class Resource(BaseModel):
2629
2730    model_config  =  ConfigDict (frozen = True )
2831
32+     _hash : int 
33+ 
34+     def  __init__ (self , ** data ):
35+         super ().__init__ (** data )
36+         if  self .id :
37+             self ._hash  =  _generate_resource_hash (resource_type = self .type , resource_id = self .id )
38+         else :
39+             self ._hash  =  getrandbits (64 )
40+ 
41+     def  __eq__ (self , __value : object ) ->  bool :
42+         return  self ._hash  ==  getattr (__value , "_hash" , None )
43+ 
44+     def  __hash__ (self ) ->  int :
45+         return  self ._hash 
46+ 
2947    @model_validator (mode = "after" ) 
3048    def  check_all_values (self ) ->  "Resource" :
3149        if  not  self .attributes  and  not  self .relationships :
0 commit comments