Skip to content

Commit 44a60a9

Browse files
Integrate Go driver examples into automated build process
- Add examples execution to gremlin-go-integration-tests container - Make server URLs configurable via environment variables - Build fails if any example fails to execute - Improve consistency between root-level and glv-level examples
1 parent 2cd2748 commit 44a60a9

File tree

7 files changed

+88
-49
lines changed

7 files changed

+88
-49
lines changed

gremlin-examples/gremlin-go/basic_gremlin.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ import (
2525
"github.com/apache/tinkerpop/gremlin-go/v3/driver"
2626
)
2727

28+
var serverURL = "ws://localhost:8182/gremlin"
29+
var vertexLabel = "person"
30+
2831
func main() {
29-
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection("ws://localhost:8182/gremlin")
32+
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(serverURL)
3033
if err != nil {
3134
fmt.Println(err)
3235
return
@@ -35,9 +38,9 @@ func main() {
3538
g := gremlingo.Traversal_().WithRemote(driverRemoteConnection)
3639

3740
// Basic Gremlin: adding and retrieving data
38-
v1, err := g.AddV("person").Property("name", "marko").Next()
39-
v2, err := g.AddV("person").Property("name", "stephen").Next()
40-
v3, err := g.AddV("person").Property("name", "vadas").Next()
41+
v1, err := g.AddV(vertexLabel).Property("name", "marko").Next()
42+
v2, err := g.AddV(vertexLabel).Property("name", "stephen").Next()
43+
v3, err := g.AddV(vertexLabel).Property("name", "vadas").Next()
4144
v1Vertex, err := v1.GetVertex()
4245
v2Vertex, err := v2.GetVertex()
4346
v3Vertex, err := v3.GetVertex()
@@ -58,12 +61,12 @@ func main() {
5861
}
5962

6063
// Retrieve the data from the "marko" vertex
61-
marko, err := g.V().Has("person", "name", "marko").Values("name").Next()
64+
marko, err := g.V().Has(vertexLabel, "name", "marko").Values("name").Next()
6265
fmt.Println("name:", marko.GetString())
6366

6467
// Find the "marko" vertex and then traverse to the people he "knows" and return their data
65-
peopleMarkoKnows, err := g.V().Has("person", "name", "marko").Out("knows").Values("name").ToList()
68+
peopleMarkoKnows, err := g.V().Has(vertexLabel, "name", "marko").Out("knows").Values("name").ToList()
6669
for _, person := range peopleMarkoKnows {
6770
fmt.Println("marko knows", person.GetString())
6871
}
69-
}
72+
}

gremlin-examples/gremlin-go/connections.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ import (
2525
"github.com/apache/tinkerpop/gremlin-go/v3/driver"
2626
)
2727

28+
var serverURL = "ws://localhost:8182/gremlin"
29+
var vertexLabel = "connection"
30+
2831
func main() {
2932
withRemote()
3033
withConfigs()
3134
}
3235

3336
func withRemote() {
34-
// Creating the connection to the server
35-
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection("ws://localhost:8182/gremlin")
37+
// Creating the connection to the server
38+
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(serverURL)
3639

3740
// Error handling
3841
if err != nil {
@@ -43,22 +46,18 @@ func withRemote() {
4346
// Cleanup
4447
defer driverRemoteConnection.Close()
4548

46-
// Creating the graph traversal
49+
// Creating the graph traversal
4750
g := gremlingo.Traversal_().WithRemote(driverRemoteConnection)
4851

49-
// Drop existing vertices
50-
prom := g.V().Drop().Iterate()
51-
<-prom
52-
53-
// Simple query to verify connection
54-
g.AddV().Iterate()
55-
count, _ := g.V().Count().Next()
56-
fmt.Println("Vertex count:", *count)
52+
// Simple query to verify connection
53+
g.AddV(vertexLabel).Iterate()
54+
count, _ := g.V().HasLabel(vertexLabel).Count().Next()
55+
fmt.Println("Vertex count:", *count)
5756
}
5857

5958
func withConfigs() {
6059
// Connecting to the server with customized configurations
61-
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection("ws://localhost:8182/gremlin",
60+
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(serverURL,
6261
func(settings *gremlingo.DriverRemoteConnectionSettings) {
6362
settings.TraversalSource = "g"
6463
settings.NewConnectionThreshold = 4
@@ -75,7 +74,7 @@ func withConfigs() {
7574
defer driverRemoteConnection.Close()
7675
g := gremlingo.Traversal_().WithRemote(driverRemoteConnection)
7776

78-
g.AddV().Iterate()
79-
count, _ := g.V().Count().Next()
80-
fmt.Println("Vertex count:", *count)
81-
}
77+
g.AddV(vertexLabel).Iterate()
78+
count, _ := g.V().HasLabel(vertexLabel).Count().Next()
79+
fmt.Println("Vertex count:", *count)
80+
}

gremlin-examples/gremlin-go/modern_traversals.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ import (
2828
var __ = gremlingo.T__
2929
var T = gremlingo.T
3030
var P = gremlingo.P
31+
var serverURL = "ws://localhost:8182/gremlin"
3132

3233
func main() {
33-
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection("ws://localhost:8182/gremlin")
34+
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(serverURL)
3435
if err != nil {
3536
fmt.Println(err)
3637
return

gremlin-go/docker-compose.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,16 @@ services:
6060
- RUN_BASIC_AUTH_INTEGRATION_TESTS=true
6161
- GREMLIN_SOCKET_SERVER_URL=ws://gremlin-socket-server-go
6262
- GREMLIN_SOCKET_SERVER_CONFIG_PATH=/go_app/gremlin-socket-server/conf/test-ws-gremlin.yaml
63+
- VERTEX_LABEL=go-example
6364
working_dir: /go_app
6465
command: >
6566
bash -c "go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest
66-
&& go test -v -json ./... -race -covermode=atomic -coverprofile=\"coverage.out\" -coverpkg=./... | gotestfmt"
67+
&& go test -v -json ./... -race -covermode=atomic -coverprofile=\"coverage.out\" -coverpkg=./... | gotestfmt
68+
&& echo 'Running examples...'
69+
&& go run examples/basic_gremlin.go
70+
&& go run examples/connections.go
71+
&& go run examples/modern_traversals.go
72+
&& echo 'All examples completed successfully'"
6773
depends_on:
6874
gremlin-server-test:
6975
condition: service_healthy

gremlin-go/examples/basic_gremlin.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,23 @@ package main
2121

2222
import (
2323
"fmt"
24+
"os"
2425

2526
"github.com/apache/tinkerpop/gremlin-go/v3/driver"
2627
)
2728

29+
var serverURL = getEnv("GREMLIN_SERVER_URL", "ws://localhost:8182/gremlin")
30+
var vertexLabel = getEnv("VERTEX_LABEL", "person")
31+
32+
func getEnv(key, defaultValue string) string {
33+
if value := os.Getenv(key); value != "" {
34+
return value
35+
}
36+
return defaultValue
37+
}
38+
2839
func main() {
29-
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection("ws://localhost:8182/gremlin")
40+
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(serverURL)
3041
if err != nil {
3142
fmt.Println(err)
3243
return
@@ -35,9 +46,9 @@ func main() {
3546
g := gremlingo.Traversal_().WithRemote(driverRemoteConnection)
3647

3748
// Basic Gremlin: adding and retrieving data
38-
v1, err := g.AddV("person").Property("name", "marko").Next()
39-
v2, err := g.AddV("person").Property("name", "stephen").Next()
40-
v3, err := g.AddV("person").Property("name", "vadas").Next()
49+
v1, err := g.AddV(vertexLabel).Property("name", "marko").Next()
50+
v2, err := g.AddV(vertexLabel).Property("name", "stephen").Next()
51+
v3, err := g.AddV(vertexLabel).Property("name", "vadas").Next()
4152
v1Vertex, err := v1.GetVertex()
4253
v2Vertex, err := v2.GetVertex()
4354
v3Vertex, err := v3.GetVertex()
@@ -58,12 +69,12 @@ func main() {
5869
}
5970

6071
// Retrieve the data from the "marko" vertex
61-
marko, err := g.V().Has("person", "name", "marko").Values("name").Next()
72+
marko, err := g.V().Has(vertexLabel, "name", "marko").Values("name").Next()
6273
fmt.Println("name:", marko.GetString())
6374

6475
// Find the "marko" vertex and then traverse to the people he "knows" and return their data
65-
peopleMarkoKnows, err := g.V().Has("person", "name", "marko").Out("knows").Values("name").ToList()
76+
peopleMarkoKnows, err := g.V().Has(vertexLabel, "name", "marko").Out("knows").Values("name").ToList()
6677
for _, person := range peopleMarkoKnows {
6778
fmt.Println("marko knows", person.GetString())
6879
}
69-
}
80+
}

gremlin-go/examples/connections.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,29 @@ package main
2121

2222
import (
2323
"fmt"
24+
"os"
2425

2526
"github.com/apache/tinkerpop/gremlin-go/v3/driver"
2627
)
2728

29+
var serverURL = getEnv("GREMLIN_SERVER_URL", "ws://localhost:8182/gremlin")
30+
var vertexLabel = getEnv("VERTEX_LABEL", "connection")
31+
32+
func getEnv(key, defaultValue string) string {
33+
if value := os.Getenv(key); value != "" {
34+
return value
35+
}
36+
return defaultValue
37+
}
38+
2839
func main() {
2940
withRemote()
3041
withConfigs()
3142
}
3243

3344
func withRemote() {
34-
// Creating the connection to the server
35-
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection("ws://localhost:8182/gremlin")
45+
// Creating the connection to the server
46+
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(serverURL)
3647

3748
// Error handling
3849
if err != nil {
@@ -43,22 +54,18 @@ func withRemote() {
4354
// Cleanup
4455
defer driverRemoteConnection.Close()
4556

46-
// Creating the graph traversal
57+
// Creating the graph traversal
4758
g := gremlingo.Traversal_().WithRemote(driverRemoteConnection)
4859

49-
// Drop existing vertices
50-
prom := g.V().Drop().Iterate()
51-
<-prom
52-
53-
// Simple query to verify connection
54-
g.AddV().Iterate()
55-
count, _ := g.V().Count().Next()
56-
fmt.Println("Vertex count:", *count)
60+
// Simple query to verify connection
61+
g.AddV(vertexLabel).Iterate()
62+
count, _ := g.V().HasLabel(vertexLabel).Count().Next()
63+
fmt.Println("Vertex count:", *count)
5764
}
5865

5966
func withConfigs() {
6067
// Connecting to the server with customized configurations
61-
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection("ws://localhost:8182/gremlin",
68+
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(serverURL,
6269
func(settings *gremlingo.DriverRemoteConnectionSettings) {
6370
settings.TraversalSource = "g"
6471
settings.NewConnectionThreshold = 4
@@ -75,7 +82,7 @@ func withConfigs() {
7582
defer driverRemoteConnection.Close()
7683
g := gremlingo.Traversal_().WithRemote(driverRemoteConnection)
7784

78-
g.AddV().Iterate()
79-
count, _ := g.V().Count().Next()
80-
fmt.Println("Vertex count:", *count)
81-
}
85+
g.AddV(vertexLabel).Iterate()
86+
count, _ := g.V().HasLabel(vertexLabel).Count().Next()
87+
fmt.Println("Vertex count:", *count)
88+
}

gremlin-go/examples/modern_traversals.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,28 @@ package main
2121

2222
import (
2323
"fmt"
24+
"os"
2425

2526
"github.com/apache/tinkerpop/gremlin-go/v3/driver"
2627
)
2728

2829
var __ = gremlingo.T__
2930
var T = gremlingo.T
3031
var P = gremlingo.P
32+
var serverURL = getEnv("GREMLIN_SERVER_URL", "ws://localhost:8182/gremlin")
33+
34+
func getEnv(key, defaultValue string) string {
35+
if value := os.Getenv(key); value != "" {
36+
return value
37+
}
38+
return defaultValue
39+
}
3140

3241
func main() {
33-
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection("ws://localhost:8182/gremlin")
42+
driverRemoteConnection, err := gremlingo.NewDriverRemoteConnection(serverURL,
43+
func(settings *gremlingo.DriverRemoteConnectionSettings) {
44+
settings.TraversalSource = "gmodern"
45+
})
3446
if err != nil {
3547
fmt.Println(err)
3648
return

0 commit comments

Comments
 (0)