Skip to content
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

refactoring and fix test #6

Open
wants to merge 3 commits into
base: trocco-version
Choose a base branch
from
Open
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
36 changes: 6 additions & 30 deletions src/main/java/org/embulk/parser/jsonpath/JsonpathParserPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,10 @@ public int read()
skipOrThrow(new DataException(e), stopOnInvalidRecord);
continue;
}
Map<Column, JsonNode> additionalValues = createAdditionalColumns(jsonPathMap, rootNode);
if (json.isArray()) {
for (JsonNode recordValue : json) {
try {
createRecordFromJson(recordValue, schema, jsonPathMap, visitor, pageBuilder, additionalValues);
createRecordFromJson(rootNode, recordValue, schema, jsonPathMap, visitor, pageBuilder);
}
catch (DataException e) {
skipOrThrow(e, stopOnInvalidRecord);
Expand All @@ -195,7 +194,7 @@ public int read()
}
else {
try {
createRecordFromJson(json, schema, jsonPathMap, visitor, pageBuilder, additionalValues);
createRecordFromJson(rootNode, json, schema, jsonPathMap, visitor, pageBuilder);
}
catch (DataException e) {
skipOrThrow(e, stopOnInvalidRecord);
Expand All @@ -209,23 +208,6 @@ public int read()
}
}

private Map<Column, JsonNode> createAdditionalColumns(Map<Column, String> jsonPathMap, JsonNode rootNode)
{
Map<Column, JsonNode> additionalColumns = new HashMap<>();
jsonPathMap.forEach((column, path) -> {
if (path.startsWith("$")) {
try {
additionalColumns.put(
column,
JsonPath.using(JSON_PATH_CONFIG).parse(rootNode).read(path, JsonNode.class)
);
} catch (PathNotFoundException e) {
logger.warn("Failed to get %s", path);
}
}
});
return Collections.unmodifiableMap(additionalColumns);
}
private Map<Column, String> createJsonPathMap(PluginTask task, Schema schema)
{
Map<Column, String> columnMap = new HashMap<>();
Expand All @@ -239,7 +221,7 @@ private Map<Column, String> createJsonPathMap(PluginTask task, Schema schema)
return Collections.unmodifiableMap(columnMap);
}

private void createRecordFromJson(JsonNode json, Schema schema, Map<Column, String> jsonPathMap, ColumnVisitorImpl visitor, PageBuilder pageBuilder, Map<Column, JsonNode> additionalValues)
private void createRecordFromJson(JsonNode root, JsonNode json, Schema schema, Map<Column, String> jsonPathMap, ColumnVisitorImpl visitor, PageBuilder pageBuilder)
{
if (json.getNodeType() != JsonNodeType.OBJECT) {
throw new JsonRecordValidateException(format(Locale.ENGLISH,
Expand All @@ -248,27 +230,21 @@ private void createRecordFromJson(JsonNode json, Schema schema, Map<Column, Stri

for (Column column : schema.getColumns()) {
JsonNode value = null;
if (jsonPathMap.containsKey(column) && !jsonPathMap.get(column).startsWith("$")) {
if (jsonPathMap.containsKey(column)) {
try {
value = JsonPath.using(JSON_PATH_CONFIG).parse(json).read(jsonPathMap.get(column));
String path = jsonPathMap.get(column);
value = JsonPath.using(JSON_PATH_CONFIG).parse(path.startsWith("$") ? root : json).read(path, JsonNode.class);
}
catch (PathNotFoundException e) {
// pass (value is nullable)
}
}
else {
value = json.get(column.getName());

}
visitor.setValue(value);
column.visit(visitor);
}
additionalValues.forEach( (k, v) -> {
visitor.setValue(v);
k.visit(visitor);
}
);

pageBuilder.addRecord();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,11 @@ public void useJsonPath()
throws Exception
{
SchemaConfig schema = schema(
column("__c0", BOOLEAN, config().set("path", "$._c0")), column("__c1", LONG, config().set("path", "$._c1")),
column("__c2", DOUBLE, config().set("path", "$._c2")), column("__c3", STRING, config().set("path", "$._c3")),
column("__c4", TIMESTAMP, config().set("format", "%Y-%m-%d %H:%M:%S %Z").set("path", "$._c4")),
column("__c5", JSON, config().set("path", "$._c5")));
column("__c0", BOOLEAN, config().set("path", "_c0")), column("__c1", LONG, config().set("path", "_c1")),
column("__c2", DOUBLE, config().set("path", "_c2")), column("__c3", STRING, config().set("path", "_c3")),
column("__c4", TIMESTAMP, config().set("format", "%Y-%m-%d %H:%M:%S %Z").set("path", "_c4")),
column("__c5", JSON, config().set("path", "_c5")),
column("__c6", STRING, config().set("path", "$[0]._c3")));
ConfigSource config = this.config.deepCopy().set("columns", schema);

transaction(config, fileInput(
Expand All @@ -397,6 +398,7 @@ record = records.get(0);
assertEquals("embulk", record[3]);
assertEquals(Timestamp.ofEpochSecond(1451606400L), record[4]);
assertEquals(newMap(newString("k"), newString("v")), record[5]);
assertEquals("embulk", record[6]);
}
{
record = records.get(1);
Expand All @@ -406,6 +408,7 @@ record = records.get(1);
assertEquals("エンバルク", record[3]);
assertEquals(Timestamp.ofEpochSecond(1451606400L), record[4]);
assertEquals(newArray(newString("e0"), newString("e1")), record[5]);
assertEquals("embulk", record[6]);
}

recreatePageOutput();
Expand All @@ -416,10 +419,10 @@ public void writeNilsWithJsonPath()
throws Exception
{
SchemaConfig schema = schema(
column("__c0", BOOLEAN, config().set("path", "$._c0")), column("__c1", LONG, config().set("path", "$._c1")),
column("__c2", DOUBLE, config().set("path", "$._c2")), column("__c3", STRING, config().set("path", "$._c3")),
column("__c4", TIMESTAMP, config().set("format", "%Y-%m-%d %H:%M:%S %Z").set("path", "$._c4")),
column("__c5", JSON, config().set("path", "$._c5")));
column("__c0", BOOLEAN, config().set("path", "_c0")), column("__c1", LONG, config().set("path", "_c1")),
column("__c2", DOUBLE, config().set("path", "_c2")), column("__c3", STRING, config().set("path", "_c3")),
column("__c4", TIMESTAMP, config().set("format", "%Y-%m-%d %H:%M:%S %Z").set("path", "_c4")),
column("__c5", JSON, config().set("path", "_c5")), column("__c6", STRING, config().set("path", "$[0]._c3")));
ConfigSource config = this.config.deepCopy().set("columns", schema);

transaction(config, fileInput(
Expand All @@ -435,7 +438,7 @@ public void writeNilsWithJsonPath()
assertEquals(4, records.size());

for (Object[] record : records) {
for (int i = 0; i < 6; i++) {
for (int i = 0; i < 7; i++) {
assertNull(record[i]);
}
}
Expand All @@ -445,7 +448,7 @@ public void writeNilsWithJsonPath()
public void notArrayObject()
throws Exception
{
SchemaConfig schema = schema(column("__c0", STRING, config().set("path", "$._c0")));
SchemaConfig schema = schema(column("__c0", STRING, config().set("path", "_c0")));
ConfigSource config = this.config.deepCopy().set("columns", schema);

transaction(config, fileInput("{ \"_c0\": \"embulk\" }"));
Expand All @@ -459,7 +462,7 @@ public void notArrayObject()
public void rootPathJsonIsObject()
throws Exception
{
SchemaConfig schema = schema(column("__c0", STRING, config().set("path", "$._c0")));
SchemaConfig schema = schema(column("__c0", STRING, config().set("path", "_c0")));
ConfigSource config = this.config.deepCopy().set("columns", schema).set("root", "$[0].root");

transaction(config, fileInput(
Expand All @@ -477,7 +480,7 @@ public void rootPathJsonIsObject()
public void rootPathJsonIsNotArrayOrObject()
{
assertThrows(DataException.class, () -> {
SchemaConfig schema = schema(column("__c0", STRING, config().set("path", "$._c0")));
SchemaConfig schema = schema(column("__c0", STRING, config().set("path", "_c0")));
ConfigSource config = this.config.deepCopy()
.set("columns", schema)
.set("root", "$[0].root")
Expand Down
Loading