dialect/sqlgraph: add builder with functional options for neighbors steps

Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/199

Reviewed By: alexsn

Differential Revision: D18707950

fbshipit-source-id: 77e5308dc4f984306c131d8541cbbd60d82a1488
This commit is contained in:
Ariel Mashraki
2019-11-26 12:22:08 -08:00
committed by Facebook Github Bot
parent 413bbad8d8
commit 6fe565f059
2 changed files with 185 additions and 319 deletions

View File

@@ -67,6 +67,54 @@ type Step struct {
}
}
// StepOption allows configuring Steps using functional options.
type StepOption func(*Step)
// From sets the source of the step.
func From(table, column string, v ...interface{}) StepOption {
return func(s *Step) {
s.From.Table = table
s.From.Column = column
if len(v) > 0 {
s.From.V = v[0]
}
}
}
// To sets the destination of the step.
func To(table, column string) StepOption {
return func(s *Step) {
s.To.Table = table
s.To.Column = column
}
}
// Edge sets the edge info for getting the neighbors.
func Edge(rel Rel, inverse bool, table string, columns ...string) StepOption {
return func(s *Step) {
s.Edge.Rel = rel
s.Edge.Table = table
s.Edge.Columns = columns
s.Edge.Inverse = inverse
}
}
// NewStep gets list of options and returns a configured step.
//
// NewStep(
// From("table", "pk", V),
// To("table", "pk"),
// Edge("name", O2M, "fk"),
// )
//
func NewStep(opts ...StepOption) *Step {
s := &Step{}
for _, opt := range opts {
opt(s)
}
return s
}
// Neighbors returns a Selector for evaluating the path-step
// and getting the neighbors of one vertex.
func Neighbors(dialect string, s *Step) (q *Selector) {