Skip to content

Commit a577380

Browse files
Breaking change: Bundle is now first parameter without label
- Changed API to: Embedded.getJSON(bundle, path: "file.json") - Property wrappers: @Embedded.json(bundle, path: "file.json") - Removed default Bundle.main - must be explicit - More consistent and cleaner API
1 parent 7f769cf commit a577380

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ struct User: Decodable {
4848
}
4949

5050
struct MyApp {
51-
@Embedded.json("Resources/users.json")
51+
@Embedded.json(Bundle.main, path: "Resources/users.json")
5252
var users: [User]
5353

54-
@Embedded.yaml("Config/settings.yaml")
54+
@Embedded.yaml(Bundle.main, path: "Config/settings.yaml")
5555
var config: Config
5656

5757
func printInfo() {
@@ -76,7 +76,7 @@ struct APITests {
7676
let expected: String
7777
}
7878

79-
@Test("URL Validation", arguments: Embedded.getJSON("TestData/url_tests.json", as: [TestCase].self))
79+
@Test("URL Validation", arguments: Embedded.getJSON(Bundle.module, path: "TestData/url_tests.json", as: [TestCase].self))
8080
func testURLs(testCase: TestCase) {
8181
// Test implementation
8282
}
@@ -91,13 +91,13 @@ You can also load resources directly without property wrappers:
9191
import SwiftEmbed
9292

9393
// Load and decode JSON
94-
let users = Embedded.getJSON("users.json", as: [User].self)
94+
let users = Embedded.getJSON(Bundle.main, path: "users.json", as: [User].self)
9595

9696
// Load and decode YAML
97-
let config = Embedded.getYAML("config.yaml", as: Config.self)
97+
let config = Embedded.getYAML(Bundle.main, path: "config.yaml", as: Config.self)
9898

99-
// Specify bundle explicitly
100-
let data = Embedded.getJSON("data.json", bundle: Bundle.module, as: MyData.self)
99+
// In tests with Bundle.module
100+
let testData = Embedded.getJSON(Bundle.module, path: "TestData/tests.json", as: [TestCase].self)
101101
```
102102

103103
## File Organization

Sources/SwiftEmbed/Embedded.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import Yams
44
/// Namespace for embedded resource property wrappers
55
public enum Embedded {
66
/// Load JSON resource and decode it to the specified type
7-
public static func getJSON<T: Decodable>(_ path: String, bundle: Bundle = Bundle.main, as type: T.Type = T.self) -> T {
8-
@Embedded.json(path, bundle: bundle)
7+
public static func getJSON<T: Decodable>(_ bundle: Bundle, path: String, as type: T.Type = T.self) -> T {
8+
@json(bundle, path: path)
99
var value: T
1010
return value
1111
}
1212

1313
/// Load YAML resource and decode it to the specified type
14-
public static func getYAML<T: Decodable>(_ path: String, bundle: Bundle = Bundle.main, as type: T.Type = T.self) -> T {
15-
@Embedded.yaml(path, bundle: bundle)
14+
public static func getYAML<T: Decodable>(_ bundle: Bundle, path: String, as type: T.Type = T.self) -> T {
15+
@yaml(bundle, path: path)
1616
var value: T
1717
return value
1818
}
@@ -25,7 +25,7 @@ public enum Embedded {
2525
value
2626
}
2727

28-
public init(_ path: String, bundle: Bundle = Bundle.main) {
28+
public init(_ bundle: Bundle, path: String) {
2929
let data = Self.loadData(path: path, bundle: bundle)
3030

3131
do {
@@ -45,7 +45,7 @@ public enum Embedded {
4545
value
4646
}
4747

48-
public init(_ path: String, bundle: Bundle = Bundle.main) {
48+
public init(_ bundle: Bundle, path: String) {
4949
let data = Self.loadData(path: path, bundle: bundle)
5050

5151
do {

Tests/SwiftEmbedTests/EmbeddedTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct EmbeddedTests {
3636

3737
@Test("Load JSON file using @Embedded.json")
3838
func testLoadJSON() {
39-
@Embedded.json("TestData/sample.json", bundle: Bundle.module)
39+
@Embedded.json(Bundle.module, path: "TestData/sample.json")
4040
var user: User
4141

4242
#expect(user.name == "Test User")
@@ -47,7 +47,7 @@ struct EmbeddedTests {
4747

4848
@Test("JSON property wrapper maintains value")
4949
func testJSONValueConsistency() {
50-
@Embedded.json("TestData/sample.json", bundle: Bundle.module)
50+
@Embedded.json(Bundle.module, path: "TestData/sample.json")
5151
var user: User
5252

5353
let firstAccess = user
@@ -60,7 +60,7 @@ struct EmbeddedTests {
6060

6161
@Test("Load YAML file using @Embedded.yaml")
6262
func testLoadYAML() {
63-
@Embedded.yaml("TestData/config.yaml", bundle: Bundle.module)
63+
@Embedded.yaml(Bundle.module, path: "TestData/config.yaml")
6464
var config: Config
6565

6666
#expect(config.app.name == "TestApp")
@@ -76,7 +76,7 @@ struct EmbeddedTests {
7676

7777
@Test("YAML property wrapper maintains value")
7878
func testYAMLValueConsistency() {
79-
@Embedded.yaml("TestData/config.yaml", bundle: Bundle.module)
79+
@Embedded.yaml(Bundle.module, path: "TestData/config.yaml")
8080
var config: Config
8181

8282
let firstAccess = config

0 commit comments

Comments
 (0)