Skip to content
This repository was archived by the owner on Mar 2, 2026. It is now read-only.

Commit d5f0db0

Browse files
committed
feat: literals pipeline stage
1 parent e16f3be commit d5f0db0

5 files changed

Lines changed: 71 additions & 1 deletion

File tree

google/cloud/firestore_v1/base_pipeline.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,15 @@ def find_nearest(
273273
stages.FindNearest(field, vector, distance_measure, options)
274274
)
275275

276+
def literals(
277+
self,
278+
documents: Selectable,
279+
) -> "_BasePipeline":
280+
"""
281+
TODO: add docstring.
282+
"""
283+
return self._append(stages.Literals(documents))
284+
276285
def replace_with(
277286
self,
278287
field: Selectable,

google/cloud/firestore_v1/pipeline_stages.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,17 @@ def _pb_args(self):
342342
return [Value(integer_value=self.limit)]
343343

344344

345+
class Literals(Stage):
346+
"""TODO: add docstring."""
347+
348+
def __init__(self, documents: Selectable):
349+
super().__init__("literals")
350+
self.documents = Field(documents) if isinstance(documents, str) else documents
351+
352+
def _pb_args(self):
353+
return [self.documents._to_pb()]
354+
355+
345356
class Offset(Stage):
346357
"""Skips a specified number of documents."""
347358

tests/system/pipeline_e2e/general.yaml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,4 +684,27 @@ tests:
684684
- args:
685685
- fieldReferenceValue: awards
686686
- stringValue: full_replace
687-
name: replace_with
687+
name: replace_with
688+
- description: literals
689+
pipeline:
690+
- Literals:
691+
- Constant:
692+
- title: "The Hitchhiker's Guide to the Galaxy"
693+
author: "Douglas Adams"
694+
assert_results:
695+
- title: "The Hitchhiker's Guide to the Galaxy"
696+
author: "Douglas Adams"
697+
assert_proto:
698+
pipeline:
699+
stages:
700+
- args:
701+
- arrayValue:
702+
values:
703+
- mapValue:
704+
fields:
705+
author:
706+
stringValue: "Douglas Adams"
707+
title:
708+
stringValue: "The Hitchhiker's Guide to the Galaxy"
709+
name: literals
710+

tests/unit/v1/test_pipeline.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ def test_pipeline_execute_stream_equivalence():
392392
),
393393
("replace_with", ("name",), stages.ReplaceWith),
394394
("replace_with", (Field.of("n"),), stages.ReplaceWith),
395+
("literals", (Field.of("a"),), stages.Literals),
395396
("sort", (Field.of("n").descending(),), stages.Sort),
396397
("sort", (Field.of("n").descending(), Field.of("m").ascending()), stages.Sort),
397398
("sample", (10,), stages.Sample),

tests/unit/v1/test_pipeline_stages.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,32 @@ def test_to_pb(self):
516516
assert len(result.options) == 0
517517

518518

519+
class TestLiterals:
520+
def _make_one(self, *args, **kwargs):
521+
return stages.Literals(*args, **kwargs)
522+
523+
def test_ctor(self):
524+
val = Constant.of({"a": 1})
525+
instance = self._make_one(val)
526+
assert instance.documents == val
527+
assert instance.name == "literals"
528+
529+
def test_repr(self):
530+
val = Constant.of({"a": 1})
531+
instance = self._make_one(val)
532+
repr_str = repr(instance)
533+
assert repr_str == "Literals(documents=Constant.of({'a': 1}))"
534+
535+
def test_to_pb(self):
536+
val = Constant.of({"a": 1})
537+
instance = self._make_one(val)
538+
result = instance._to_pb()
539+
assert result.name == "literals"
540+
assert len(result.args) == 1
541+
assert result.args[0].map_value.fields["a"].integer_value == 1
542+
assert len(result.options) == 0
543+
544+
519545
class TestOffset:
520546
def _make_one(self, *args, **kwargs):
521547
return stages.Offset(*args, **kwargs)

0 commit comments

Comments
 (0)