|
| 1 | +@resultBuilder |
| 2 | +public struct JSONValueBuilder { |
| 3 | + public static func buildExpression(_ expression: JSONValueRepresentable?) -> JSONValueRepresentable { |
| 4 | + expression ?? JSONNullValue() |
| 5 | + } |
| 6 | + |
| 7 | + public static func buildBlock(_ expression: JSONValueRepresentable...) -> JSONValueRepresentable { |
| 8 | + if expression.count == 1 { |
| 9 | + expression[0] |
| 10 | + } else { |
| 11 | + JSONArrayValue(elements: expression) |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + // MARK: Type hints |
| 16 | + |
| 17 | + public static func buildExpression(_ expression: String) -> JSONStringValue { |
| 18 | + JSONStringValue(string: expression) |
| 19 | + } |
| 20 | + |
| 21 | + public static func buildExpression(_ expression: Int) -> JSONIntegerValue { |
| 22 | + JSONIntegerValue(integer: expression) |
| 23 | + } |
| 24 | + |
| 25 | + public static func buildExpression(_ expression: Double) -> JSONNumberValue { |
| 26 | + JSONNumberValue(number: expression) |
| 27 | + } |
| 28 | + |
| 29 | + public static func buildExpression(_ expression: Bool) -> JSONBooleanValue { |
| 30 | + JSONBooleanValue(boolean: expression) |
| 31 | + } |
| 32 | + |
| 33 | + public static func buildExpression(_ expression: [JSONValueRepresentable]) -> JSONArrayValue { |
| 34 | + JSONArrayValue(elements: expression) |
| 35 | + } |
| 36 | + |
| 37 | + public static func buildExpression(_ expression: [String: JSONValueRepresentable]) -> JSONObjectValue { |
| 38 | + JSONObjectValue(properties: expression) |
| 39 | + } |
| 40 | + |
| 41 | + // MARK: Additional array type hints |
| 42 | + |
| 43 | + public static func buildExpression(_ expression: [String]) -> JSONArrayValue { |
| 44 | + JSONArrayValue(elements: expression.map { JSONStringValue(string: $0) }) |
| 45 | + } |
| 46 | + |
| 47 | + public static func buildExpression(_ expression: [Int]) -> JSONArrayValue { |
| 48 | + JSONArrayValue(elements: expression.map { JSONIntegerValue(integer: $0) }) |
| 49 | + } |
| 50 | + |
| 51 | + public static func buildExpression(_ expression: [Double]) -> JSONArrayValue { |
| 52 | + JSONArrayValue(elements: expression.map { JSONNumberValue(number: $0) }) |
| 53 | + } |
| 54 | + |
| 55 | + public static func buildExpression(_ expression: [Bool]) -> JSONArrayValue { |
| 56 | + JSONArrayValue(elements: expression.map { JSONBooleanValue(boolean: $0) }) |
| 57 | + } |
| 58 | + |
| 59 | + public static func buildExpression(_ expression: [JSONValueRepresentable?]) -> JSONArrayValue { |
| 60 | + JSONArrayValue(elements: expression.map { $0 ?? JSONNullValue() }) |
| 61 | + } |
| 62 | + |
| 63 | + // MARK: Addition dictionary type hints |
| 64 | + |
| 65 | + public static func buildExpression(_ expression: [String: Int]) -> JSONObjectValue { |
| 66 | + JSONObjectValue(properties: expression.mapValues { JSONIntegerValue(integer: $0) }) |
| 67 | + } |
| 68 | + |
| 69 | + public static func buildExpression(_ expression: [String: Double]) -> JSONObjectValue { |
| 70 | + JSONObjectValue(properties: expression.mapValues { JSONNumberValue(number: $0) }) |
| 71 | + } |
| 72 | + |
| 73 | + public static func buildExpression(_ expression: [String: Bool]) -> JSONObjectValue { |
| 74 | + JSONObjectValue(properties: expression.mapValues { JSONBooleanValue(boolean: $0) }) |
| 75 | + } |
| 76 | + |
| 77 | + public static func buildExpression(_ expression: [String: String]) -> JSONObjectValue { |
| 78 | + JSONObjectValue(properties: expression.mapValues { JSONStringValue(string: $0) }) |
| 79 | + } |
| 80 | + |
| 81 | + public static func buildExpression(_ expression: [String: JSONValueRepresentable?]) -> JSONObjectValue { |
| 82 | + JSONObjectValue(properties: expression.mapValues { $0 ?? JSONNullValue() }) |
| 83 | + } |
| 84 | + |
| 85 | + // MARK: Advanced builers |
| 86 | + |
| 87 | + public static func buildArray(_ components: [JSONValueRepresentable]) -> JSONValueRepresentable { |
| 88 | + JSONArrayValue(elements: components) |
| 89 | + } |
| 90 | + |
| 91 | + public static func buildOptional(_ component: JSONValueRepresentable?) -> JSONValueRepresentable { |
| 92 | + component ?? JSONNullValue() |
| 93 | + } |
| 94 | + |
| 95 | + public static func buildEither(first: JSONValueRepresentable) -> JSONValueRepresentable { |
| 96 | + first |
| 97 | + } |
| 98 | + |
| 99 | + public static func buildEither(second: JSONValueRepresentable) -> JSONValueRepresentable { |
| 100 | + second |
| 101 | + } |
| 102 | +} |
0 commit comments