Skip to content

Commit bd75ed6

Browse files
committed
refactor
1 parent 3c8ad91 commit bd75ed6

7 files changed

Lines changed: 41 additions & 35 deletions

firestore/integration_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func TestMain(m *testing.M) {
8181
testParams[firestoreEditionKey] = edition
8282
initIntegrationTest()
8383
status := m.Run()
84-
// cleanupIntegrationTest()
84+
cleanupIntegrationTest()
8585
if status != 0 {
8686
os.Exit(status)
8787
}
@@ -189,20 +189,20 @@ func deleteCollection(ctx context.Context, coll *CollectionRef) error {
189189
bulkwriter := iClient.BulkWriter(ctx)
190190

191191
// Get documents
192-
iter := coll.Select().Documents(ctx)
192+
iter := coll.DocumentRefs(ctx)
193193

194194
// Iterate through the documents, adding
195195
// a delete operation for each one to the BulkWriter.
196196
for {
197-
docSnap, err := iter.Next()
197+
docRef, err := iter.Next()
198198
if err == iterator.Done {
199199
break
200200
}
201201
if err != nil {
202202
log.Printf("Failed to get next document: %+v\n", err)
203203
return err
204204
}
205-
err = deleteDocument(ctx, docSnap.Ref, bulkwriter)
205+
err = deleteDocument(ctx, docRef, bulkwriter)
206206
if err != nil {
207207
log.Printf("Failed to delete document: %+v\n", err)
208208
return err

firestore/pipeline_expression.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,15 @@ type Expression interface {
170170
//
171171
// The parameter 'n' can be an integer constant or an [Expression] that evaluates to an integer.
172172
ArrayLastN(n any) Expression
173-
// ArraySlice creates an expression that returns a slice of an array starting from the specified offset.
173+
// ArraySliceToEnd creates an expression that returns a slice of an array starting from the specified offset.
174174
//
175175
// The parameter 'offset' is the 0-based index of the first element to include. It can be an int, int32, int64 or [Expression].
176-
ArraySlice(offset any) Expression
177-
// ArraySliceToEnd creates an expression that returns a slice of an array starting from the specified offset with a given length.
176+
ArraySliceToEnd(offset any) Expression
177+
// ArraySlice creates an expression that returns a slice of an array starting from the specified offset with a given length.
178178
//
179179
// The parameter 'offset' is the 0-based index of the first element to include. It can be an int, int32, int64 or [Expression].
180180
// The parameter 'length' is the number of elements to include. It can be an int, int32, int64 or [Expression].
181-
ArraySliceToEnd(offset, length any) Expression
181+
ArraySlice(offset, length any) Expression
182182
// ArrayIndexOf creates an expression that returns the first index of a search value in an array.
183183
//
184184
// The parameter 'search' is the value to search for. It can be a constant or [Expression].
@@ -577,18 +577,18 @@ func (b *baseExpression) ArrayReverse() Expression { return Arra
577577
func (b *baseExpression) ArrayConcat(otherArrays ...any) Expression {
578578
return ArrayConcat(b, otherArrays...)
579579
}
580-
func (b *baseExpression) ArraySum() Expression { return ArraySum(b) }
581-
func (b *baseExpression) ArrayMaximum() Expression { return ArrayMaximum(b) }
582-
func (b *baseExpression) ArrayMaximumN(n any) Expression { return ArrayMaximumN(b, n) }
583-
func (b *baseExpression) ArrayMinimum() Expression { return ArrayMinimum(b) }
584-
func (b *baseExpression) ArrayMinimumN(n any) Expression { return ArrayMinimumN(b, n) }
585-
func (b *baseExpression) ArrayFirst() Expression { return ArrayFirst(b) }
586-
func (b *baseExpression) ArrayFirstN(n any) Expression { return ArrayFirstN(b, n) }
587-
func (b *baseExpression) ArrayLast() Expression { return ArrayLast(b) }
588-
func (b *baseExpression) ArrayLastN(n any) Expression { return ArrayLastN(b, n) }
589-
func (b *baseExpression) ArraySlice(offset any) Expression { return ArraySlice(b, offset) }
590-
func (b *baseExpression) ArraySliceToEnd(offset, length any) Expression {
591-
return ArraySliceToEnd(b, offset, length)
580+
func (b *baseExpression) ArraySum() Expression { return ArraySum(b) }
581+
func (b *baseExpression) ArrayMaximum() Expression { return ArrayMaximum(b) }
582+
func (b *baseExpression) ArrayMaximumN(n any) Expression { return ArrayMaximumN(b, n) }
583+
func (b *baseExpression) ArrayMinimum() Expression { return ArrayMinimum(b) }
584+
func (b *baseExpression) ArrayMinimumN(n any) Expression { return ArrayMinimumN(b, n) }
585+
func (b *baseExpression) ArrayFirst() Expression { return ArrayFirst(b) }
586+
func (b *baseExpression) ArrayFirstN(n any) Expression { return ArrayFirstN(b, n) }
587+
func (b *baseExpression) ArrayLast() Expression { return ArrayLast(b) }
588+
func (b *baseExpression) ArrayLastN(n any) Expression { return ArrayLastN(b, n) }
589+
func (b *baseExpression) ArraySliceToEnd(offset any) Expression { return ArraySliceToEnd(b, offset) }
590+
func (b *baseExpression) ArraySlice(offset, length any) Expression {
591+
return ArraySlice(b, offset, length)
592592
}
593593
func (b *baseExpression) ArrayIndexOf(search any) Expression {
594594
return ArrayIndexOf(b, search)

firestore/pipeline_function.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,24 +569,24 @@ func ArrayLastN(exprOrFieldPath any, n any) Expression {
569569
return newBaseFunction("array_last_n", []Expression{asFieldExpr(exprOrFieldPath), asInt64Expr(n)})
570570
}
571571

572-
// ArraySlice creates an expression that returns a slice of an array starting from the specified offset.
572+
// ArraySliceToEnd creates an expression that returns a slice of an array starting from the specified offset.
573573
// - exprOrFieldPath can be a field path string, [FieldPath] or an [Expression] that evaluates to an array.
574574
// - offset is the 0-based index of the first element to include. It can be an int, int32, int64 or [Expression].
575575
//
576576
// Experimental: Firestore Pipelines is currently in preview and is subject to potential breaking changes in future versions,
577577
// regardless of any other documented package stability guarantees.
578-
func ArraySlice(exprOrFieldPath any, offset any) Expression {
578+
func ArraySliceToEnd(exprOrFieldPath any, offset any) Expression {
579579
return newBaseFunction("array_slice", []Expression{asFieldExpr(exprOrFieldPath), asInt64Expr(offset)})
580580
}
581581

582-
// ArraySliceToEnd creates an expression that returns a slice of an array starting from the specified offset with a given length.
582+
// ArraySlice creates an expression that returns a slice of an array starting from the specified offset with a given length.
583583
// - exprOrFieldPath can be a field path string, [FieldPath] or an [Expression] that evaluates to an array.
584584
// - offset is the 0-based index of the first element to include. It can be an int, int32, int64 or [Expression].
585585
// - length is the number of elements to include. It can be an int, int32, int64 or [Expression].
586586
//
587587
// Experimental: Firestore Pipelines is currently in preview and is subject to potential breaking changes in future versions,
588588
// regardless of any other documented package stability guarantees.
589-
func ArraySliceToEnd(exprOrFieldPath any, offset any, length any) Expression {
589+
func ArraySlice(exprOrFieldPath any, offset any, length any) Expression {
590590
return newBaseFunction("array_slice", []Expression{asFieldExpr(exprOrFieldPath), asInt64Expr(offset), asInt64Expr(length)})
591591
}
592592

firestore/pipeline_function_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -969,8 +969,8 @@ func TestArrayFunctions(t *testing.T) {
969969
}},
970970
},
971971
{
972-
desc: "ArraySlice",
973-
expr: ArraySlice("field", 1),
972+
desc: "ArraySliceToEnd",
973+
expr: ArraySliceToEnd("field", 1),
974974
want: &pb.Value{ValueType: &pb.Value_FunctionValue{
975975
FunctionValue: &pb.Function{
976976
Name: "array_slice",
@@ -983,7 +983,7 @@ func TestArrayFunctions(t *testing.T) {
983983
},
984984
{
985985
desc: "ArraySliceWithLength",
986-
expr: ArraySliceToEnd("field", 1, 2),
986+
expr: ArraySlice("field", 1, 2),
987987
want: &pb.Value{ValueType: &pb.Value_FunctionValue{
988988
FunctionValue: &pb.Function{
989989
Name: "array_slice",

firestore/pipeline_integration_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -931,9 +931,6 @@ func aggregationFuncs(t *testing.T) {
931931
docRefs = append(docRefs, docRef)
932932
h.mustCreate(docRef, d)
933933
}
934-
t.Cleanup(func() {
935-
deleteDocuments(docRefs)
936-
})
937934

938935
pipeline := client.Pipeline().Collection(coll.ID).
939936
Sort(Orders(Ascending(FieldOf("val")))).
@@ -1501,13 +1498,13 @@ func arrayFuncs(t *testing.T) {
15011498
want: map[string]interface{}{"last_n": []interface{}{int64(2), int64(3)}},
15021499
},
15031500
{
1504-
name: "ArraySlice",
1505-
pipeline: client.Pipeline().Collection(coll.ID).Select(Fields(ArraySlice("a", 1).As("slice"))),
1501+
name: "ArraySliceToEnd",
1502+
pipeline: client.Pipeline().Collection(coll.ID).Select(Fields(ArraySliceToEnd("a", 1).As("slice"))),
15061503
want: map[string]interface{}{"slice": []interface{}{int64(2), int64(3)}},
15071504
},
15081505
{
15091506
name: "ArraySliceWithLength",
1510-
pipeline: client.Pipeline().Collection(coll.ID).Select(Fields(ArraySliceToEnd("a", 1, 1).As("slice_len"))),
1507+
pipeline: client.Pipeline().Collection(coll.ID).Select(Fields(ArraySlice("a", 1, 1).As("slice_len"))),
15111508
want: map[string]interface{}{"slice_len": []interface{}{int64(2)}},
15121509
},
15131510
// TODO: Uncomment this after fixing the proto representation of this function.

firestore/pipeline_source.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package firestore
1616

17+
import "fmt"
18+
1719
// PipelineSource is a factory for creating Pipeline instances.
1820
// It is obtained by calling [Client.Pipeline].
1921
//
@@ -168,7 +170,14 @@ func (ps *PipelineSource) Documents(refs []*DocumentRef, opts ...DocumentsOption
168170
opt.applyStage(options)
169171
}
170172
}
171-
return newPipeline(ps.client, newInputStageDocuments(refs, options))
173+
p := newPipeline(ps.client, newInputStageDocuments(refs, options))
174+
for _, ref := range refs {
175+
if ref == nil {
176+
p.err = fmt.Errorf("firestore: Documents() cannot contain nil references")
177+
break
178+
}
179+
}
180+
return p
172181
}
173182

174183
// CreateFromQuery creates a new [Pipeline] from the given [Queryer]. Under the hood, this will

firestore/pipeline_stage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (s *inputStageDocuments) toProto() (*pb.Pipeline_Stage, error) {
158158
args := make([]*pb.Value, len(s.refs))
159159
for i, ref := range s.refs {
160160
if ref == nil {
161-
return nil, fmt.Errorf("firestore: internal error: inputStageDocuments contains nil reference")
161+
return nil, fmt.Errorf("firestore: inputStageDocuments contains nil reference")
162162
}
163163
args[i] = &pb.Value{ValueType: &pb.Value_ReferenceValue{ReferenceValue: "/" + ref.shortPath}}
164164
}

0 commit comments

Comments
 (0)