Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
eiiches committed Sep 26, 2021
1 parent f763e70 commit bb32c1c
Showing 1 changed file with 8 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -45,7 +46,7 @@ public FileSystemModuleLoader(final Scope parentScope, final Version version, fi
this.version = version;
}

private Pair<Path, byte[]> loadBytes(final String path, final String ext) {
private Pair<Path, byte[]> loadBytes(final String path, final String ext) throws IOException {
for (final Path searchPath : searchPaths) {
final Path modulePath = searchPath.getFileSystem().getPath(path);
if (modulePath.isAbsolute())
Expand All @@ -59,20 +60,16 @@ private Pair<Path, byte[]> loadBytes(final String path, final String ext) {
try {
final byte[] moduleBytes = Files.readAllBytes(moduleFilePath);
return Pair.of(moduleFilePath, moduleBytes);
} catch (FileNotFoundException e) {
} catch (FileNotFoundException | NoSuchFileException e) {
/* continue */
} catch (IOException e) {
throw new RuntimeException(e);
}

final Path moduleFilePath2 = resolvedPath.resolve(resolvedPath.getFileName() + "." + ext);
try {
final byte[] moduleBytes = Files.readAllBytes(moduleFilePath2);
return Pair.of(moduleFilePath2, moduleBytes);
} catch (FileNotFoundException e) {
} catch (FileNotFoundException | NoSuchFileException e) {
/* continue */
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return null;
Expand All @@ -81,7 +78,7 @@ private Pair<Path, byte[]> loadBytes(final String path, final String ext) {
private final ConcurrentHashMap<String, TryOnce<Module>> loadedModules = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, TryOnce<JsonNode>> loadedData = new ConcurrentHashMap<>();

private Module loadModuleActual(final String path) throws JsonQueryException {
private Module loadModuleActual(final String path) throws IOException {
final Pair<Path, byte[]> moduleBytes = loadBytes(path, "jq");
if (moduleBytes == null)
return null;
Expand Down Expand Up @@ -160,23 +157,18 @@ public JsonNode loadData(final String path) throws JsonQueryException {
return loadDataActual(path);
});
} catch (CompletionException e) {
throw new JsonQueryException(String.format("failed to load data %s", path), e);
throw new JsonQueryException(String.format("failed to load data %s: %s", path, e.getCause().getMessage()), e);
}
}

private JsonNode loadDataActual(final String path) throws JsonQueryException {
private JsonNode loadDataActual(final String path) throws IOException {
final Pair<Path, byte[]> bytes = loadBytes(path, "json");
if (bytes == null)
return null;

final ArrayNode data = MAPPER.createArrayNode();

MappingIterator<JsonNode> iter;
try {
iter = MAPPER.readValues(MAPPER.getFactory().createParser(bytes._2), JsonNode.class);
} catch (IOException e) {
throw new JsonQueryException(e);
}
final MappingIterator<JsonNode> iter = MAPPER.readValues(MAPPER.getFactory().createParser(bytes._2), JsonNode.class);
while (iter.hasNext())
data.add(iter.next());

Expand Down

0 comments on commit bb32c1c

Please sign in to comment.