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

Use cleaner impl for query tags deser #120

Merged
merged 2 commits into from
Aug 26, 2024
Merged
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
20 changes: 10 additions & 10 deletions src/main/java/com/fauna/codec/json/QueryTagsDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

import java.io.IOException;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class QueryTagsDeserializer extends JsonDeserializer<Map<String, String>> {
@Override
Expand All @@ -19,18 +21,16 @@ public Map<String,String> deserialize(JsonParser jp, DeserializationContext dese
case VALUE_NULL:
return null;
case VALUE_STRING:
var raw = jp.getValueAsString();
Map<String, String> ret = new HashMap<>();
if (raw.isEmpty()) {
return ret;
}
var rawString = jp.getValueAsString();

String[] tagPairs = jp.getValueAsString().split(",");
for (String tagPair : tagPairs) {
String[] tokens = tagPair.split("=");
ret.put(tokens[0], tokens[1]);
if (rawString.isEmpty()) {
return Map.of();
}
return ret;

return Arrays.stream(rawString.split(","))
.map(queryTag -> queryTag.split("="))
.filter(parts -> parts.length >= 2)
.collect(Collectors.toMap(t -> t[0], t -> t[1]));
default:
throw new JsonParseException(jp, MessageFormat.format("Unexpected token `{0}` deserializing query tags", jp.currentToken()));
}
Expand Down
Loading