Skip to content

Commit 54381fe

Browse files
committed
BE: standarize import paths
1 parent a9bc82c commit 54381fe

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

api/src/main/java/io/kafbat/ui/serdes/builtin/ProtobufFileSerde.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import javax.annotation.Nullable;
6565
import lombok.SneakyThrows;
6666
import lombok.extern.slf4j.Slf4j;
67+
import org.apache.commons.lang3.SystemUtils;
6768
import org.jetbrains.annotations.NotNull;
6869

6970
@Slf4j
@@ -399,6 +400,7 @@ private Loader createFilesLoader(Map<String, ProtoFile> files) {
399400
return new Loader() {
400401
@Override
401402
public @NotNull ProtoFile load(@NotNull String path) {
403+
//path = path.replace("/","\\");
402404
return Preconditions.checkNotNull(files.get(path), "ProtoFile not found for import '%s'", path);
403405
}
404406

@@ -416,7 +418,7 @@ private Map<String, ProtoFile> loadFilesWithLocations() {
416418
files.filter(p -> !Files.isDirectory(p) && p.toString().endsWith(".proto"))
417419
.forEach(path -> {
418420
// relative path will be used as "import" statement
419-
String relativePath = baseLocation.relativize(path).toString();
421+
String relativePath = removeBackSlashes(baseLocation.relativize(path).toString());
420422
var protoFileElement = ProtoParser.Companion.parse(
421423
Location.get(baseLocation.toString(), relativePath),
422424
readFileAsString(path)
@@ -426,6 +428,24 @@ private Map<String, ProtoFile> loadFilesWithLocations() {
426428
}
427429
return filesByLocations;
428430
}
431+
432+
/**
433+
* Replaces backslashes in the given file path with forward slashes if the operating system is Windows.
434+
*
435+
* <p>This method is designed to standardize file paths by converting Windows-style backslashes (`\`)
436+
* to Linux/Unix-style forward slashes (`/`) when the application is running on a Windows OS.
437+
* On other operating systems, the input path is returned unchanged.</p>
438+
* This is needed because imports in Protobuf use forward slashes (`/`) for the imports
439+
*
440+
* @param path the file path to standardize; must not be {@code null}.
441+
* @return the standardized file path with forward slashes if running on Windows, or the original path otherwise.
442+
*/
443+
private @NotNull String removeBackSlashes(@NotNull final String path) {
444+
if (SystemUtils.IS_OS_WINDOWS) {
445+
return path.replace("\\", "/");
446+
}
447+
return path;
448+
}
429449
}
430450

431451
}

0 commit comments

Comments
 (0)