Skip to content

Commit 2f4b505

Browse files
committed
Merge branch 'develop' into release
2 parents 2df2a7e + 2709c8f commit 2f4b505

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

Sources/ValueCodable/Value.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ extension Value {
196196
}
197197

198198
public subscript(index: Int) -> Value? {
199-
switch self {
200-
case .array(let v):
201-
return index >= 0
202-
? v[index]
203-
: v[v.index(v.endIndex, offsetBy: index)] // from end
199+
switch (self, index) {
200+
case (.array(let v), index) where (0..<v.count).contains(index):
201+
return v[index]
202+
case (.array(let v), index) where (-v.count..<0).contains(index):
203+
return v[v.index(v.endIndex, offsetBy: index)] // from end
204204
default:
205205
return nil
206206
}

Tests/LinuxMain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated using Sourcery 0.15.0 — https://github.com/krzysztofzablocki/Sourcery
1+
// Generated using Sourcery 0.16.0 — https://github.com/krzysztofzablocki/Sourcery
22
// DO NOT EDIT
33

44

Tests/ValueCodableTests/ValueCodableTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ final class ValueCodableTests: XCTestCase {
211211
XCTAssertEqual(a[0], "a")
212212
XCTAssertEqual(a[-1], "z")
213213
XCTAssertEqual(a[1], a[-2])
214+
XCTAssertEqual(a[-3], "a")
215+
XCTAssertNil(a[-4])
216+
let empty: Value = .array([])
217+
XCTAssertNil(empty[0])
218+
XCTAssertNil(empty[-1])
214219
}
215220

216221
}

examples/basic.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/swift sh
2+
import ValueCodable // @finestructure ~> 0.0.2
3+
import Yams // @jpsim
4+
5+
let s = """
6+
name: foo
7+
data:
8+
obj:
9+
a: 1
10+
b: two
11+
list:
12+
- 1
13+
- two
14+
- foo: bar
15+
"""
16+
17+
struct Test: Decodable {
18+
let name: String
19+
// capture the unstructure data as "Value"
20+
let data: Value
21+
}
22+
23+
let t = try YAMLDecoder().decode(Test.self, from: s)
24+
25+
// access "data" via subscript
26+
print(t.data["obj"]) // Optional(["a": 1, "b": "two"])
27+
print(t.data["list"]) // Optional([1, "two", ["foo": "bar"]])
28+
print(t.data["list"]?[0]) // Optional(1)
29+
30+
// act on the decoded types
31+
switch t.data["list"]?[2] {
32+
case let .some(.dictionary(dict)):
33+
print(dict) // ["foo": "bar"]
34+
default:
35+
print("unhandled type")
36+
}
37+
38+
// access propertyies via key paths
39+
print(t.data["obj.b"]) // Optional("two")
40+
print(t.data["list[0]"]) // Optional(1)
41+
print(t.data["list[2].foo"]) // Optional("bar")

0 commit comments

Comments
 (0)