@@ -835,6 +835,102 @@ func TestScan(t *testing.T) {
835835 assert .Equal (t , true , testStructList [1 ].Col3 )
836836}
837837
838+ func TestScanWithNestStruct (t * testing.T ) {
839+ resp := & graph.ExecutionResponse {
840+ ErrorCode : nebula .ErrorCode_SUCCEEDED ,
841+ LatencyInUs : 1000 ,
842+ Data : getNestDateset (),
843+ SpaceName : []byte ("test_space" ),
844+ ErrorMsg : []byte ("test" ),
845+ PlanDesc : graph .NewPlanDescription (),
846+ Comment : []byte ("test_comment" )}
847+ resultSet , err := genResultSet (resp , testTimezone )
848+ if err != nil {
849+ t .Error (err )
850+ }
851+
852+ type Person struct {
853+ Name string `nebula:"name"`
854+ City string `nebula:"city"`
855+ }
856+ type Friend struct {
857+ CreatedAt string `nebula:"created_at"`
858+ }
859+ type Result struct {
860+ Nodes []Person `nebula:"nodes"`
861+ Edges []Friend `nebula:"relationships"`
862+ }
863+
864+ var results []Result
865+ err = resultSet .Scan (& results )
866+ if err != nil {
867+ t .Error (err )
868+ }
869+ assert .Equal (t , 1 , len (results ))
870+ assert .Equal (t , "Tom" , results [0 ].Nodes [0 ].Name )
871+ assert .Equal (t , "Shanghai" , results [0 ].Nodes [0 ].City )
872+ assert .Equal (t , "Bob" , results [0 ].Nodes [1 ].Name )
873+ assert .Equal (t , "Hangzhou" , results [0 ].Nodes [1 ].City )
874+ assert .Equal (t , "2024-07-07" , results [0 ].Edges [0 ].CreatedAt )
875+ assert .Equal (t , "2024-07-07" , results [0 ].Edges [1 ].CreatedAt )
876+
877+ // Scan again should work
878+ err = resultSet .Scan (& results )
879+ if err != nil {
880+ t .Error (err )
881+ }
882+ assert .Equal (t , 2 , len (results ))
883+ }
884+
885+ func TestScanWithNestStructPtr (t * testing.T ) {
886+ resp := & graph.ExecutionResponse {
887+ ErrorCode : nebula .ErrorCode_SUCCEEDED ,
888+ LatencyInUs : 1000 ,
889+ Data : getNestDateset (),
890+ SpaceName : []byte ("test_space" ),
891+ ErrorMsg : []byte ("test" ),
892+ PlanDesc : graph .NewPlanDescription (),
893+ Comment : []byte ("test_comment" )}
894+ resultSet , err := genResultSet (resp , testTimezone )
895+ if err != nil {
896+ t .Error (err )
897+ }
898+
899+ type Person struct {
900+ Name string `nebula:"name"`
901+ City string `nebula:"city"`
902+ }
903+ type Friend struct {
904+ CreatedAt string `nebula:"created_at"`
905+ }
906+ type Result struct {
907+ Nodes []* Person `nebula:"nodes"`
908+ Edges []* Friend `nebula:"relationships"`
909+ }
910+
911+ // TODO: actually, the `results` should be []*Result,
912+ // we still need to support this case
913+ var results []Result
914+ err = resultSet .Scan (& results )
915+ if err != nil {
916+ t .Error (err )
917+ }
918+ assert .Equal (t , 1 , len (results ))
919+ assert .Equal (t , "Tom" , results [0 ].Nodes [0 ].Name )
920+ assert .Equal (t , "Shanghai" , results [0 ].Nodes [0 ].City )
921+ assert .Equal (t , "Bob" , results [0 ].Nodes [1 ].Name )
922+ assert .Equal (t , "Hangzhou" , results [0 ].Nodes [1 ].City )
923+ assert .Equal (t , "2024-07-07" , results [0 ].Edges [0 ].CreatedAt )
924+ assert .Equal (t , "2024-07-07" , results [0 ].Edges [1 ].CreatedAt )
925+
926+ // Scan again should work
927+ err = resultSet .Scan (& results )
928+ if err != nil {
929+ t .Error (err )
930+ }
931+ assert .Equal (t , 2 , len (results ))
932+ }
933+
838934func TestIntVid (t * testing.T ) {
839935 vertex := getVertexInt (101 , 3 , 5 )
840936 node , err := genNode (vertex , testTimezone )
@@ -1032,6 +1128,83 @@ func getDateset2() *nebula.DataSet {
10321128 }
10331129}
10341130
1131+ func getNestDateset () * nebula.DataSet {
1132+ colNames := [][]byte {
1133+ []byte ("nodes" ),
1134+ []byte ("relationships" ),
1135+ }
1136+ var list1 = nebula .NewValue ()
1137+ list1 .SetLVal (& nebula.NList {
1138+ Values : []* nebula.Value {
1139+ {
1140+ VVal : & nebula.Vertex {
1141+ Vid : & nebula.Value {SVal : []byte ("person_id_0" )},
1142+ Tags : []* nebula.Tag {
1143+ {
1144+ Name : []byte ("person" ),
1145+ Props : map [string ]* nebula.Value {
1146+ "name" : {SVal : []byte ("Tom" )},
1147+ "city" : {SVal : []byte ("Shanghai" )},
1148+ },
1149+ },
1150+ },
1151+ },
1152+ },
1153+ {
1154+ VVal : & nebula.Vertex {
1155+ Vid : & nebula.Value {SVal : []byte ("person_id_1" )},
1156+ Tags : []* nebula.Tag {
1157+ {
1158+ Name : []byte ("person" ),
1159+ Props : map [string ]* nebula.Value {
1160+ "name" : {SVal : []byte ("Bob" )},
1161+ "city" : {SVal : []byte ("Hangzhou" )},
1162+ },
1163+ },
1164+ },
1165+ },
1166+ },
1167+ },
1168+ })
1169+
1170+ var list2 = nebula .NewValue ()
1171+ list2 .SetLVal (& nebula.NList {
1172+ Values : []* nebula.Value {
1173+ {
1174+ EVal : & nebula.Edge {
1175+ Src : & nebula.Value {SVal : []byte ("person_id_0" )},
1176+ Dst : & nebula.Value {SVal : []byte ("person_id_1" )},
1177+ Name : []byte ("friend" ),
1178+ Props : map [string ]* nebula.Value {
1179+ "created_at" : {SVal : []byte ("2024-07-07" )},
1180+ },
1181+ },
1182+ },
1183+ {
1184+ EVal : & nebula.Edge {
1185+ Src : & nebula.Value {SVal : []byte ("person_id_1" )},
1186+ Dst : & nebula.Value {SVal : []byte ("person_id_0" )},
1187+ Name : []byte ("friend" ),
1188+ Props : map [string ]* nebula.Value {
1189+ "created_at" : {SVal : []byte ("2024-07-07" )},
1190+ },
1191+ },
1192+ },
1193+ },
1194+ })
1195+
1196+ valueList := []* nebula.Value {list1 , list2 }
1197+ var rows []* nebula.Row
1198+ row := & nebula.Row {
1199+ Values : valueList ,
1200+ }
1201+ rows = append (rows , row )
1202+ return & nebula.DataSet {
1203+ ColumnNames : colNames ,
1204+ Rows : rows ,
1205+ }
1206+ }
1207+
10351208func setIVal (ival int ) * nebula.Value {
10361209 var value = nebula .NewValue ()
10371210 newNum := new (int64 )
0 commit comments