Skip to content

Commit 05481e2

Browse files
committed
Create extension on JSONDecoder to decode directly from JSON string
1 parent 7f11a31 commit 05481e2

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import Foundation
12+
13+
extension JSONDecoder {
14+
public func decode<T>(_ type: T.Type, from string: String) throws -> T where T : Decodable {
15+
guard let data = string.data(using: .utf8) else {
16+
let context = DecodingError.Context(codingPath: [], debugDescription: "invalid UTF-8 string")
17+
throw DecodingError.dataCorrupted(context)
18+
}
19+
20+
return try decode(type, from: data)
21+
}
22+
}

Tests/PackageRegistryTests/RegistryConfigurationTests.swift

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
import XCTest
12+
import SPMTestSupport
1213
@testable import PackageRegistry
1314

1415
private let defaultRegistryBaseURL = URL(string: "https://packages.example.com/")!
@@ -47,20 +48,20 @@ final class RegistryConfigurationTests: XCTestCase {
4748
}
4849

4950
func testDecodeEmptyConfiguration() throws {
50-
let data = #"""
51+
let json = #"""
5152
{
5253
"registries": {},
5354
"version": 1
5455
}
55-
"""#.data(using: .utf8)!
56+
"""#
5657

57-
let configuration = try decoder.decode(RegistryConfiguration.self, from: data)
58+
let configuration = try decoder.decode(RegistryConfiguration.self, from: json)
5859
XCTAssertNil(configuration.defaultRegistry)
5960
XCTAssertEqual(configuration.scopedRegistries, [:])
6061
}
6162

6263
func testDecodeExampleConfiguration() throws {
63-
let data = #"""
64+
let json = #"""
6465
{
6566
"registries": {
6667
"[default]": {
@@ -75,58 +76,58 @@ final class RegistryConfigurationTests: XCTestCase {
7576
},
7677
"version": 1
7778
}
78-
"""#.data(using: .utf8)!
79+
"""#
7980

80-
let configuration = try decoder.decode(RegistryConfiguration.self, from: data)
81+
let configuration = try decoder.decode(RegistryConfiguration.self, from: json)
8182
XCTAssertEqual(configuration.defaultRegistry?.url, defaultRegistryBaseURL)
8283
XCTAssertEqual(configuration.scopedRegistries["foo"]?.url, customRegistryBaseURL)
8384
XCTAssertEqual(configuration.scopedRegistries["bar"]?.url, customRegistryBaseURL)
8485
}
8586

8687
func testDecodeConfigurationWithInvalidRegistryKey() throws {
87-
let data = #"""
88+
let json = #"""
8889
{
8990
"registries": {
9091
0: "\#(customRegistryBaseURL)"
9192
},
9293
"version": 1
9394
}
94-
"""#.data(using: .utf8)!
95+
"""#
9596

96-
XCTAssertThrowsError(try decoder.decode(RegistryConfiguration.self, from: data))
97+
XCTAssertThrowsError(try decoder.decode(RegistryConfiguration.self, from: json))
9798
}
9899

99100
func testDecodeConfigurationWithInvalidRegistryValue() throws {
100-
let data = #"""
101+
let json = #"""
101102
{
102103
"registries": {
103104
"[default]": "\#(customRegistryBaseURL)"
104105
},
105106
"version": 1
106107
}
107-
"""#.data(using: .utf8)!
108+
"""#
108109

109-
XCTAssertThrowsError(try decoder.decode(RegistryConfiguration.self, from: data))
110+
XCTAssertThrowsError(try decoder.decode(RegistryConfiguration.self, from: json))
110111
}
111112

112113
func testDecodeConfigurationWithMissingVersion() throws {
113-
let data = #"""
114+
let json = #"""
114115
{
115116
"registries": {}
116117
}
117-
"""#.data(using: .utf8)!
118+
"""#
118119

119-
XCTAssertThrowsError(try decoder.decode(RegistryConfiguration.self, from: data))
120+
XCTAssertThrowsError(try decoder.decode(RegistryConfiguration.self, from: json))
120121
}
121122

122123
func testDecodeConfigurationWithInvalidVersion() throws {
123-
let data = #"""
124+
let json = #"""
124125
{
125126
"registries": {},
126127
"version": 999
127128
}
128-
"""#.data(using: .utf8)!
129+
"""#
129130

130-
XCTAssertThrowsError(try decoder.decode(RegistryConfiguration.self, from: data))
131+
XCTAssertThrowsError(try decoder.decode(RegistryConfiguration.self, from: json))
131132
}
132133
}

0 commit comments

Comments
 (0)