Skip to content
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

Add SerializedJSON type as string literal escaping helper #6730

Merged
merged 3 commits into from Jul 19, 2023

Conversation

MaxDesiatov
Copy link
Member

@MaxDesiatov MaxDesiatov commented Jul 19, 2023

Motivation:

Currently we have to call _nativePathString manually in a lot of places where AbsolutePath is interpolated into strings. We also need to take into account whether _nativePathString should be called with escaped: true or escaped: false arguments, which is prone to errors. The serialization format should carry the knowledge of its correct escaping algorithm, not the path representation itself.

We can make this much cleaner if string interpolations call _nativePathString automatically where needed, and serialization formats add proper escaping on top of that.

Let's mplements this for JSON, specifically where it is used in Swift SDKs.

Modifications:

Added SerializedJSON type as string literal escaping helper.

Also added a custom AbsolutePath interpolation to the new type, which removes the need to call _nativePathString when paths are interpolated in JSON strings.

Result:

We don't have to call _nativePathString manually in as many places. In future PRs _nativePathString should be declared private or at least internal so that it's hidden as an implementation detail of appropriate string interpolations.

Also added a custom `AbsolutePath` interpolation to the new type, which removes the need to call `_nativePathString` when paths are interpolated in JSON strings.
@MaxDesiatov
Copy link
Member Author

@swift-ci smoke test

@MaxDesiatov
Copy link
Member Author

@swift-ci smoke test

@MaxDesiatov
Copy link
Member Author

@swift-ci test windows

1 similar comment
@MaxDesiatov
Copy link
Member Author

@swift-ci test windows

@@ -348,15 +348,15 @@ final class DestinationTests: XCTestCase {
otherToolsNoRoot,
someToolsWithRoot,
invalidToolset,
] {
try fs.writeFileContents(AbsolutePath(validating: testFile.path), string: testFile.json)
] as [(path: AbsolutePath, json: SerializedJSON)] {
Copy link
Collaborator

@compnerd compnerd Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not move this into the variable declaration?

for testFile: [(AbsolutePath, SerializedJSON)] in [
  ...
] {
  try fs.writeFileContents($0.path, string: $0.json.underlying)
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separated out into a file-local private let declaration in the latest commit.

@MaxDesiatov
Copy link
Member Author

@swift-ci smoke test

@MaxDesiatov
Copy link
Member Author

@swift-ci test windows

@MaxDesiatov MaxDesiatov enabled auto-merge (squash) July 19, 2023 18:53
@MaxDesiatov MaxDesiatov merged commit 37e8245 into main Jul 19, 2023
4 of 5 checks passed
@MaxDesiatov MaxDesiatov deleted the maxd/serialized-json branch July 19, 2023 20:40
@@ -31,7 +31,7 @@ private let extraFlags = BuildFlags(
)

private let destinationV1 = (
path: "\(bundleRootPath)/destinationV1.json",
path: bundleRootPath.appending(component: "destinationV1.json"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

component: is redundant

@@ -59,11 +59,11 @@ private let destinationV2 = (
"extraCXXFlags": \#(extraFlags.cxxCompilerFlags),
"extraLinkerFlags": \#(extraFlags.linkerFlags)
}
"""#
"""# as SerializedJSON
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make SerializedJSON ExpressibleByStringLiteral so this case can go away

Copy link
Member

@tomerd tomerd Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, this is tuple... maybe use a type to avoid the cast than

)

private let usrBinTools = Dictionary(uniqueKeysWithValues: Toolset.KnownTool.allCases.map {
($0, "/usr/bin/\($0.rawValue)")
})

private let otherToolsNoRoot = (
path: "/tools/otherToolsNoRoot.json",
path: try! AbsolutePath(validating: "/tools/otherToolsNoRoot.json"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try validating: is not required in tests (there is an extension that does the force try)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cross-compilation test suite improvements to SwiftPM test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants