Skip to content

Commit 7f769cf

Browse files
Add direct loading API with getJSON/getYAML methods
- Add Embedded.getJSON() and Embedded.getYAML() static methods - Useful for Swift Testing arguments and immediate loading - Update README with both API styles - Simplify examples to avoid duplication
1 parent 24df8f0 commit 7f769cf

File tree

2 files changed

+48
-29
lines changed

2 files changed

+48
-29
lines changed

README.md

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,52 +27,43 @@ dependencies: [
2727

2828
## Usage
2929

30-
### JSON Files
30+
SwiftEmbed provides two ways to load resources:
31+
- **Property wrappers** (`@Embedded.json` / `@Embedded.yaml`) - for class/struct properties
32+
- **Direct loading** (`Embedded.getJSON` / `Embedded.getYAML`) - for immediate use
33+
34+
### Property Wrappers
3135

3236
```swift
3337
import SwiftEmbed
3438

39+
struct Config: Decodable {
40+
let apiURL: String
41+
let timeout: Int
42+
let features: [String]
43+
}
44+
3545
struct User: Decodable {
3646
let name: String
3747
let email: String
3848
}
3949

40-
struct MyClass {
50+
struct MyApp {
4151
@Embedded.json("Resources/users.json")
4252
var users: [User]
4353

44-
func printUsers() {
45-
for user in users {
46-
print("\(user.name): \(user.email)")
47-
}
48-
}
49-
}
50-
```
51-
52-
### YAML Files
53-
54-
```swift
55-
import SwiftEmbed
56-
57-
struct Config: Decodable {
58-
let apiURL: String
59-
let timeout: Int
60-
}
61-
62-
struct Settings {
6354
@Embedded.yaml("Config/settings.yaml")
6455
var config: Config
6556

66-
func configure() {
67-
print("API URL: \(config.apiURL)")
68-
print("Timeout: \(config.timeout)s")
57+
func printInfo() {
58+
print("API: \(config.apiURL)")
59+
print("Users: \(users.count)")
6960
}
7061
}
7162
```
7263

7364
### In Tests
7465

75-
Perfect for loading test data:
66+
Perfect for loading test data with parametrized tests:
7667

7768
```swift
7869
import Testing
@@ -85,16 +76,30 @@ struct APITests {
8576
let expected: String
8677
}
8778

88-
@Embedded.json("TestData/url_tests.json")
89-
var testCases: [TestCase]
90-
91-
@Test("URL Validation", arguments: testCases)
79+
@Test("URL Validation", arguments: Embedded.getJSON("TestData/url_tests.json", as: [TestCase].self))
9280
func testURLs(testCase: TestCase) {
9381
// Test implementation
9482
}
9583
}
9684
```
9785

86+
### Direct Loading
87+
88+
You can also load resources directly without property wrappers:
89+
90+
```swift
91+
import SwiftEmbed
92+
93+
// Load and decode JSON
94+
let users = Embedded.getJSON("users.json", as: [User].self)
95+
96+
// Load and decode YAML
97+
let config = Embedded.getYAML("config.yaml", as: Config.self)
98+
99+
// Specify bundle explicitly
100+
let data = Embedded.getJSON("data.json", bundle: Bundle.module, as: MyData.self)
101+
```
102+
98103
## File Organization
99104

100105
Place your resource files in your target:

Sources/SwiftEmbed/Embedded.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ import Yams
33

44
/// Namespace for embedded resource property wrappers
55
public enum Embedded {
6+
/// 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)
9+
var value: T
10+
return value
11+
}
12+
13+
/// 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)
16+
var value: T
17+
return value
18+
}
19+
620
@propertyWrapper
721
public struct json<T: Decodable> {
822
private let value: T

0 commit comments

Comments
 (0)