-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from skiptools/bridgesupport
Additions for bridge support
- Loading branch information
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2024 Skip | ||
// | ||
// This is free software: you can redistribute and/or modify it | ||
// under the terms of the GNU Lesser General Public License 3.0 | ||
// as published by the Free Software Foundation https://fsf.org | ||
|
||
/// Marker protocol for types that are custom bridged from the Swift side. | ||
public protocol SwiftCustomBridged { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2024 Skip | ||
// | ||
// This is free software: you can redistribute and/or modify it | ||
// under the terms of the GNU Lesser General Public License 3.0 | ||
// as published by the Free Software Foundation https://fsf.org | ||
import XCTest | ||
|
||
final class CodableTests: XCTestCase { | ||
} | ||
|
||
// Just verify that this transpiles into valid code | ||
enum CodableTestsEnum: Codable { | ||
case string(String?) | ||
case int(Int?) | ||
|
||
enum RawType: String, Codable { | ||
case string | ||
case int | ||
} | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case type | ||
case value | ||
} | ||
|
||
init(from decoder: any Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
let rawType: RawType = try container.decode(RawType.self, forKey: .type) | ||
switch rawType { | ||
case .int: | ||
self = .int(try container.decodeIfPresent(Int.self, forKey: .value)) | ||
case .string: | ||
self = .string(try container.decodeIfPresent(String.self, forKey: .value)) | ||
} | ||
} | ||
|
||
func encode(to encoder: any Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
|
||
let rawType: RawType | ||
switch self { | ||
case let .string(value): | ||
rawType = .string | ||
try container.encodeIfPresent(value, forKey: .value) | ||
case let .int(value): | ||
rawType = .int | ||
try container.encodeIfPresent(value, forKey: .value) | ||
} | ||
try container.encode(rawType, forKey: .type) | ||
} | ||
} |