MapExpression.swift 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2025 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /// An expression that represents a map (or dictionary) of key-value pairs.
  15. ///
  16. /// `MapExpression` is used to construct a map from a dictionary of `String` keys
  17. /// and `Sendable` values. The values can be literals (like numbers and strings)
  18. /// or other `Expression` instances, allowing for the creation of dynamic nested
  19. /// objects within a pipeline.
  20. ///
  21. /// Example:
  22. /// ```swift
  23. /// MapExpression([
  24. /// "genre": Field("genre"),
  25. /// "rating": Field("rating").multiply(10),
  26. /// "nestedArray": ArrayExpression([Field("title")]),
  27. /// "nestedMap": MapExpression(["published": Field("published")]),
  28. /// ]).as("metadata")
  29. /// ```
  30. public class MapExpression: FunctionExpression, @unchecked Sendable {
  31. var result: [Expression] = []
  32. public init(_ elements: [String: Sendable]) {
  33. for element in elements {
  34. result.append(Constant(element.key))
  35. result.append(Helper.sendableToExpr(element.value))
  36. }
  37. super.init("map", result)
  38. }
  39. }