Skip to content
Merged
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
2 changes: 1 addition & 1 deletion ezyhttp-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
</parent>

<artifactId>ezyhttp-client</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
</parent>

<artifactId>ezyhttp-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
</parent>

<artifactId>ezyhttp-server-boot</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
</parent>

<artifactId>ezyhttp-server-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ezyhttp-server-graphql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyhttp</artifactId>
<version>1.5.3</version>
<version>1.5.4</version>
</parent>
<artifactId>ezyhttp-server-graphql</artifactId>
<name>ezyhttp-server-graphql</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,42 @@ public Map filter(
GraphQLField queryDefinition
) {
Map answer = new HashMap<>();
Map parentMap = null;
Deque<StackEntry> stack = new ArrayDeque<>();
stack.push(new StackEntry(queryDefinition, data));
stack.push(new StackEntry(queryDefinition, data, answer));
filterStack(stack);
return answer;
}

@SuppressWarnings({"rawtypes"})
public List<Map> filterList(
List<Map> dataList,
GraphQLField queryDefinition
) {
List<Map> answer = new LinkedList<>();
Deque<StackEntry> stack = new ArrayDeque<>();
pushListItems(
stack,
dataList,
queryDefinition,
answer,
queryDefinition.getName()
);
filterStack(stack);
return answer;
}

@SuppressWarnings({"rawtypes", "unchecked"})
private void filterStack(Deque<StackEntry> stack) {
while (!stack.isEmpty()) {
StackEntry entry = stack.pop();
String parentName = entry.field.getName();
parentMap = parentMap == null
? answer
: (Map) parentMap.get(parentName);

GraphQLField allField = entry.field.getField(ALL_FIELDS);
if (allField != null) {
if (allField != null && entry.data != null) {
Set<Map.Entry> entries = entry.data.entrySet();
for (Map.Entry e : entries) {
Object v = e.getValue();
if (v != null) {
parentMap.put(e.getKey(), v);
entry.output.put(e.getKey(), v);
}
}
}
Expand All @@ -53,47 +72,66 @@ public Map filter(
continue;
}
if (field.getFields().isEmpty()) {
parentMap.put(fieldName, value);
entry.output.put(fieldName, value);
continue;
}
if (value instanceof Map) {
Object newItem = new HashMap<>();
parentMap.put(fieldName, newItem);
stack.push(new StackEntry(field, (Map) value));
Map newItem = new HashMap<>();
entry.output.put(fieldName, newItem);
stack.push(new StackEntry(field, (Map) value, newItem));
} else if (value instanceof List) {
parentMap.put(
fieldName,
filterList((List) value, field)
List<Map> newList = new LinkedList<>();
entry.output.put(fieldName, newList);
pushListItems(
stack,
(List) value,
field,
newList,
fieldName
);
} else {
throw new GraphQLInvalidSchemeException(
EzyMapBuilder.mapBuilder()
.put("schema", "invalid")
.put("field", fieldName)
.toMap()
);
throw newInvalidSchemeException(fieldName);
}
}
}
return answer;
}

@SuppressWarnings({"rawtypes"})
public List<Map> filterList(
List<Map> dataList,
GraphQLField queryDefinition
@SuppressWarnings({"rawtypes", "unchecked"})
private void pushListItems(
Deque<StackEntry> stack,
List dataList,
GraphQLField queryDefinition,
List<Map> answer,
String fieldName
) {
List<Map> answer = new LinkedList<>();
for (Map map : dataList) {
answer.add(filter(map, queryDefinition));
int size = dataList.size();
for (int i = size - 1; i >= 0; --i) {
Object data = dataList.get(i);
if (!(data instanceof Map)) {
throw newInvalidSchemeException(fieldName);
}
Map item = new HashMap<>();
answer.add(0, item);
stack.push(new StackEntry(queryDefinition, (Map) data, item));
}
return answer;
}

private GraphQLInvalidSchemeException newInvalidSchemeException(
String fieldName
) {
return new GraphQLInvalidSchemeException(
EzyMapBuilder.mapBuilder()
.put("schema", "invalid")
.put("field", fieldName)
.toMap()
);
}

@AllArgsConstructor
@SuppressWarnings("rawtypes")
private static class StackEntry {
private GraphQLField field;
private Map data;
private Map output;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import com.tvd12.ezyhttp.core.codec.SingletonStringDeserializer;
import lombok.Getter;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -103,7 +105,7 @@ public <T> T getFieldArgumentValue(
) {
GraphQLField field = this;
for (String fieldName : fieldNames) {
field = fieldByName.get(fieldName);
field = field.getField(fieldName);
if (field == null) {
break;
}
Expand Down Expand Up @@ -147,6 +149,11 @@ public <T> T getFieldArgumentValue(
if (value == null) {
return null;
}
if (value instanceof String) {
return SingletonStringDeserializer
.getInstance()
.deserializeOrNull((String) value, type);
}
return (T) EzySingletonOutputTransformer
.getInstance()
.transform(value, type);
Expand All @@ -162,15 +169,48 @@ private static String toString(
Map<String, Object> arguments,
List<GraphQLField> fields
) {
StringBuilder builder = new StringBuilder()
.append(name);
StringBuilder builder = new StringBuilder();
Deque<ToStringTask> stack = new ArrayDeque<>();
stack.push(new ToStringTask(name, arguments, fields));
while (!stack.isEmpty()) {
ToStringTask task = stack.pop();
if (task.text != null) {
builder.append(task.text);
continue;
}
appendFieldStart(builder, task.name, task.arguments);
List<GraphQLField> taskFields = task.fields;
if (taskFields == null) {
continue;
}
builder.append(", [");
stack.push(new ToStringTask("]"));
for (int i = taskFields.size() - 1; i >= 0; --i) {
GraphQLField field = taskFields.get(i);
stack.push(
new ToStringTask(
field.name,
field.arguments,
field.fields
)
);
if (i > 0) {
stack.push(new ToStringTask(", "));
}
}
}
return builder.toString();
}

private static void appendFieldStart(
StringBuilder builder,
String name,
Map<String, Object> arguments
) {
builder.append(name);
if (arguments != null && !arguments.isEmpty()) {
builder.append("(").append(arguments).append(")");
}
if (fields != null) {
builder.append(", ").append(fields);
}
return builder.toString();
}

@Override
Expand Down Expand Up @@ -226,4 +266,29 @@ public String toString() {
return GraphQLField.toString(name, arguments, fields);
}
}

private static class ToStringTask {
private final String text;
private final String name;
private final Map<String, Object> arguments;
private final List<GraphQLField> fields;

ToStringTask(String text) {
this.text = text;
this.name = null;
this.arguments = null;
this.fields = null;
}

ToStringTask(
String name,
Map<String, Object> arguments,
List<GraphQLField> fields
) {
this.text = null;
this.name = name;
this.arguments = arguments;
this.fields = fields;
}
}
}
Loading
Loading