JSON parser

This commit is contained in:
Alexandr Goncharov 2020-06-09 23:41:48 +03:00
parent 86edf18cde
commit d1a7045229
3 changed files with 32 additions and 11 deletions

View File

@ -1,10 +1,12 @@
import Foundation
protocol ConfigurationProvider { protocol ConfigurationProvider {
func configuration() throws -> [Key: String] func configuration() throws -> [Key: String]
} }
final class CommonConfigurationProvider: ConfigurationProvider { final class CommonConfigurationProvider: ConfigurationProvider {
typealias Fetcher = () throws -> String typealias Fetcher = () throws -> Data
typealias Parser = (String) throws -> [String: Any] typealias Parser = (Data) throws -> [String: Any]
init(loader: @escaping Fetcher, parser: @escaping Parser) { init(loader: @escaping Fetcher, parser: @escaping Parser) {
self.load = loader self.load = loader
@ -15,7 +17,7 @@ final class CommonConfigurationProvider: ConfigurationProvider {
let parse: Parser let parse: Parser
func configuration() throws -> [Key: String] { func configuration() throws -> [Key: String] {
let rawData: String let rawData: Data
let parsedData: [String: Any] let parsedData: [String: Any]
do { do {
try rawData = load() try rawData = load()

18
Sources/Conf/Parser.swift Normal file
View File

@ -0,0 +1,18 @@
import Foundation
/// Namespace for the predefined parsers
enum Parser {
static let json: CommonConfigurationProvider.Parser = { data in
let object: Any
do {
object = try JSONSerialization.jsonObject(with: data, options: [])
} catch {
throw ConfigurationError.parse(error)
}
guard let values = object as? [String: Any] else {
struct InvalidJsonFormat: Error { let data: Data }
throw ConfigurationError.parse(InvalidJsonFormat(data: data))
}
return values
}
}

View File

@ -17,7 +17,7 @@ final class CommonConfigurationProviderTests: XCTestCase {
} }
func testParserError() { func testParserError() {
let fetcher: CommonConfigurationProvider.Fetcher = { return "" } let fetcher: CommonConfigurationProvider.Fetcher = { Data() }
let parser: CommonConfigurationProvider.Parser = { _ in throw TestError() } let parser: CommonConfigurationProvider.Parser = { _ in throw TestError() }
let provider = CommonConfigurationProvider(loader: fetcher, parser: parser) let provider = CommonConfigurationProvider(loader: fetcher, parser: parser)
@ -31,7 +31,7 @@ final class CommonConfigurationProviderTests: XCTestCase {
} }
func testDecodeError() { func testDecodeError() {
let fetcher: CommonConfigurationProvider.Fetcher = { return "" } let fetcher: CommonConfigurationProvider.Fetcher = { Data() }
let uuid = UUID() let uuid = UUID()
let parser: CommonConfigurationProvider.Parser = { data in let parser: CommonConfigurationProvider.Parser = { data in
return [ return [
@ -52,9 +52,10 @@ final class CommonConfigurationProviderTests: XCTestCase {
} }
func testDataFlow() throws { func testDataFlow() throws {
let fetcher: CommonConfigurationProvider.Fetcher = { return "string" } let data = "string".data(using: .utf8)!
let parser: CommonConfigurationProvider.Parser = { data in let fetcher: CommonConfigurationProvider.Fetcher = { data }
XCTAssertEqual(data, "string") let parser: CommonConfigurationProvider.Parser = { parserInput in
XCTAssertEqual(parserInput, data)
return [:] return [:]
} }
let provider = CommonConfigurationProvider(loader: fetcher, parser: parser) let provider = CommonConfigurationProvider(loader: fetcher, parser: parser)
@ -63,7 +64,7 @@ final class CommonConfigurationProviderTests: XCTestCase {
} }
func testDecodeValues() throws { func testDecodeValues() throws {
let fetcher: CommonConfigurationProvider.Fetcher = {"" } let fetcher: CommonConfigurationProvider.Fetcher = { Data() }
let parser: CommonConfigurationProvider.Parser = { _ in let parser: CommonConfigurationProvider.Parser = { _ in
return [ "key": 22] return [ "key": 22]
} }
@ -73,7 +74,7 @@ final class CommonConfigurationProviderTests: XCTestCase {
} }
func testDecodeArray() throws { func testDecodeArray() throws {
let fetcher: CommonConfigurationProvider.Fetcher = {"" } let fetcher: CommonConfigurationProvider.Fetcher = { Data() }
let parser: CommonConfigurationProvider.Parser = { _ in let parser: CommonConfigurationProvider.Parser = { _ in
return [ "key": ["one", "two"]] return [ "key": ["one", "two"]]
} }
@ -87,7 +88,7 @@ final class CommonConfigurationProviderTests: XCTestCase {
} }
func testNestedValues() throws { func testNestedValues() throws {
let fetcher: CommonConfigurationProvider.Fetcher = {"" } let fetcher: CommonConfigurationProvider.Fetcher = { Data() }
let parser: CommonConfigurationProvider.Parser = { _ in let parser: CommonConfigurationProvider.Parser = { _ in
return [ return [
"key": [ "nested": "value" ], "key": [ "nested": "value" ],