Skip to content

Commit

Permalink
Merge pull request #10 from skiptools/bridgesupport
Browse files Browse the repository at this point in the history
Additions for bridge support
  • Loading branch information
aabewhite authored Nov 3, 2024
2 parents d3abf59 + 5b28bf3 commit d4e46f6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Sources/SkipLib/BridgeSupport.swift
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 {
}
51 changes: 51 additions & 0 deletions Tests/SkipLibTests/CodableTests.swift
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)
}
}

0 comments on commit d4e46f6

Please sign in to comment.