-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add SerializedJSON
type as string literal escaping helper
#6730
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
Conversation
Also added a custom `AbsolutePath` interpolation to the new type, which removes the need to call `_nativePathString` when paths are interpolated in JSON strings.
@swift-ci smoke test |
@swift-ci smoke test |
@swift-ci test windows |
1 similar comment
@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)] { |
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.
Why not move this into the variable declaration?
for testFile: [(AbsolutePath, SerializedJSON)] in [
...
] {
try fs.writeFileContents($0.path, string: $0.json.underlying)
}
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.
Separated out into a file-local private let
declaration in the latest commit.
@swift-ci smoke test |
@swift-ci test windows |
@@ -31,7 +31,7 @@ private let extraFlags = BuildFlags( | |||
) | |||
|
|||
private let destinationV1 = ( | |||
path: "\(bundleRootPath)/destinationV1.json", | |||
path: bundleRootPath.appending(component: "destinationV1.json"), |
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.
component:
is redundant
@@ -59,11 +59,11 @@ private let destinationV2 = ( | |||
"extraCXXFlags": \#(extraFlags.cxxCompilerFlags), | |||
"extraLinkerFlags": \#(extraFlags.linkerFlags) | |||
} | |||
"""# | |||
"""# as SerializedJSON |
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.
make SerializedJSON ExpressibleByStringLiteral so this case can go away
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.
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"), |
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.
try validating:
is not required in tests (there is an extension that does the force try)
Motivation:
Currently we have to call
_nativePathString
manually in a lot of places whereAbsolutePath
is interpolated into strings. We also need to take into account whether_nativePathString
should be called withescaped: true
orescaped: 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 declaredprivate
or at leastinternal
so that it's hidden as an implementation detail of appropriate string interpolations.