@@ -784,4 +784,139 @@ mod tests {
784784 let propagated_model = entity_with_metadata. match_model . unwrap ( ) ;
785785 assert_eq ! ( propagated_model. name( ) , "Game-Player" ) ;
786786 }
787+
788+ #[ test]
789+ fn test_entity_conversion_preserves_nested_struct_values ( ) {
790+ use dojo_types:: schema:: Member ;
791+
792+ let now = Utc :: now ( ) ;
793+
794+ // Create a nested struct similar to TroopGuards
795+ let nested_struct = Ty :: Struct ( Struct {
796+ name : "TroopGuards" . to_string ( ) ,
797+ children : vec ! [
798+ Member {
799+ name: "knight_count" . to_string( ) ,
800+ ty: Ty :: Primitive ( dojo_types:: primitive:: Primitive :: U32 ( Some ( 100 ) ) ) ,
801+ key: false ,
802+ } ,
803+ Member {
804+ name: "crossbowman_count" . to_string( ) ,
805+ ty: Ty :: Primitive ( dojo_types:: primitive:: Primitive :: U32 ( Some ( 50 ) ) ) ,
806+ key: false ,
807+ } ,
808+ Member {
809+ name: "paladin_count" . to_string( ) ,
810+ ty: Ty :: Primitive ( dojo_types:: primitive:: Primitive :: U32 ( Some ( 25 ) ) ) ,
811+ key: false ,
812+ } ,
813+ ] ,
814+ } ) ;
815+
816+ // Create the main model with nested struct
817+ let entity = Entity {
818+ id : "0xworld:0xentity" . to_string ( ) ,
819+ entity_id : "0x123" . to_string ( ) ,
820+ world_address : "0xabc" . to_string ( ) ,
821+ keys : "0x1/0x2" . to_string ( ) ,
822+ event_id : "event_123" . to_string ( ) ,
823+ executed_at : now,
824+ created_at : now,
825+ updated_at : now,
826+ updated_model : Some ( Ty :: Struct ( Struct {
827+ name : "Game-Structure" . to_string ( ) ,
828+ children : vec ! [
829+ Member {
830+ name: "base" . to_string( ) ,
831+ ty: Ty :: Struct ( Struct {
832+ name: "Position" . to_string( ) ,
833+ children: vec![
834+ Member {
835+ name: "coord_x" . to_string( ) ,
836+ ty: Ty :: Primitive ( dojo_types:: primitive:: Primitive :: U32 ( Some (
837+ 10 ,
838+ ) ) ) ,
839+ key: false ,
840+ } ,
841+ Member {
842+ name: "coord_y" . to_string( ) ,
843+ ty: Ty :: Primitive ( dojo_types:: primitive:: Primitive :: U32 ( Some (
844+ 20 ,
845+ ) ) ) ,
846+ key: false ,
847+ } ,
848+ ] ,
849+ } ) ,
850+ key: false ,
851+ } ,
852+ Member {
853+ name: "troop_guards" . to_string( ) ,
854+ ty: nested_struct,
855+ key: false ,
856+ } ,
857+ ] ,
858+ } ) ) ,
859+ deleted : false ,
860+ match_model : None ,
861+ } ;
862+
863+ // Convert to proto Entity
864+ let proto_entity: torii_proto:: schema:: Entity < false > = entity. into ( ) ;
865+
866+ // Verify structure
867+ assert_eq ! ( proto_entity. models. len( ) , 1 ) ;
868+ let model = & proto_entity. models [ 0 ] ;
869+ assert_eq ! ( model. name, "Game-Structure" ) ;
870+ assert_eq ! ( model. children. len( ) , 2 ) ;
871+
872+ // Find the troop_guards member
873+ let troop_guards = model
874+ . children
875+ . iter ( )
876+ . find ( |c| c. name == "troop_guards" )
877+ . expect ( "troop_guards member should exist" ) ;
878+
879+ // Verify nested struct values are preserved
880+ if let Ty :: Struct ( nested) = & troop_guards. ty {
881+ assert_eq ! ( nested. name, "TroopGuards" ) ;
882+ assert_eq ! ( nested. children. len( ) , 3 ) ;
883+
884+ // Verify each primitive value
885+ let knight_count = nested
886+ . children
887+ . iter ( )
888+ . find ( |c| c. name == "knight_count" )
889+ . expect ( "knight_count should exist" ) ;
890+ if let Ty :: Primitive ( dojo_types:: primitive:: Primitive :: U32 ( val) ) = & knight_count. ty {
891+ assert_eq ! ( * val, Some ( 100 ) , "knight_count should be 100, not zero" ) ;
892+ } else {
893+ panic ! ( "knight_count should be U32 primitive" ) ;
894+ }
895+
896+ let crossbowman_count = nested
897+ . children
898+ . iter ( )
899+ . find ( |c| c. name == "crossbowman_count" )
900+ . expect ( "crossbowman_count should exist" ) ;
901+ if let Ty :: Primitive ( dojo_types:: primitive:: Primitive :: U32 ( val) ) = & crossbowman_count. ty
902+ {
903+ assert_eq ! ( * val, Some ( 50 ) , "crossbowman_count should be 50, not zero" ) ;
904+ } else {
905+ panic ! ( "crossbowman_count should be U32 primitive" ) ;
906+ }
907+
908+ let paladin_count = nested
909+ . children
910+ . iter ( )
911+ . find ( |c| c. name == "paladin_count" )
912+ . expect ( "paladin_count should exist" ) ;
913+ if let Ty :: Primitive ( dojo_types:: primitive:: Primitive :: U32 ( val) ) = & paladin_count. ty {
914+ assert_eq ! ( * val, Some ( 25 ) , "paladin_count should be 25, not zero" ) ;
915+ } else {
916+ panic ! ( "paladin_count should be U32 primitive" ) ;
917+ }
918+ } else {
919+ panic ! ( "troop_guards should be a Struct type" ) ;
920+ }
921+ }
787922}
0 commit comments