Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #130 from supplyon/f/add_fold_to_values
Browse files Browse the repository at this point in the history
Add Fold to returned values
  • Loading branch information
ThomasObenaus authored Feb 28, 2023
2 parents fe2059a + cd58c76 commit da640c3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
38 changes: 38 additions & 0 deletions api/value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package api

import (
"github.com/supplyon/gremcos/interfaces"
)

type value struct {
builders []interfaces.QueryBuilder
}

func (v *value) String() string {
queryString := ""
for _, queryBuilder := range v.builders {
queryString += queryBuilder.String()
}
return queryString
}

func NewValueV(e interfaces.Vertex) interfaces.Value {
queryBuilders := make([]interfaces.QueryBuilder, 0)
queryBuilders = append(queryBuilders, e)

return &value{
builders: queryBuilders,
}
}

// Add can be used to add a custom QueryBuilder
// e.g. g.V().Add(NewSimpleQB(".myCustomCall("%s")",label))
func (v *value) Add(builder interfaces.QueryBuilder) interfaces.Value {
v.builders = append(v.builders, builder)
return v
}

// Fold adds .fold() to the query.
func (v *value) Fold() interfaces.Value {
return v.Add(NewSimpleQB(".fold()"))
}
25 changes: 25 additions & 0 deletions api/value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package api

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFoldVal(t *testing.T) {
// GIVEN
graphName := "mygraph"
g := NewGraph(graphName)
require.NotNil(t, g)
v := g.V()
require.NotNil(t, v)

// WHEN
result := v.ValuesBy("test").Fold()

// THEN
assert.NotNil(t, result)
assert.Equal(t, fmt.Sprintf("%s.V().values(\"test\").fold()", graphName), result.String())
}
10 changes: 7 additions & 3 deletions api/vertex.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (v *vertex) And(traversals ...interfaces.QueryBuilder) interfaces.Vertex {
return v.Add(query)
}

// Not adds .not(<traversal>) to the query.
// Not adds .not(<traversal>) to the query.
func (v *vertex) Not(not interfaces.QueryBuilder) interfaces.Vertex {
return v.Add(NewSimpleQB(".not(%s)", not))
}
Expand All @@ -195,6 +195,7 @@ func (v *vertex) Where(where interfaces.QueryBuilder) interfaces.Vertex {
// e.g. .has("temperature",23.02) or .has("available",true)
// The method can also be used to return vertices that have a certain property.
// Then .has("<prop name>") will be added to the query.
//
// v.Has("prop1")
func (v *vertex) Has(key string, value ...interface{}) interfaces.Vertex {
if len(value) == 0 {
Expand All @@ -216,8 +217,9 @@ func (v *vertex) HasLabel(vertexLabel ...string) interfaces.Vertex {
}

// ValuesBy adds .values("<label>"), e.g. .values("user")
func (v *vertex) ValuesBy(label string) interfaces.QueryBuilder {
return v.Add(NewSimpleQB(".values(\"%s\")", label))
func (v *vertex) ValuesBy(label string) interfaces.Value {
v.Add(NewSimpleQB(".values(\"%s\")", label))
return NewValueV(v)
}

// Values adds .values()
Expand Down Expand Up @@ -324,7 +326,9 @@ func (v *vertex) Property(key, value interface{}) interfaces.Vertex {
}

// toKeyValueString creates a string based on the given key and value as a key/value pair using the following format
//
// (\"key\",\"value\")
//
// Depending on the given type of the value the quotes for the value are omitted.
// e.g. ("temperature",23.02) or ("available",true)
func toKeyValueString(key, value interface{}) (string, error) {
Expand Down
15 changes: 14 additions & 1 deletion interfaces/querybuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ type Graph interface {
E() Edge
}

// Value represents a QueryBuilder that can be used to create
// queries that return values
type Value interface {
QueryBuilder

// Fold adds .fold() to the query.
Fold() Value

// Add can be used to add a custom QueryBuilder
// e.g. g.V().Add(NewSimpleQB(".myCustomCall('%s')",label))
Add(builder QueryBuilder) Value
}

// Vertex represents a QueryBuilder that can be used to create
// queries on vertex level
type Vertex interface {
Expand Down Expand Up @@ -76,7 +89,7 @@ type Vertex interface {
HasId(id string) Vertex

// ValuesBy adds .values('<label>'), e.g. .values('user'), to the query. The query call returns all values of the vertex.
ValuesBy(label string) QueryBuilder
ValuesBy(label string) Value

// Values adds .values(), to the query. The query call returns all values with the given label of the vertex.
Values() QueryBuilder
Expand Down

0 comments on commit da640c3

Please sign in to comment.