-
Notifications
You must be signed in to change notification settings - Fork 62
Add Foundation ProtobufMessage conformances (URL, UUID, Data, Locale) #804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+332
−21
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 1 addition & 19 deletions
20
Example/Example.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
107 changes: 107 additions & 0 deletions
107
Sources/OpenSwiftUICore/Data/Protobuf/Foundation+ProtobufMessage.swift
This file contains hidden or 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,107 @@ | ||
| // | ||
| // Foundation+ProtobufMessage.swift | ||
| // OpenSwiftUICore | ||
| // | ||
| // Audited for 6.5.4 | ||
| // Status: WIP | ||
| // Note: Data archiveWriter/archiveReader deduplication not yet implemented | ||
|
|
||
| package import Foundation | ||
|
|
||
| // MARK: - URL + ProtobufMessage | ||
|
|
||
| extension URL: ProtobufMessage { | ||
| package func encode(to encoder: inout ProtobufEncoder) throws { | ||
| try encoder.stringField(1, relativeString) | ||
| if let baseURL { | ||
| try encoder.messageField(2, baseURL) | ||
| } | ||
| } | ||
|
|
||
| package init(from decoder: inout ProtobufDecoder) throws { | ||
| var relativeString = "" | ||
| var baseURL: URL? = nil | ||
| while let field = try decoder.nextField() { | ||
| switch field.tag { | ||
| case 1: relativeString = try decoder.stringField(field) | ||
| case 2: baseURL = try decoder.messageField(field) | ||
| default: try decoder.skipField(field) | ||
| } | ||
| } | ||
| guard let url = URL(string: relativeString, relativeTo: baseURL) else { | ||
| throw ProtobufDecoder.DecodingError.failed | ||
| } | ||
| self = url | ||
| } | ||
| } | ||
|
|
||
| // MARK: - UUID + ProtobufMessage | ||
|
|
||
| extension UUID: ProtobufMessage { | ||
| package func encode(to encoder: inout ProtobufEncoder) throws { | ||
| withUnsafeBytes(of: uuid) { buffer in | ||
| encoder.dataField(1, buffer) | ||
| } | ||
| } | ||
|
|
||
| package init(from decoder: inout ProtobufDecoder) throws { | ||
| var uuidBytes: uuid_t = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | ||
| while let field = try decoder.nextField() { | ||
| switch field.tag { | ||
| case 1: | ||
| let buffer = try decoder.dataBufferField(field) | ||
| guard buffer.count == 16, let src = buffer.baseAddress else { | ||
| throw ProtobufDecoder.DecodingError.failed | ||
| } | ||
| withUnsafeMutableBytes(of: &uuidBytes) { $0.baseAddress!.copyMemory(from: src, byteCount: 16) } | ||
| default: | ||
| try decoder.skipField(field) | ||
| } | ||
| } | ||
| self = UUID(uuid: uuidBytes) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Data + ProtobufMessage [WIP] | ||
|
|
||
| extension Data: ProtobufMessage { | ||
| package func encode(to encoder: inout ProtobufEncoder) throws { | ||
| if /*let archiveWriter*/ false { | ||
| // TODO | ||
| _openSwiftUIUnreachableCode() | ||
| } else { | ||
| encoder.dataField(2, self) | ||
| } | ||
| } | ||
|
|
||
| package init(from decoder: inout ProtobufDecoder) throws { | ||
| self = Data() | ||
| while let field = try decoder.nextField() { | ||
| switch field.tag { | ||
| case 2: | ||
| self = try decoder.dataField(field) | ||
| default: | ||
| try decoder.skipField(field) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Locale + ProtobufMessage | ||
|
|
||
| extension Locale: ProtobufMessage { | ||
| package func encode(to encoder: inout ProtobufEncoder) throws { | ||
| try encoder.stringField(1, identifier) | ||
| } | ||
|
|
||
| package init(from decoder: inout ProtobufDecoder) throws { | ||
| var identifier = "" | ||
| while let field = try decoder.nextField() { | ||
| switch field.tag { | ||
| case 1: identifier = try decoder.stringField(field) | ||
| default: try decoder.skipField(field) | ||
| } | ||
| } | ||
| self = Locale(identifier: identifier) | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
Sources/OpenSwiftUISymbolDualTestsSupport/Data/Protobuf/FoundationProtobufMessageTestsStub.c
This file contains hidden or 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,26 @@ | ||
| // | ||
| // FoundationProtobufMessageTestsStub.c | ||
| // OpenSwiftUISymbolDualTestsSupport | ||
|
|
||
| #include "OpenSwiftUIBase.h" | ||
|
|
||
| #if OPENSWIFTUI_TARGET_OS_DARWIN | ||
| #import <SymbolLocator.h> | ||
|
|
||
| // URL | ||
| DEFINE_SL_STUB_SLF(OpenSwiftUITestStub_URLEncode, SwiftUICore, $s10Foundation3URLV7SwiftUIE6encode2toyAD15ProtobufEncoderVz_tKF); | ||
| DEFINE_SL_STUB_SLF(OpenSwiftUITestStub_URLDecode, SwiftUICore, $s10Foundation3URLV7SwiftUIE4fromAcD15ProtobufDecoderVz_tKcfC); | ||
|
|
||
| // UUID | ||
| DEFINE_SL_STUB_SLF(OpenSwiftUITestStub_UUIDEncode, SwiftUICore, $s10Foundation4UUIDV7SwiftUIE6encode2toyAD15ProtobufEncoderVz_tKF); | ||
| DEFINE_SL_STUB_SLF(OpenSwiftUITestStub_UUIDDecode, SwiftUICore, $s10Foundation4UUIDV7SwiftUIE4fromAcD15ProtobufDecoderVz_tKcfC); | ||
|
|
||
| // Data | ||
| DEFINE_SL_STUB_SLF(OpenSwiftUITestStub_DataEncode, SwiftUICore, $s10Foundation4DataV7SwiftUIE6encode2toyAD15ProtobufEncoderVz_tKF); | ||
| DEFINE_SL_STUB_SLF(OpenSwiftUITestStub_DataDecode, SwiftUICore, $s10Foundation4DataV7SwiftUIE4fromAcD15ProtobufDecoderVz_tKcfC); | ||
|
|
||
| // Locale | ||
| DEFINE_SL_STUB_SLF(OpenSwiftUITestStub_LocaleEncode, SwiftUICore, $s10Foundation6LocaleV7SwiftUIE6encode2toyAD15ProtobufEncoderVz_tKF); | ||
| DEFINE_SL_STUB_SLF(OpenSwiftUITestStub_LocaleDecode, SwiftUICore, $s10Foundation6LocaleV7SwiftUIE4fromAcD15ProtobufDecoderVz_tKcfC); | ||
|
|
||
| #endif |
196 changes: 196 additions & 0 deletions
196
Tests/OpenSwiftUISymbolDualTests/Data/Protobuf/FoundationProtobufMessageDualTests.swift
This file contains hidden or 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,196 @@ | ||
| // | ||
| // FoundationProtobufMessageDualTests.swift | ||
| // OpenSwiftUISymbolDualTests | ||
|
|
||
| #if canImport(SwiftUI, _underlyingVersion: 6.5.4) | ||
| import Foundation | ||
| import OpenSwiftUICore | ||
| import OpenSwiftUITestsSupport | ||
| import Testing | ||
|
|
||
| // MARK: - @_silgen_name declarations | ||
|
|
||
| extension URL { | ||
| @_silgen_name("OpenSwiftUITestStub_URLEncode") | ||
| func swiftUI_encode(to encoder: inout ProtobufEncoder) throws | ||
|
|
||
| @_silgen_name("OpenSwiftUITestStub_URLDecode") | ||
| init(swiftUI_from decoder: inout ProtobufDecoder) throws | ||
| } | ||
|
|
||
| extension UUID { | ||
| @_silgen_name("OpenSwiftUITestStub_UUIDEncode") | ||
| func swiftUI_encode(to encoder: inout ProtobufEncoder) throws | ||
|
|
||
| @_silgen_name("OpenSwiftUITestStub_UUIDDecode") | ||
| init(swiftUI_from decoder: inout ProtobufDecoder) throws | ||
| } | ||
|
|
||
| extension Data { | ||
| @_silgen_name("OpenSwiftUITestStub_DataEncode") | ||
| func swiftUI_encode(to encoder: inout ProtobufEncoder) throws | ||
|
|
||
| @_silgen_name("OpenSwiftUITestStub_DataDecode") | ||
| init(swiftUI_from decoder: inout ProtobufDecoder) throws | ||
| } | ||
|
|
||
| extension Locale { | ||
| @_silgen_name("OpenSwiftUITestStub_LocaleEncode") | ||
| func swiftUI_encode(to encoder: inout ProtobufEncoder) throws | ||
|
|
||
| @_silgen_name("OpenSwiftUITestStub_LocaleDecode") | ||
| init(swiftUI_from decoder: inout ProtobufDecoder) throws | ||
| } | ||
|
|
||
| // MARK: - Tests | ||
|
|
||
| @Suite | ||
| struct FoundationProtobufMessageDualTests { | ||
| @Suite | ||
| struct URLTests { | ||
| @Test( | ||
| arguments: [ | ||
| ( | ||
| URL(string: "https://example.com")!, | ||
| "0a1368747470733a2f2f6578616d706c652e636f6d" | ||
| ), | ||
| ( | ||
| URL(string: "path", relativeTo: URL(string: "https://example.com"))!, | ||
| "0a04706174681215 0a1368747470733a2f2f6578616d706c652e636f6d" | ||
| .replacingOccurrences(of: " ", with: "") | ||
| ), | ||
| ] | ||
| ) | ||
| func pbMessage(url: URL, hexString: String) throws { | ||
| try url.testPBEncoding(hexString: hexString) | ||
| try url.testPBDecoding(hexString: hexString) | ||
| try url.testPBEncoding(swiftUI_hexString: hexString) | ||
| try url.testPBDecoding(swiftUI_hexString: hexString) | ||
| } | ||
| } | ||
|
|
||
| @Suite | ||
| struct UUIDTests { | ||
| @Test( | ||
| arguments: [ | ||
| ( | ||
| UUID(uuidString: "E621E1F8-C36C-495A-93FC-0C247A3E6E5F")!, | ||
| "0a10e621e1f8c36c495a93fc0c247a3e6e5f" | ||
| ), | ||
| ] | ||
| ) | ||
| func pbMessage(uuid: UUID, hexString: String) throws { | ||
| try uuid.testPBEncoding(hexString: hexString) | ||
| try uuid.testPBDecoding(hexString: hexString) | ||
| try uuid.testPBEncoding(swiftUI_hexString: hexString) | ||
| try uuid.testPBDecoding(swiftUI_hexString: hexString) | ||
| } | ||
| } | ||
|
|
||
| @Suite | ||
| struct DataTests { | ||
| @Test( | ||
| arguments: [ | ||
| (Data(), ""), | ||
| (Data([0x48, 0x65, 0x6c, 0x6c, 0x6f]), "120548656c6c6f"), | ||
| ] | ||
| ) | ||
| func pbMessage(data: Data, hexString: String) throws { | ||
| try data.testPBEncoding(hexString: hexString) | ||
| try data.testPBDecoding(hexString: hexString) | ||
| try data.testPBEncoding(swiftUI_hexString: hexString) | ||
| try data.testPBDecoding(swiftUI_hexString: hexString) | ||
| } | ||
| } | ||
|
|
||
| @Suite | ||
| struct LocaleTests { | ||
| @Test( | ||
| arguments: [ | ||
| (Locale(identifier: "en_US"), "0a05656e5f5553"), | ||
| ] | ||
| ) | ||
| func pbMessage(locale: Locale, hexString: String) throws { | ||
| try locale.testPBEncoding(hexString: hexString) | ||
| try locale.testPBDecoding(hexString: hexString) | ||
| try locale.testPBEncoding(swiftUI_hexString: hexString) | ||
| try locale.testPBDecoding(swiftUI_hexString: hexString) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // MARK: - SwiftUI Dual Test Helpers | ||
|
|
||
| extension URL { | ||
| func testPBEncoding(swiftUI_hexString expectedHexString: String) throws { | ||
| let data = try ProtobufEncoder.encoding { encoder in | ||
| try swiftUI_encode(to: &encoder) | ||
| } | ||
| #expect(data.hexString == expectedHexString) | ||
| } | ||
|
|
||
| func testPBDecoding(swiftUI_hexString hexString: String) throws { | ||
| guard let data = Data(hexString: hexString) else { | ||
| throw ProtobufDecoder.DecodingError.failed | ||
| } | ||
| var decoder = ProtobufDecoder(data) | ||
| let decoded = try URL(swiftUI_from: &decoder) | ||
| #expect(decoded == self) | ||
| } | ||
| } | ||
|
|
||
| extension UUID { | ||
| func testPBEncoding(swiftUI_hexString expectedHexString: String) throws { | ||
| let data = try ProtobufEncoder.encoding { encoder in | ||
| try swiftUI_encode(to: &encoder) | ||
| } | ||
| #expect(data.hexString == expectedHexString) | ||
| } | ||
|
|
||
| func testPBDecoding(swiftUI_hexString hexString: String) throws { | ||
| guard let data = Data(hexString: hexString) else { | ||
| throw ProtobufDecoder.DecodingError.failed | ||
| } | ||
| var decoder = ProtobufDecoder(data) | ||
| let decoded = try UUID(swiftUI_from: &decoder) | ||
| #expect(decoded == self) | ||
| } | ||
| } | ||
|
|
||
| extension Data { | ||
| func testPBEncoding(swiftUI_hexString expectedHexString: String) throws { | ||
| let data = try ProtobufEncoder.encoding { encoder in | ||
| try swiftUI_encode(to: &encoder) | ||
| } | ||
| #expect(data.hexString == expectedHexString) | ||
| } | ||
|
|
||
| func testPBDecoding(swiftUI_hexString hexString: String) throws { | ||
| guard let data = Data(hexString: hexString) else { | ||
| throw ProtobufDecoder.DecodingError.failed | ||
| } | ||
| var decoder = ProtobufDecoder(data) | ||
| let decoded = try Data(swiftUI_from: &decoder) | ||
| #expect(decoded == self) | ||
| } | ||
| } | ||
|
|
||
| extension Locale { | ||
| func testPBEncoding(swiftUI_hexString expectedHexString: String) throws { | ||
| let data = try ProtobufEncoder.encoding { encoder in | ||
| try swiftUI_encode(to: &encoder) | ||
| } | ||
| #expect(data.hexString == expectedHexString) | ||
| } | ||
|
|
||
| func testPBDecoding(swiftUI_hexString hexString: String) throws { | ||
| guard let data = Data(hexString: hexString) else { | ||
| throw ProtobufDecoder.DecodingError.failed | ||
| } | ||
| var decoder = ProtobufDecoder(data) | ||
| let decoded = try Locale(swiftUI_from: &decoder) | ||
| #expect(decoded == self) | ||
| } | ||
| } | ||
|
|
||
| #endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defaulting
USE_LOCAL_DEPStotruemeans a defaultswift buildwill try to resolve sibling path dependencies (e.g.../OpenCoreGraphics,../OpenObservation) and can fail for users/CI that don’t have those checkouts. Is it intentional that the “local deps” mode is now the default behavior?Severity: high
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.