Skip to content

Commit 86b6678

Browse files
committed
feat: add IntersectionType
1 parent 5a958b4 commit 86b6678

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed
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/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
}

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 = """

0 commit comments

Comments
 (0)