Skip to content

Commit

Permalink
Setup for list restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Apr 4, 2024
1 parent d201e20 commit 1359ff8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
31 changes: 29 additions & 2 deletions Sources/Confidence/ConfidenceValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,35 @@ public class ConfidenceValue: Equatable, Encodable {
self.value = .timestamp(timestamp)
}

public init(list: [ConfidenceValue]) {
self.value = .list(list.map { $0.value })
// TODO: Handle heterogeneous types
public init(valueList: [ConfidenceValue]) {
self.value = .list(valueList.map { $0.value })
}

public init(boolList: [Bool]) {
self.value = .list(boolList.map { .boolean($0) })
}

public init(stringList: [String]) {
self.value = .list(stringList.map { .string($0) })
}


public init(integerList: [Int64]) {
self.value = .list(integerList.map { .integer($0) })
}

public init(doubleList: [Double]) {
self.value = .list(doubleList.map { .double($0) })
}


public init(dateComponentList: [DateComponents]) {
self.value = .list(dateComponentList.map { .date($0) })
}

public init(dateList: [Date]) {
self.value = .list(dateList.map { .timestamp($0) })
}

public init(structure: [String: ConfidenceValue]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public enum ConfidenceTypeMapper {
case .date(let value):
return ConfidenceValue(timestamp: value)
case .list(let values):
return ConfidenceValue(list: values.compactMap(convertValue))
return ConfidenceValue(valueList: values.compactMap(convertValue))
case .structure(let values):
return ConfidenceValue(structure: values.compactMapValues(convertValue))
case .null:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ValueConverterTest: XCTestCase {
"int": ConfidenceValue(integer: 3),
"double": ConfidenceValue(double: 4.5),
"date": ConfidenceValue(timestamp: date),
"list": ConfidenceValue(list: [ConfidenceValue(integer: 3), ConfidenceValue(integer: 5)]),
"list": ConfidenceValue(integerList: [3, 5]),
"structure": ConfidenceValue(
structure: [
"field1": ConfidenceValue(string: "test"),
Expand Down
11 changes: 4 additions & 7 deletions Tests/ConfidenceTests/ConfidenceValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ final class ConfidenceConfidenceValueTests: XCTestCase {
}

func testListShouldConvertToList() {
let value = ConfidenceValue(list: [
ConfidenceValue(integer: 3),
ConfidenceValue(integer: 4)
])
let value = ConfidenceValue(integerList: [3, 4])
XCTAssertEqual(value.asList(), [ConfidenceValue(integer: 3), ConfidenceValue(integer: 4)])
}

Expand All @@ -61,7 +58,7 @@ final class ConfidenceConfidenceValueTests: XCTestCase {
}

func testEmptyListAllowed() {
let value = ConfidenceValue(list: [])
let value = ConfidenceValue(integerList: [])
XCTAssertEqual(value.asList(), [])
}

Expand Down Expand Up @@ -94,7 +91,7 @@ final class ConfidenceConfidenceValueTests: XCTestCase {
"date": ConfidenceValue(date: dateComponents),
"double": ConfidenceValue(double: 4.5),
"int": ConfidenceValue(integer: 3),
"list": ConfidenceValue(list: [ConfidenceValue(boolean: false), ConfidenceValue(integer: 4)]),
"list": ConfidenceValue(integerList: [3, 5]),
"null": ConfidenceValue(null: ()),
"string": ConfidenceValue(string: "value"),
"structure": ConfidenceValue(structure: ["int": ConfidenceValue(integer: 5)]),
Expand All @@ -108,7 +105,7 @@ final class ConfidenceConfidenceValueTests: XCTestCase {
\"date\":\"03-04-2024\",
\"double\":4.5,
\"int\":3,
\"list\":[false,4],
\"list\":[3,5],
\"null\":null,
\"string\":\"value\",
\"structure\":{\"int\":5},
Expand Down

0 comments on commit 1359ff8

Please sign in to comment.