Skip to content

Commit c98dd8a

Browse files
committed
config: log-level should encode as string
1 parent c8023b7 commit c98dd8a

File tree

5 files changed

+87
-6
lines changed

5 files changed

+87
-6
lines changed

Samples/SwiftJavaExtractFFMSampleApp/Sources/MySwiftLibrary/LibrarySubDirectory/SwiftTypeInSubDirectory.swift

Whitespace-only changes.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"javaPackage": "com.example.swift"
2+
"javaPackage": "com.example.swift",
3+
"logLevel": "trace"
34
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 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+
package com.example.swift;
16+
17+
import org.junit.jupiter.api.Disabled;
18+
import org.junit.jupiter.api.Test;
19+
import org.swift.swiftkit.core.SwiftLibraries;
20+
import org.swift.swiftkit.ffm.AllocatingSwiftArena;
21+
22+
import java.io.File;
23+
import java.util.stream.Stream;
24+
25+
import static org.junit.jupiter.api.Assertions.*;
26+
27+
public class MySwiftClassTest {
28+
29+
void checkPaths(Throwable throwable) {
30+
var paths = SwiftLibraries.getJavaLibraryPath().split(":");
31+
for (var path : paths) {
32+
Stream.of(new File(path).listFiles())
33+
.filter(file -> !file.isDirectory())
34+
.forEach((file) -> {
35+
System.out.println(" - " + file.getPath());
36+
});
37+
}
38+
39+
throw new RuntimeException(throwable);
40+
}
41+
42+
@Test
43+
void test_MySwiftClass_voidMethod() {
44+
try(var arena = AllocatingSwiftArena.ofConfined()) {
45+
MySwiftClass o = MySwiftClass.init(12, 42, arena);
46+
o.voidMethod();
47+
} catch (Throwable throwable) {
48+
checkPaths(throwable);
49+
}
50+
}
51+
52+
@Test
53+
void test_MySwiftClass_makeIntMethod() {
54+
try(var arena = AllocatingSwiftArena.ofConfined()) {
55+
MySwiftClass o = MySwiftClass.init(12, 42, arena);
56+
var got = o.makeIntMethod();
57+
assertEquals(12, got);
58+
}
59+
}
60+
61+
@Test
62+
@Disabled // TODO: Need var mangled names in interfaces
63+
void test_MySwiftClass_property_len() {
64+
try(var arena = AllocatingSwiftArena.ofConfined()) {
65+
MySwiftClass o = MySwiftClass.init(12, 42, arena);
66+
var got = o.getLen();
67+
assertEquals(12, got);
68+
}
69+
}
70+
71+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"javaPackage": "com.example.swift",
33
"mode": "jni",
4-
"logLevel": ["debug"]
4+
"logLevel": "debug"
55
}

Sources/SwiftJavaConfigurationShared/Configuration.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@ public func readConfiguration(string: String, configPath: URL?, file: String = #
183183
decoder.allowsJSON5 = true
184184
return try decoder.decode(Configuration.self, from: configData)
185185
} catch {
186-
throw ConfigurationError(message: "Failed to parse SwiftJava configuration at '\(configPath.map({ $0.absoluteURL.description }) ?? "<no-path>")'! \(#fileID):\(#line)", error: error,
186+
throw ConfigurationError(
187+
message: "Failed to parse SwiftJava configuration at '\(configPath.map({ $0.absoluteURL.description }) ?? "<no-path>")'! \(#fileID):\(#line)",
188+
error: error,
189+
text: string,
187190
file: file, line: line)
188191
}
189192
}
@@ -276,31 +279,37 @@ extension Configuration {
276279
public struct ConfigurationError: Error {
277280
let message: String
278281
let error: any Error
282+
let text: String?
279283

280284
let file: String
281285
let line: UInt
282286

283-
init(message: String, error: any Error, file: String = #fileID, line: UInt = #line) {
287+
init(message: String, error: any Error, text: String?, file: String = #fileID, line: UInt = #line) {
284288
self.message = message
285289
self.error = error
290+
self.text = text
286291
self.file = file
287292
self.line = line
288293
}
289294
}
290295

291-
public enum LogLevel: String, Codable, Hashable {
296+
public enum LogLevel: String, ExpressibleByStringLiteral, Codable, Hashable {
292297
case trace = "trace"
293298
case debug = "debug"
294299
case info = "info"
295300
case notice = "notice"
296301
case warning = "warning"
297302
case error = "error"
298303
case critical = "critical"
304+
305+
public init(stringLiteral value: String) {
306+
self = LogLevel(rawValue: value) ?? .info
307+
}
299308
}
300309

301310
extension LogLevel {
302311
public init(from decoder: any Decoder) throws {
303-
var container = try decoder.unkeyedContainer()
312+
var container = try decoder.singleValueContainer()
304313
let string = try container.decode(String.self)
305314
switch string {
306315
case "trace": self = .trace

0 commit comments

Comments
 (0)