Skip to content

Commit

Permalink
Add example code for using ResultSet Scan (#303)
Browse files Browse the repository at this point in the history
Co-authored-by: Anqi <[email protected]>
  • Loading branch information
haoxins and Nicole00 authored Jan 23, 2024
1 parent d8b37eb commit 68ea9f8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 11 additions & 1 deletion examples/session_pool_example/session_pool_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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)
Expand All @@ -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()

Expand Down
4 changes: 4 additions & 0 deletions result_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 68ea9f8

Please sign in to comment.