Skip to content

Commit e76d2a2

Browse files
committed
add documentation
1 parent a54bd45 commit e76d2a2

File tree

3 files changed

+87
-10
lines changed

3 files changed

+87
-10
lines changed

Firestore/Swift/Source/SwiftAPI/Pipeline/Expressions/Constant.swift

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@
1818
@_exported import FirebaseFirestoreInternal
1919
#endif // SWIFT_PACKAGE
2020

21+
///
22+
/// A `Constant` is an `Expression` that represents a fixed, literal value within a Firestore pipeline.
23+
///
24+
/// `Constant`s are used to introduce literal values into a query, which can be useful for:
25+
/// - Comparing a field to a specific value in a `where` clause.
26+
/// - Adding new fields with fixed values using `addFields`.
27+
/// - Providing literal arguments to functions like `sum` or `average`.
28+
///
29+
/// Example of using a `Constant` to add a new field:
30+
/// ```swift
31+
/// // Add a new field "source" with the value "manual" to each document
32+
/// firestore.pipeline()
33+
/// .collection("entries")
34+
/// .addFields([
35+
/// Constant("manual").as("source")
36+
/// ])
37+
/// ```
2138
public struct Constant: Expression, BridgeWrapper, @unchecked Sendable {
2239
let bridge: ExprBridge
2340

@@ -33,55 +50,78 @@ public struct Constant: Expression, BridgeWrapper, @unchecked Sendable {
3350
}
3451
}
3552

36-
// Initializer for integer
53+
/// Creates a new `Constant` expression from an integer literal.
54+
///
55+
/// - Parameter value: The integer value.
3756
public init(_ value: Int) {
3857
self.init(value as Any)
3958
}
4059

41-
// Initializer for double
60+
/// Creates a new `Constant` expression from a double-precision floating-point literal.
61+
///
62+
/// - Parameter value: The double value.
4263
public init(_ value: Double) {
4364
self.init(value as Any)
4465
}
4566

46-
// Initializer for strings
67+
/// Creates a new `Constant` expression from a string literal.
68+
///
69+
/// - Parameter value: The string value.
4770
public init(_ value: String) {
4871
self.init(value as Any)
4972
}
5073

51-
// Initializer for boolean values
74+
/// Creates a new `Constant` expression from a boolean literal.
75+
///
76+
/// - Parameter value: The boolean value.
5277
public init(_ value: Bool) {
5378
self.init(value as Any)
5479
}
5580

56-
// Initializer for Bytes
81+
/// Creates a new `Constant` expression from a `Data` (bytes) literal.
82+
///
83+
/// - Parameter value: The `Data` value.
5784
public init(_ value: Data) {
5885
self.init(value as Any)
5986
}
6087

61-
// Initializer for GeoPoint values
88+
/// Creates a new `Constant` expression from a `GeoPoint` literal.
89+
///
90+
/// - Parameter value: The `GeoPoint` value.
6291
public init(_ value: GeoPoint) {
6392
self.init(value as Any)
6493
}
6594

66-
// Initializer for Timestamp values
95+
/// Creates a new `Constant` expression from a `Timestamp` literal.
96+
///
97+
/// - Parameter value: The `Timestamp` value.
6798
public init(_ value: Timestamp) {
6899
self.init(value as Any)
69100
}
70101

71-
// Initializer for Date values
102+
/// Creates a new `Constant` expression from a `Date` literal.
103+
///
104+
/// The `Date` will be converted to a `Timestamp` internally.
105+
///
106+
/// - Parameter value: The `Date` value.
72107
public init(_ value: Date) {
73108
self.init(value as Any)
74109
}
75110

76-
// Initializer for DocumentReference
111+
/// Creates a new `Constant` expression from a `DocumentReference` literal.
112+
///
113+
/// - Parameter value: The `DocumentReference` value.
77114
public init(_ value: DocumentReference) {
78115
self.init(value as Any)
79116
}
80117

81-
// Initializer for vector values
118+
/// Creates a new `Constant` expression from a `VectorValue` literal.
119+
///
120+
/// - Parameter value: The `VectorValue` value.
82121
public init(_ value: VectorValue) {
83122
self.init(value as Any)
84123
}
85124

125+
/// A `Constant` representing a `nil` value.
86126
public static let `nil` = Constant(nil)
87127
}

Firestore/Swift/Source/SwiftAPI/Pipeline/Expressions/Field.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
///
16+
/// A `Field` is an `Expression` that represents a field in a Firestore document.
17+
///
18+
/// It is a central component for building queries and transformations in Firestore pipelines.
19+
/// A `Field` can be used to:
20+
/// - Reference a document field by its name or `FieldPath`.
21+
/// - Create complex `BooleanExpression`s for filtering in a `where` clause.
22+
/// - Perform mathematical operations on numeric fields.
23+
/// - Manipulate string and array fields.
24+
///
25+
/// Example of creating a `Field` and using it in a `where` clause:
26+
/// ```swift
27+
/// // Reference the "price" field in a document
28+
/// let priceField = Field("price")
29+
///
30+
/// // Create a query to find products where the price is greater than 100
31+
/// firestore.pipeline()
32+
/// .collection("products")
33+
/// .where(priceField.greaterThan(100))
34+
/// ```
1535
public struct Field: Expression, Selectable, BridgeWrapper, SelectableWrapper,
1636
@unchecked Sendable {
1737
let bridge: ExprBridge
@@ -24,13 +44,15 @@ public struct Field: Expression, Selectable, BridgeWrapper, SelectableWrapper,
2444

2545
public let fieldName: String
2646

47+
/// Creates a new `Field` expression from a field name.
2748
public init(_ name: String) {
2849
let fieldBridge = FieldBridge(name: name)
2950
bridge = fieldBridge
3051
fieldName = fieldBridge.field_name()
3152
alias = fieldName
3253
}
3354

55+
/// Creates a new `Field` expression from a `FieldPath`.
3456
public init(_ path: FieldPath) {
3557
let fieldBridge = FieldBridge(path: path)
3658
bridge = fieldBridge

Firestore/Swift/Source/SwiftAPI/Pipeline/Expressions/FunctionExpressions/RandomExpression.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,22 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
///
16+
/// A `RandomExpression` is a `FunctionExpression` that generates a random floating-point
17+
/// number between 0.0 (inclusive) and 1.0 (exclusive).
18+
///
19+
/// This expression is useful when you need to introduce a random value into a pipeline,
20+
/// for example, to randomly sample a subset of documents.
21+
///
22+
/// Example of using `RandomExpression` to sample documents:
23+
/// ```swift
24+
/// // Create a query to sample approximately 10% of the documents in a collection
25+
/// firestore.pipeline()
26+
/// .collection("users")
27+
/// .where(RandomExpression().lessThan(0.1))
28+
/// ```
1529
public class RandomExpression: FunctionExpression, @unchecked Sendable {
30+
/// Creates a new `RandomExpression` that generates a random number.
1631
public init() {
1732
super.init("rand", [])
1833
}

0 commit comments

Comments
 (0)