Skip to content

Commit

Permalink
feat: Add PropsExpr API
Browse files Browse the repository at this point in the history
  • Loading branch information
rlch committed Sep 7, 2023
1 parent 366bf97 commit ead28ff
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 11 deletions.
12 changes: 12 additions & 0 deletions db/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,18 @@ func ExampleProps() {
// ({name: p.name})
}

func ExamplePropsExpr() {
var p tests.Person
c().
Create(
Node(Qual(&p, "p", PropsExpr("$someVar"))).
To(Var("e", PropsExpr("$anotherVar")), nil),
).
Print()
// Output:
// CREATE (p:Person $someVar)-[e $anotherVar]->()
}

func ExampleWhere() {
c().
Match(Node("n")).
Expand Down
10 changes: 10 additions & 0 deletions db/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,13 @@ func VarLength(varLengthExpr internal.Expr) internal.VariableOption {
// - Keys behave as [pkg/github.com/rlch/neogo/client.PropertyIdentifier]'s
// - Values behave as [pkg/github.com/rlch/neogo/client.ValueIdentifier]'s
type Props = internal.Props

// PropsExpr sets the properties of a node or relationship to the provided
// expression.
func PropsExpr(propsExpr internal.Expr) internal.VariableOption {
return &internal.Configurer{
Variable: func(v *internal.Variable) {
v.PropsExpr = propsExpr
},
}
}
50 changes: 40 additions & 10 deletions internal/cypher.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
errWhereReturnSubclause = errors.New("WHERE clause in RETURN sub-clause is not allowed")
errInvalidPropExpr = errors.New("invalid property expression. Property expressions must be strings or an identifier")
errSubqueryImportAlias = errors.New("aliasing or expressions are not supported in importing WITH clauses")
errUnresolvedProps = errors.New("resolving from multiple properties is not allowed")
)

func (s *cypher) catch(op func()) {
Expand Down Expand Up @@ -88,17 +89,33 @@ func (cy *cypher) writeNode(m *member) {
padProps = true
fmt.Fprintf(cy, ":%s", strings.Join(nodeLabels, ":"))
}
if m.variable != nil && m.variable.Props != nil {
if padProps {
cy.WriteRune(' ')
var resolvedProps int
if m.variable != nil {
if m.variable.Props != nil {
resolvedProps++
if padProps {
cy.WriteRune(' ')
}
cy.writeProps(m.variable.Props)
}
cy.writeProps(m.variable.Props)
} else if m.props != "" {
if m.variable.PropsExpr != "" {
resolvedProps++
if padProps {
cy.WriteRune(' ')
}
cy.WriteString(string(m.variable.PropsExpr))
}
}
if m.props != "" {
resolvedProps++
if padProps {
cy.WriteRune(' ')
}
cy.WriteString(m.props)
}
if resolvedProps > 1 {
panic(errUnresolvedProps)
}
if m.where != nil {
cy.WriteRune(' ')
m.where.Identifier = m.identifier
Expand Down Expand Up @@ -142,13 +159,26 @@ func (cy *cypher) writeRelationship(m *member, rs *relationshipPattern) {
if m.variable != nil && m.variable.VarLength != "" {
inner = inner + string(m.variable.VarLength)
}
if m.variable != nil && m.variable.Props != nil {
inner = inner + " " + cy.writeToString(func(cy *cypher) {
cy.writeProps(m.variable.Props)
})
} else if m.props != "" {
var resolvedProps int
if m.variable != nil {
if m.variable.Props != nil {
resolvedProps++
inner = inner + " " + cy.writeToString(func(cy *cypher) {
cy.writeProps(m.variable.Props)
})
}
if m.variable.PropsExpr != "" {
resolvedProps++
inner = inner + " " + string(m.variable.PropsExpr)
}
}
if m.props != "" {
resolvedProps++
inner = inner + " " + m.props
}
if resolvedProps > 1 {
panic(errUnresolvedProps)
}
if m.where != nil {
m.where.Identifier = m.identifier
prevBuilder := cy.Builder
Expand Down
3 changes: 2 additions & 1 deletion internal/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func ConfigureWhere(w *Where, configurer WhereOption) {
}

type Configurer struct {
Merge func(*Merge)
Merge func(*Merge)
Variable func(*Variable)
ProjectionBody func(*ProjectionBody)
Where func(*Where)
Expand Down Expand Up @@ -68,6 +68,7 @@ type (
Expr Expr
Where *Where
Props Props
PropsExpr Expr
Pattern Expr
VarLength Expr
}
Expand Down
3 changes: 3 additions & 0 deletions internal/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ func (s *Scope) unfoldIdentifier(value any) (
if variable.VarLength == "" {
variable.VarLength = v.VarLength
}
if variable.PropsExpr == "" {
variable.PropsExpr = v.PropsExpr
}
}
RecurseToEntity:
for {
Expand Down

0 comments on commit ead28ff

Please sign in to comment.