Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions firestore/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ func (p *Pipeline) Offset(offset int) *Pipeline {
//
// client.Pipeline().Collection("users").Select("info.email")
// client.Pipeline().Collection("users").Select(FieldOf("info.email"))
// client.Pipeline().Collection("users").Select(FieldOfPath([]string{"info", "email"}))
// client.Pipeline().Collection("users").Select(FieldOfPath([]string{"info", "email"}))
// client.Pipeline().Collection("users").Select(FieldOf([]string{"info", "email"}))
// client.Pipeline().Collection("users").Select(FieldOf([]string{"info", "email"}))
// client.Pipeline().Collection("users").Select(Add("age", 5).As("agePlus5"))
func (p *Pipeline) Select(fieldpathsOrSelectables ...any) *Pipeline {
if p.err != nil {
Expand Down Expand Up @@ -341,7 +341,7 @@ func (p *Pipeline) UnnestWithAlias(fieldpath any, alias string, opts *UnnestOpti
case string:
fieldExpr = FieldOf(v)
case FieldPath:
fieldExpr = FieldOfPath(v)
fieldExpr = FieldOf(v)
default:
p.err = errInvalidArg(fieldpath, "string", "FieldPath")
return p
Expand Down
2 changes: 1 addition & 1 deletion firestore/pipeline_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func newBaseAggregateFunction(name string, fieldOrExpr any) *baseAggregateFuncti
case string:
valueExpr = FieldOf(value)
case FieldPath:
valueExpr = FieldOfPath(value)
valueExpr = FieldOf(value)
case Expr:
valueExpr = value
default:
Expand Down
20 changes: 11 additions & 9 deletions firestore/pipeline_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,22 @@ type field struct {
}

// FieldOf creates a new field [Expr] from a field path string.
func FieldOf(path string) Expr {
fieldPath, err := parseDotSeparatedString(path)
if err != nil {
return &field{baseExpr: &baseExpr{err: err}}
func FieldOf[T string | FieldPath](path T) Expr {
anyPath := any(path)
var fieldPath FieldPath
var err error
if v, ok := anyPath.(string); ok {
fieldPath, err = parseDotSeparatedString(v)
if err != nil {
return &field{baseExpr: &baseExpr{err: err}}
}
} else {
fieldPath = anyPath.(FieldPath)
}
Comment thread
bhshkh marked this conversation as resolved.
Outdated
return FieldOfPath(fieldPath)
}

// FieldOfPath creates a new field [Expr] for the given [FieldPath].
func FieldOfPath(fieldPath FieldPath) Expr {
if err := fieldPath.validate(); err != nil {
return &field{baseExpr: &baseExpr{err: err}}
}

pbVal := &pb.Value{
ValueType: &pb.Value_FieldReferenceValue{
FieldReferenceValue: fieldPath.toServiceFieldPath(),
Expand Down
4 changes: 2 additions & 2 deletions firestore/pipeline_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,10 +868,10 @@ func aggregateFuncs(t *testing.T) {
want: map[string]interface{}{"sum_a": int64(3)},
},
{
name: "Sum - FieldOfPath Expr",
name: "Sum - FieldOf Path Expr",
pipeline: client.Pipeline().
Collection(coll.ID).
Aggregate(Sum(FieldOfPath(FieldPath([]string{"a"}))).As("sum_a")),
Aggregate(Sum(FieldOf(FieldPath([]string{"a"}))).As("sum_a")),
want: map[string]interface{}{"sum_a": int64(3)},
},
{
Expand Down
8 changes: 4 additions & 4 deletions firestore/pipeline_stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func newFindNearestStage(vectorField any, queryVector any, measure PipelineDista
case string:
propertyExpr = FieldOf(v)
case FieldPath:
propertyExpr = FieldOfPath(v)
propertyExpr = FieldOf(v)
case Expr:
propertyExpr = v
default:
Expand Down Expand Up @@ -300,7 +300,7 @@ func newRemoveFieldsStage(fieldpaths ...any) (*removeFieldsStage, error) {
case string:
fields[i] = FieldOf(v)
case FieldPath:
fields[i] = FieldOfPath(v)
fields[i] = FieldOf(v)
default:
return nil, errInvalidArg(fp, "string", "FieldPath")
}
Expand Down Expand Up @@ -332,7 +332,7 @@ func newReplaceStage(fieldOrSelectable any) (*replaceStage, error) {
case string:
expr = FieldOf(v)
case FieldPath:
expr = FieldOfPath(v)
expr = FieldOf(v)
case Selectable:
_, expr = v.getSelectionDetails()
default:
Expand Down Expand Up @@ -463,7 +463,7 @@ func newUnnestStage(fieldExpr Expr, alias string, opts *UnnestOptions) (*unnestS
var indexFieldExpr Expr
switch v := opts.IndexField.(type) {
case FieldPath:
indexFieldExpr = FieldOfPath(v)
indexFieldExpr = FieldOf(v)
case string:
indexFieldExpr = FieldOf(v)
default:
Expand Down
4 changes: 2 additions & 2 deletions firestore/pipeline_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func asFieldExpr(val any) Expr {
case Expr:
return v
case FieldPath:
return FieldOfPath(v)
return FieldOf(v)
case string:
return FieldOf(v)
default:
Expand Down Expand Up @@ -191,7 +191,7 @@ func fieldsOrSelectablesToSelectables(fieldsOrSelectables ...any) ([]Selectable,
}
s = FieldOf(v).(*field)
case FieldPath:
s = FieldOfPath(v).(*field)
s = FieldOf(v).(*field)
case Selectable:
s = v
default:
Expand Down
Loading