Skip to content

Commit bed0e03

Browse files
committed
Allow // comments in swift-java.config "JSON with comments"
1 parent 78311ba commit bed0e03

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

Sources/SwiftJavaConfigurationShared/Configuration.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,37 @@ public func readConfiguration(sourceDir: String, file: String = #fileID, line: U
157157
return try readConfiguration(configPath: configPath, file: file, line: line)
158158
}
159159

160+
/// Read a swift-java.config file at the specified path.
161+
///
162+
/// Configuration is expected to be "JSON-with-comments".
163+
/// Specifically "//" comments are allowed and will be trimmed before passing the rest of the config into a standard JSON parser.
160164
public func readConfiguration(configPath: URL, file: String = #fileID, line: UInt = #line) throws -> Configuration? {
161165
guard let configData = try? Data(contentsOf: configPath) else {
162166
return nil
163167
}
164168

169+
guard let configString = String(data: configData, encoding: .utf8) else {
170+
return nil
171+
}
172+
173+
return try readConfiguration(string: configString, configPath: configPath)
174+
}
175+
176+
public func readConfiguration(string: String, configPath: URL?, file: String = #fileID, line: UInt = #line) throws -> Configuration? {
177+
let cleanedConfigString = string
178+
.split(separator: "\n")
179+
.filter { line in
180+
!line.trimmingCharacters(in: .whitespaces).starts(with: "//")
181+
}.joined(separator: "\n")
182+
183+
guard let configData = cleanedConfigString.data(using: .utf8) else {
184+
return nil
185+
}
186+
165187
do {
166188
return try JSONDecoder().decode(Configuration.self, from: configData)
167189
} catch {
168-
throw ConfigurationError(message: "Failed to parse SwiftJava configuration at '\(configPath.absoluteURL)'! \(#fileID):\(#line)", error: error,
190+
throw ConfigurationError(message: "Failed to parse SwiftJava configuration at '\(configPath.map({ $0.absoluteURL.description }) ?? "<no-path>")'! \(#fileID):\(#line)", error: error,
169191
file: file, line: line)
170192
}
171193
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024-2025 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import SwiftJavaConfigurationShared
16+
import Testing
17+
18+
@Suite
19+
struct SwiftJavaConfigurationTests {
20+
21+
@Test
22+
func parseJSONWithComments() throws {
23+
let config = try readConfiguration(string:
24+
"""
25+
// some comments
26+
{
27+
// anywhere is ok
28+
"classpath": ""
29+
}
30+
""", configPath: nil)
31+
}
32+
}
33+

0 commit comments

Comments
 (0)