Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResultSet Scan supports more types #309

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions result_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,20 @@ func (res ResultSet) scanRow(row *nebula.Row, colNames []string, t reflect.Type)
rowVal := rowVals[cIdx]

switch f.Type.Kind() {
case reflect.Bool:
val.Field(fIdx).SetBool(rowVal.GetBVal())
case reflect.Int:
val.Field(fIdx).SetInt(rowVal.GetIVal())
case reflect.Int8:
val.Field(fIdx).SetInt(rowVal.GetIVal())
case reflect.Int16:
val.Field(fIdx).SetInt(rowVal.GetIVal())
case reflect.Int32:
val.Field(fIdx).SetInt(rowVal.GetIVal())
case reflect.Int64:
val.Field(fIdx).SetInt(rowVal.GetIVal())
case reflect.Float32:
val.Field(fIdx).SetFloat(rowVal.GetFVal())
case reflect.Float64:
val.Field(fIdx).SetFloat(rowVal.GetFVal())
case reflect.String:
Expand Down
60 changes: 51 additions & 9 deletions result_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ func TestScan(t *testing.T) {
resp := &graph.ExecutionResponse{
ErrorCode: nebula.ErrorCode_SUCCEEDED,
LatencyInUs: 1000,
Data: getDateset(),
Data: getDateset2(),
SpaceName: []byte("test_space"),
ErrorMsg: []byte("test"),
PlanDesc: graph.NewPlanDescription(),
Expand All @@ -800,11 +800,10 @@ func TestScan(t *testing.T) {
}

type testStruct struct {
Col0 int64 `nebula:"col0_int"`
Col1 string `nebula:"col1_string"`
// Col2 Node `nebula:"col2_vertex"`
// Col3 Relationship `nebula:"col3_edge"`
// Col4 PathWrapper `nebula:"col4_path"`
Col0 int64 `nebula:"col0_int64"`
Col1 float64 `nebula:"col1_float64"`
Col2 string `nebula:"col2_string"`
Col3 bool `nebula:"col3_bool"`
}

var testStructList []testStruct
Expand All @@ -814,7 +813,9 @@ func TestScan(t *testing.T) {
}
assert.Equal(t, 1, len(testStructList))
assert.Equal(t, int64(1), testStructList[0].Col0)
assert.Equal(t, "value1", testStructList[0].Col1)
assert.Equal(t, float64(2.0), testStructList[0].Col1)
assert.Equal(t, "string", testStructList[0].Col2)
assert.Equal(t, true, testStructList[0].Col3)

// Scan again should work
err = resultSet.Scan(&testStructList)
Expand All @@ -823,9 +824,13 @@ func TestScan(t *testing.T) {
}
assert.Equal(t, 2, len(testStructList))
assert.Equal(t, int64(1), testStructList[0].Col0)
assert.Equal(t, "value1", testStructList[0].Col1)
assert.Equal(t, float64(2.0), testStructList[0].Col1)
assert.Equal(t, "string", testStructList[0].Col2)
assert.Equal(t, true, testStructList[0].Col3)
assert.Equal(t, int64(1), testStructList[1].Col0)
assert.Equal(t, "value1", testStructList[1].Col1)
assert.Equal(t, float64(2.0), testStructList[1].Col1)
assert.Equal(t, "string", testStructList[1].Col2)
assert.Equal(t, true, testStructList[1].Col3)
}

func TestIntVid(t *testing.T) {
Expand Down Expand Up @@ -988,6 +993,43 @@ func getDateset() *nebula.DataSet {
}
}

func getDateset2() *nebula.DataSet {
colNames := [][]byte{
[]byte("col0_int64"),
[]byte("col1_float64"),
[]byte("col2_string"),
[]byte("col3_bool"),
}
var v1 = nebula.NewValue()
n1 := new(int64)
*n1 = int64(1)
v1.IVal = n1

var v2 = nebula.NewValue()
f2 := new(float64)
*f2 = float64(2.0)
v2.FVal = f2

var v3 = nebula.NewValue()
v3.SVal = []byte("string")

var v4 = nebula.NewValue()
b4 := new(bool)
*b4 = true
v4.BVal = b4

valueList := []*nebula.Value{v1, v2, v3, v4}
var rows []*nebula.Row
row := &nebula.Row{
Values: valueList,
}
rows = append(rows, row)
return &nebula.DataSet{
ColumnNames: colNames,
Rows: rows,
}
}

func setIVal(ival int) *nebula.Value {
var value = nebula.NewValue()
newNum := new(int64)
Expand Down