Skip to content

Commit 5c16035

Browse files
authored
Merge pull request #7 from blocto/feat/capacity-decoding
Feat - update for Cadence decoding
2 parents 29a08f7 + 86b6678 commit 5c16035

File tree

10 files changed

+66
-14
lines changed

10 files changed

+66
-14
lines changed

Sources/Cadence/Argument.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@ extension Argument {
315315
Argument(.capability(value))
316316
}
317317

318-
public static func capability(path: String, address: Address, borrowType: FType) -> Argument {
319-
Argument(.capability(Capability(path: path, address: address, borrowType: borrowType)))
318+
public static func capability(id: String, address: Address, borrowType: FType) -> Argument {
319+
Argument(.capability(Capability(id: id, address: address, borrowType: borrowType)))
320320
}
321321

322322
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Shane Chi on 2024/9/30.
6+
//
7+
8+
import Foundation
9+
10+
public struct IntersectionType: Equatable, Codable {
11+
public let typeId: String
12+
public let types: [FType]
13+
14+
public init(typeId: String, types: [FType]) {
15+
self.typeId = typeId
16+
self.types = types
17+
}
18+
19+
// MARK: Codable
20+
enum CodingKeys: String, CodingKey {
21+
case typeId = "typeID"
22+
case types
23+
}
24+
}

Sources/Cadence/Capability.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import Foundation
1010
/// https://docs.onflow.org/cadence/json-cadence-spec/#capability
1111
public struct Capability: Codable, Equatable {
1212

13-
public let path: String
13+
public let id: String
1414
public let address: Address
1515
public let borrowType: FType
1616

1717
public init(
18-
path: String,
18+
id: String,
1919
address: Address,
2020
borrowType: FType
2121
) {
22-
self.path = path
22+
self.id = id
2323
self.address = address
2424
self.borrowType = borrowType
2525
}

Sources/Cadence/FType.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public enum FType: Equatable {
7575
indirect case restriction(RestrictionType)
7676
indirect case capability(borrowType: FType)
7777
indirect case `enum`(EnumType)
78+
indirect case intersection(IntersectionType)
7879

7980
public var kind: FTypeKind {
8081
switch self {
@@ -212,6 +213,8 @@ public enum FType: Equatable {
212213
return .capability
213214
case .enum:
214215
return .enum
216+
case .intersection:
217+
return .intersection
215218
}
216219
}
217220

@@ -263,6 +266,8 @@ public enum FType: Equatable {
263266
return "\(kind.rawValue)<\(borrowType.id)>"
264267
case let .enum(enumType):
265268
return enumType.typeId
269+
case let .intersection(IntersectionType):
270+
return IntersectionType.typeId
266271
}
267272
}
268273
}
@@ -468,6 +473,8 @@ extension FType: Codable {
468473
self = .enum(enumType)
469474
decoder.addTypeToDecodingResultsIfPossible(type: self, typeId: enumType.typeId)
470475
try enumType.decodePossibleRepeatedProperties(from: decoder)
476+
case .intersection:
477+
self = .intersection(try IntersectionType(from: decoder))
471478
}
472479
}
473480

Sources/Cadence/FTypeKind.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,5 @@ public enum FTypeKind: String, Equatable, Codable {
7575
case restriction = "Restriction"
7676
case capability = "Capability"
7777
case `enum` = "Enum"
78+
case intersection = "Intersection"
7879
}

Sources/Cadence/Format.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ enum Format {
3939
case capability(
4040
borrowType: FType,
4141
address: Address,
42-
path: String)
42+
id: String)
4343
}
4444

4545
// MARK: - CustomStringConvertible
@@ -131,9 +131,9 @@ extension Format: CustomStringConvertible {
131131
} else {
132132
return "Type<\(type.id)>()"
133133
}
134-
case let .capability(borrowType, address, path):
134+
case let .capability(borrowType, address, id):
135135
let typeArgument = borrowType.id.isEmpty ? "" : "<\(borrowType.id)>"
136-
return "Capability\(typeArgument)(address: \(address.hexStringWithPrefix), path: \(path))"
136+
return "Capability\(typeArgument)(address: \(address.hexStringWithPrefix), id: \(id))"
137137
}
138138
}
139139

Sources/Cadence/Value.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ extension Value: CustomStringConvertible {
508508
return Format.capability(
509509
borrowType: capability.borrowType,
510510
address: capability.address,
511-
path: capability.path
511+
id: capability.id
512512
).description
513513
}
514514
}

Tests/CadenceTests/ArgumentTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ final class ArgumentTests: XCTestCase {
749749
{
750750
"type": "Capability",
751751
"value": {
752-
"path": "/public/someInteger",
752+
"id": "7",
753753
"address": "0x1",
754754
"borrowType": {
755755
"kind": "Int"
@@ -765,7 +765,7 @@ final class ArgumentTests: XCTestCase {
765765
XCTAssertEqual(argument.type, .capability)
766766
XCTAssertEqual(argument.value, .capability(
767767
.init(
768-
path: "/public/someInteger",
768+
id: "7",
769769
address: Address(hexString: "0x1"),
770770
borrowType: .int
771771
)

Tests/CadenceTests/StaticTypeTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,26 @@ final class StaticTypeTests: XCTestCase {
13941394
XCTAssertEqual(value.kind, .reference)
13951395
}
13961396

1397+
func testDecodeIntersectionType() throws {
1398+
// Given:
1399+
let jsonData = """
1400+
{
1401+
"kind": "Intersection",
1402+
"typeID": "type-id",
1403+
"types": [{
1404+
"kind": "String"
1405+
}]
1406+
}
1407+
""".data(using: .utf8)!
1408+
1409+
// When:
1410+
let value = try decoder.decode(FType.self, from: jsonData)
1411+
1412+
// Then
1413+
XCTAssertEqual(value, .intersection(.init(typeId: "type-id", types: [.string])))
1414+
XCTAssertEqual(value.kind, .intersection)
1415+
}
1416+
13971417
func testDecodeRestrictedType() throws {
13981418
// Given:
13991419
let jsonData = """

Tests/CadenceTests/ValueTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ final class ValueTests: XCTestCase {
564564
func testToSwiftValueCapability() throws {
565565
// Arrange
566566
let capability = Capability(
567-
path: "path",
567+
id: "id",
568568
address: Address(hexString: "0xcb2d04fc89307107"),
569569
borrowType: .fix64)
570570
let value: Cadence.Value = .capability(capability)
@@ -651,10 +651,10 @@ final class ValueTests: XCTestCase {
651651
).description, "Type<Int>()")
652652
XCTAssertEqual(Value.capability(
653653
.init(
654-
path: "/storage/foo",
654+
id: "id",
655655
address: Address(data: Data([1, 2, 3, 4, 5])),
656656
borrowType: .int)
657-
).description, "Capability<Int>(address: 0x0000000102030405, path: /storage/foo)")
657+
).description, "Capability<Int>(address: 0x0000000102030405, id: id)")
658658
}
659659

660660
func testToSwiftValueDecodable() throws {

0 commit comments

Comments
 (0)