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

Fix parsing JSON example and add it into action #152

Merged
merged 2 commits into from
Nov 2, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ run-examples:
cd .. && \
go run basic_example/graph_client_basic_example.go && \
go run gorountines_example/graph_client_goroutines_example.go && \
go run json_example/parse_json_example.go && \
cd ./nebula-docker-compose && docker-compose down -v
26 changes: 22 additions & 4 deletions json_example/parse_json_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
address = "127.0.0.1"
// The default port of Nebula Graph 2.x is 9669.
// 3699 is only for testing.
port = 29562
port = 3699
username = "root"
password = "nebula"
)
Expand Down Expand Up @@ -65,8 +65,8 @@ type JsonObj struct {
Comment string `json:"comment "`
} `json:"results"`
Errors []struct {
ErrorCode int `json:"errorCode"`
ErrorMsg string `json:"errorMsg"`
Code int `json:"code"`
Message string `json:"message"`
} `json:"errors"`
}

Expand Down Expand Up @@ -118,7 +118,25 @@ func main() {

// Get a property
birthday := jsonObj.Results[0].Data[0].Row[0].(map[string]interface{})["person.birthday"]
fmt.Printf("person.birthday is %s \n", birthday)
fmt.Printf("person.birthday is %s \n\n", birthday)
}
// With error
{
jsonStrResult, err := session.ExecuteJson("MAT (v:person {name: \"Bob\"}) RETURN v")
if err != nil {
log.Fatal(fmt.Sprintf("fail to get the result in json format, %s", err.Error()))
}

var jsonObj JsonObj
// Parse JSON
json.Unmarshal(jsonStrResult, &jsonObj)
// Get error message
errorMsg := jsonObj.Errors[0].Message
msg, err := json.MarshalIndent(errorMsg, "", " ")
if err != nil {
fmt.Println("error:", err)
}
fmt.Println("Error message: ", string(msg))
}
dropSpace(session, "client_test")

Expand Down