From 5b28bf36412e16c714678b9dedec0bd6519d173f Mon Sep 17 00:00:00 2001 From: Abe White Date: Sun, 3 Nov 2024 00:42:57 -0500 Subject: [PATCH] Additions for bridge support --- Sources/SkipLib/BridgeSupport.swift | 9 +++++ Tests/SkipLibTests/CodableTests.swift | 51 +++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 Sources/SkipLib/BridgeSupport.swift create mode 100644 Tests/SkipLibTests/CodableTests.swift diff --git a/Sources/SkipLib/BridgeSupport.swift b/Sources/SkipLib/BridgeSupport.swift new file mode 100644 index 0000000..1f3aa74 --- /dev/null +++ b/Sources/SkipLib/BridgeSupport.swift @@ -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 { +} diff --git a/Tests/SkipLibTests/CodableTests.swift b/Tests/SkipLibTests/CodableTests.swift new file mode 100644 index 0000000..1da4a0b --- /dev/null +++ b/Tests/SkipLibTests/CodableTests.swift @@ -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) + } +}