Skip to content

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

Merged
merged 3 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/Basics/ByteString+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ extension ByteString {
public var sha256Checksum: String {
SHA256().hash(self).hexadecimalRepresentation
}

public init(json: SerializedJSON) {
self.init(json.underlying.utf8)
}
}
3 changes: 2 additions & 1 deletion Sources/Basics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ add_library(Basics
Errors.swift
FileSystem/AbsolutePath.swift
FileSystem/FileSystem+Extensions.swift
FileSystem/NativePathExtensions.swift
FileSystem/RelativePath.swift
FileSystem/TemporaryFile.swift
FileSystem/TSCAdapters.swift
Expand All @@ -45,12 +46,12 @@ add_library(Basics
ImportScanning.swift
JSON+Extensions.swift
JSONDecoder+Extensions.swift
NativePathExtensions.swift
Netrc.swift
Observability.swift
SQLite.swift
Sandbox.swift
SendableTimeInterval.swift
Serialization/SerializedJSON.swift
String+Extensions.swift
SwiftVersion.swift
SQLiteBackedCache.swift
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ extension AbsolutePath {
}
}
}

extension DefaultStringInterpolation {
public mutating func appendInterpolation(_ value: AbsolutePath) {
self.appendInterpolation(value._nativePathString(escaped: false))
}
}

extension SerializedJSON.StringInterpolation {
public mutating func appendInterpolation(_ value: AbsolutePath) {
self.appendInterpolation(value._nativePathString(escaped: false))
}
}
49 changes: 49 additions & 0 deletions Sources/Basics/Serialization/SerializedJSON.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

/// Wrapper type representing serialized escaped JSON strings providing helpers
/// for escaped string interpolations for common types such as `AbsolutePath`.
public struct SerializedJSON {
let underlying: String
}

extension SerializedJSON: ExpressibleByStringLiteral {
public init(stringLiteral: String) {
self.underlying = stringLiteral
}
}

extension SerializedJSON: ExpressibleByStringInterpolation {
public init(stringInterpolation: StringInterpolation) {
self.init(underlying: stringInterpolation.value)
}

public struct StringInterpolation: StringInterpolationProtocol {
fileprivate var value: String = ""

private func escape(_ string: String) -> String {
string.replacingOccurrences(of: #"\"#, with: #"\\"#)
}

public init(literalCapacity: Int, interpolationCount: Int) {
self.value.reserveCapacity(literalCapacity)
}

public mutating func appendLiteral(_ literal: String) {
self.value.append(self.escape(literal))
}

public mutating func appendInterpolation(_ value: some CustomStringConvertible) {
self.value.append(self.escape(value.description))
}
}
}
40 changes: 40 additions & 0 deletions Tests/BasicsTests/Serialization/SerializedJSONTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

@testable import Basics
import XCTest

final class SerializedJSONTests: XCTestCase {
func testPathInterpolation() throws {
var path = try AbsolutePath(validating: #"/test\backslashes"#)
var json: SerializedJSON = "\(path)"

XCTAssertEqual(json.underlying, #"/test\\backslashes"#)

#if os(Windows)
path = try AbsolutePath(validating: #"\\?\C:\Users"#)
json = "\(path)"

XCTAssertEqual(json.underlying, #"\\\\?\\C:\\Users"#)

path = try AbsolutePath(validating: #"\\.\UNC\server\share\n\"#)
json = "\(path)"

XCTAssertEqual(json.underlying, #"\\\\.\\UNC\\server\\share\\"#)

path = try AbsolutePath(validating: #"\??\Volumes{b79de17a-a1ed-4c58-a353-731b7c4885a6}\\"#)
json = "\(path)"

XCTAssertEqual(json.underlying, #"\\??\\Volumes{b79de17a-a1ed-4c58-a353-731b7c4885a6}\\"#)
#endif
}
}
4 changes: 2 additions & 2 deletions Tests/PackageModelTests/SwiftSDKBundleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import class TSCBasic.InMemoryFileSystem

private let testArtifactID = "test-artifact"

private func generateInfoJSON(artifacts: [MockArtifact]) -> String {
private func generateInfoJSON(artifacts: [MockArtifact]) -> SerializedJSON {
"""
{
"artifacts" : {
Expand Down Expand Up @@ -68,7 +68,7 @@ private func generateTestFileSystem(bundleArtifacts: [MockArtifact]) throws -> (
(
"\($0.path)/info.json",
ByteString(
encodingAsUTF8: generateInfoJSON(artifacts: $0.artifacts)
json: generateInfoJSON(artifacts: $0.artifacts)
)
)
})
Expand Down
Loading