From 68ea9f85533abf9f32484e0b192b483ee70fea71 Mon Sep 17 00:00:00 2001 From: Xin Hao Date: Tue, 23 Jan 2024 15:33:27 +0800 Subject: [PATCH] Add example code for using ResultSet Scan (#303) Co-authored-by: Anqi --- .../session_pool_example/session_pool_example.go | 12 +++++++++++- result_set.go | 4 ++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/examples/session_pool_example/session_pool_example.go b/examples/session_pool_example/session_pool_example.go index 108b10d1..6a8f6497 100644 --- a/examples/session_pool_example/session_pool_example.go +++ b/examples/session_pool_example/session_pool_example.go @@ -30,6 +30,12 @@ const ( // Initialize logger var log = nebula.DefaultLogger{} +type Person struct { + Name string `nebula:"name"` + Age int `nebula:"age"` + Likeness float64 `nebula:"likeness"` +} + func main() { prepareSpace() hostAddress := nebula.HostAddress{Host: address, Port: port} @@ -94,7 +100,7 @@ func main() { } // Extract data from the resultSet { - query := "GO FROM 'Bob' OVER like YIELD $^.person.name, $^.person.age, like.likeness" + query := "GO FROM 'Bob' OVER like YIELD $^.person.name AS name, $^.person.age AS age, like.likeness AS likeness" // Send query in goroutine wg := sync.WaitGroup{} wg.Add(1) @@ -108,6 +114,10 @@ func main() { return } checkResultSet(query, resultSet) + var personList []Person + resultSet.Scan(&personList) + fmt.Printf("personList: %v\n", personList) + // personList: [{Bob 10 97.2} {Bob 10 80} {Bob 10 70}] }(&wg) wg.Wait() diff --git a/result_set.go b/result_set.go index cc8cd5f6..8b7c942b 100644 --- a/result_set.go +++ b/result_set.go @@ -357,8 +357,12 @@ func (res ResultSet) scanRow(row *nebula.Row, colNames []string, t reflect.Type) rowVal := rowVals[cIdx] switch f.Type.Kind() { + case reflect.Int: + val.Field(fIdx).SetInt(rowVal.GetIVal()) case reflect.Int64: val.Field(fIdx).SetInt(rowVal.GetIVal()) + case reflect.Float64: + val.Field(fIdx).SetFloat(rowVal.GetFVal()) case reflect.String: val.Field(fIdx).SetString(string(rowVal.GetSVal())) default: