-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This struct is applicable everywhere a granularity is expected, such as Insight.groupby and CustomQuery.granularity. It replaces various implementations of the same concept.
- Loading branch information
Showing
5 changed files
with
56 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
public enum QueryGranularity: String, Codable, Hashable, CaseIterable { | ||
case all | ||
case none | ||
case second | ||
case minute | ||
case fifteen_minute | ||
case thirty_minute | ||
case hour | ||
case day | ||
case week | ||
case month | ||
case quarter | ||
case year | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case type | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let type: String | ||
|
||
let singleValueContainer = try decoder.singleValueContainer() | ||
if let singleValueType = try? singleValueContainer.decode(String.self) { | ||
type = singleValueType | ||
} | ||
|
||
else { | ||
let keyedContainer = try decoder.container(keyedBy: CodingKeys.self) | ||
type = try keyedContainer.decode(String.self, forKey: .type) | ||
} | ||
|
||
for possibleCase in Self.allCases { | ||
if type == possibleCase.rawValue { | ||
self = possibleCase | ||
return | ||
} | ||
} | ||
|
||
throw DecodingError.dataCorrupted(.init( | ||
codingPath: [], | ||
debugDescription: "needs to be a string or a dict", | ||
underlyingError: nil | ||
)) | ||
} | ||
} |