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 {
func configuration() throws -> [Key: String]
}
final class CommonConfigurationProvider: ConfigurationProvider {
typealias Fetcher = () throws -> String
typealias Parser = (String) throws -> [String: Any]
typealias Fetcher = () throws -> Data
typealias Parser = (Data) throws -> [String: Any]
init(loader: @escaping Fetcher, parser: @escaping Parser) {
self.load = loader
@ -15,7 +17,7 @@ final class CommonConfigurationProvider: ConfigurationProvider {
let parse: Parser
func configuration() throws -> [Key: String] {
let rawData: String
let rawData: Data
let parsedData: [String: Any]
do {
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() {
let fetcher: CommonConfigurationProvider.Fetcher = { return "" }
let fetcher: CommonConfigurationProvider.Fetcher = { Data() }
let parser: CommonConfigurationProvider.Parser = { _ in throw TestError() }
let provider = CommonConfigurationProvider(loader: fetcher, parser: parser)
@ -31,7 +31,7 @@ final class CommonConfigurationProviderTests: XCTestCase {
}
func testDecodeError() {
let fetcher: CommonConfigurationProvider.Fetcher = { return "" }
let fetcher: CommonConfigurationProvider.Fetcher = { Data() }
let uuid = UUID()
let parser: CommonConfigurationProvider.Parser = { data in
return [
@ -52,9 +52,10 @@ final class CommonConfigurationProviderTests: XCTestCase {
}
func testDataFlow() throws {
let fetcher: CommonConfigurationProvider.Fetcher = { return "string" }
let parser: CommonConfigurationProvider.Parser = { data in
XCTAssertEqual(data, "string")
let data = "string".data(using: .utf8)!
let fetcher: CommonConfigurationProvider.Fetcher = { data }
let parser: CommonConfigurationProvider.Parser = { parserInput in
XCTAssertEqual(parserInput, data)
return [:]
}
let provider = CommonConfigurationProvider(loader: fetcher, parser: parser)
@ -63,7 +64,7 @@ final class CommonConfigurationProviderTests: XCTestCase {
}
func testDecodeValues() throws {
let fetcher: CommonConfigurationProvider.Fetcher = {"" }
let fetcher: CommonConfigurationProvider.Fetcher = { Data() }
let parser: CommonConfigurationProvider.Parser = { _ in
return [ "key": 22]
}
@ -73,7 +74,7 @@ final class CommonConfigurationProviderTests: XCTestCase {
}
func testDecodeArray() throws {
let fetcher: CommonConfigurationProvider.Fetcher = {"" }
let fetcher: CommonConfigurationProvider.Fetcher = { Data() }
let parser: CommonConfigurationProvider.Parser = { _ in
return [ "key": ["one", "two"]]
}
@ -87,7 +88,7 @@ final class CommonConfigurationProviderTests: XCTestCase {
}
func testNestedValues() throws {
let fetcher: CommonConfigurationProvider.Fetcher = {"" }
let fetcher: CommonConfigurationProvider.Fetcher = { Data() }
let parser: CommonConfigurationProvider.Parser = { _ in
return [
"key": [ "nested": "value" ],