-
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 |
| 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 |
|
|
||
| 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
| "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 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
_nativePathStringmanually in a lot of places whereAbsolutePathis interpolated into strings. We also need to take into account whether_nativePathStringshould be called withescaped: trueorescaped: falsearguments, 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
_nativePathStringautomatically 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
SerializedJSONtype as string literal escaping helper.Also added a custom
AbsolutePathinterpolation to the new type, which removes the need to call_nativePathStringwhen paths are interpolated in JSON strings.Result:
We don't have to call
_nativePathStringmanually in as many places. In future PRs_nativePathStringshould be declaredprivateor at leastinternalso that it's hidden as an implementation detail of appropriate string interpolations.