mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(appsync): Phase 3 — $util runtime library for VTL resolvers (#1223)
* test(appsync): add remaining SDK tests and fix listDomainNames field name
- Add 4 SDK tests: getIntrospectionSchema, listDomainNames, getFunction, listResolversByFunction
- Fix listDomainNames response field from 'domainNames' to 'domainNameConfigs' (AWS SDK v2 expects domainNameConfigs)
- Update integration test assertion to match corrected field name
* feat(appsync): add $util runtime library for VTL resolvers
Standalone utility library matching AWS AppSync $util namespace.
Validated against official AWS AppSync documentation.
Classes:
- VtlErrorSignal: exception for $util.error() and $util.unauthorized()
- AppSyncUtil: top-level $util with error/validate/type-check methods
- StrUtil: $util.str (toUpper, toLower, toReplace, normalize)
- TimeUtil: $util.time (12 methods, epochMilliSeconds-based per AWS)
- MathUtil: $util.math (roundNum, minVal, maxVal, randomDouble, randomWithinRange)
- DynamoDbUtil: $util.dynamodb (24 methods: value + JSON variants)
- TransformUtil: $util.transform (DynamoDB filter, Elasticsearch DSL, subscription filter)
- ListUtil: $util.list (copyAndRetainAll, copyAndRemoveAll, sortList)
- MapUtil: $util.map (copyAndRetainAllKeys, copyAndRemoveAllKeys)
* test(appsync): add unit tests for $util runtime library
234 test cases covering all $util methods with edge cases:
- AppSyncUtilTest: root methods, error, validate, type checks (85 tests)
- DynamoDbUtilTest: DynamoDB attribute value conversion (47 tests)
- StrUtilTest: string operations (21 tests)
- TimeUtilTest: time operations (21 tests)
- MathUtilTest: math operations (19 tests)
- ListUtilTest: list operations (17 tests)
- TransformUtilTest: filter/DSL conversion (12 tests)
- MapUtilTest: map operations (12 tests)
* fix(appsync): fix DynamoDbUtil *Json methods and refactor utils
- Fix 10 *Json methods to return JSON String instead of Map
- Add private helpers: toJson(Map), base64Encode(String) to DynamoDbUtil
- Extract parseInput(Object) helper in TransformUtil to remove duplication
- Chain qr() to quiet() in AppSyncUtil
- Chain validate() overloads in AppSyncUtil
- Update tests to verify *Json methods return correct JSON strings
* fix(appsync): address review feedback on $util methods
- toMapValues: return inner map without M wrapper (matches AWS: toMapValues = toMap.get("M"))
- toBinary/toBinarySet: wrap input as-is, don't re-encode base64
- str.normalize: use Unicode normalization (NFC/NFD/NFKC/NFKD) instead of whitespace collapse
- typeOf: return exact AWS values (Null/Number/String/Map/List/Boolean/Object)
- unauthorized: errorType 'Unauthorized' not 'UnauthorizedException'
- nowISO8601: always include 3 fractional digits per AWS format
- Mark transform.* methods as not-yet-implemented (complex expression building)
- Update all affected tests
* fix(appsync): change default errorType from UnknownError to Unknown
Research confirms AWS AppSync uses 'Unknown' as default errorType when
$util.error(message) is called without errorType argument.
* chore(appsync): remove dead base64Encode helper from DynamoDbUtil
This commit is contained in:
1 parent
61c3a07199
commit
e20f7d83d2
20 files changed
+2402
-2
No files matched your search
@@ -572,7 +572,7 @@ public class AppSyncController {
|
||||
@QueryParam("nextToken") String nextToken) {
|
||||
var page = service.listDomainNames(maxResults, nextToken);
|
||||
ObjectNode root = objectMapper.createObjectNode();
|
||||
ArrayNode items = root.putArray("domainNames");
|
||||
ArrayNode items = root.putArray("domainNameConfigs");
|
||||
page.items().forEach(items::addPOJO);
|
||||
if (page.nextToken() != null) {
|
||||
root.put("nextToken", page.nextToken());
|
||||
|
||||
+233
@@ -0,0 +1,233 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class AppSyncUtil {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
private final StrUtil strUtil;
|
||||
private final TimeUtil timeUtil;
|
||||
private final MathUtil mathUtil;
|
||||
private final DynamoDbUtil dynamoDbUtil;
|
||||
private final TransformUtil transformUtil;
|
||||
private final ListUtil listUtil;
|
||||
private final MapUtil mapUtil;
|
||||
|
||||
public AppSyncUtil(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
this.strUtil = new StrUtil();
|
||||
this.timeUtil = new TimeUtil();
|
||||
this.mathUtil = new MathUtil();
|
||||
this.dynamoDbUtil = new DynamoDbUtil(objectMapper);
|
||||
this.transformUtil = new TransformUtil(objectMapper);
|
||||
this.listUtil = new ListUtil();
|
||||
this.mapUtil = new MapUtil();
|
||||
}
|
||||
|
||||
public StrUtil getStr() { return strUtil; }
|
||||
public TimeUtil getTime() { return timeUtil; }
|
||||
public MathUtil getMath() { return mathUtil; }
|
||||
public DynamoDbUtil getDynamodb() { return dynamoDbUtil; }
|
||||
public TransformUtil getTransform() { return transformUtil; }
|
||||
public ListUtil getList() { return listUtil; }
|
||||
public MapUtil getMap() { return mapUtil; }
|
||||
|
||||
public String escapeJavaScript(String s) {
|
||||
if (s == null) return "";
|
||||
StringBuilder sb = new StringBuilder(s.length() + 16);
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
switch (c) {
|
||||
case '\\' -> sb.append("\\\\");
|
||||
case '"' -> sb.append("\\\"");
|
||||
case '\'' -> sb.append("\\'");
|
||||
case '/' -> sb.append("\\/");
|
||||
case '\b' -> sb.append("\\b");
|
||||
case '\t' -> sb.append("\\t");
|
||||
case '\n' -> sb.append("\\n");
|
||||
case '\f' -> sb.append("\\f");
|
||||
case '\r' -> sb.append("\\r");
|
||||
default -> {
|
||||
if (c < 0x20 || c > 0x7E) {
|
||||
sb.append("\\u").append(String.format("%04x", (int) c));
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public String urlEncode(String s) {
|
||||
if (s == null) return "";
|
||||
return URLEncoder.encode(s, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public String urlDecode(String s) {
|
||||
if (s == null) return "";
|
||||
return URLDecoder.decode(s, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public String base64Encode(byte[] data) {
|
||||
if (data == null) return "";
|
||||
return Base64.getEncoder().encodeToString(data);
|
||||
}
|
||||
|
||||
public String base64Decode(String s) {
|
||||
if (s == null) return "";
|
||||
return new String(Base64.getDecoder().decode(s), StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public Object parseJson(String s) {
|
||||
if (s == null || s.isEmpty()) return Map.of();
|
||||
try {
|
||||
return objectMapper.readValue(s, Object.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
return Map.of();
|
||||
}
|
||||
}
|
||||
|
||||
public String toJson(Object o) {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(o);
|
||||
} catch (JsonProcessingException e) {
|
||||
return "null";
|
||||
}
|
||||
}
|
||||
|
||||
public String autoId() {
|
||||
return UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
public String quiet(String s) {
|
||||
return "";
|
||||
}
|
||||
|
||||
public String qr(String s) {
|
||||
return quiet(s);
|
||||
}
|
||||
|
||||
public boolean matches(String pattern, String value) {
|
||||
if (pattern == null || value == null) return false;
|
||||
return Pattern.matches(pattern, value);
|
||||
}
|
||||
|
||||
public Object defaultIfNull(Object value, Object defaultValue) {
|
||||
return value != null ? value : defaultValue;
|
||||
}
|
||||
|
||||
public String defaultIfNullOrEmpty(String value, String defaultValue) {
|
||||
if (value == null || value.isEmpty()) return defaultValue;
|
||||
return value;
|
||||
}
|
||||
|
||||
public String defaultIfNullOrBlank(String value, String defaultValue) {
|
||||
if (value == null || value.isBlank()) return defaultValue;
|
||||
return value;
|
||||
}
|
||||
|
||||
public boolean isNull(Object value) {
|
||||
return value == null;
|
||||
}
|
||||
|
||||
public boolean isNullOrEmpty(String value) {
|
||||
return value == null || value.isEmpty();
|
||||
}
|
||||
|
||||
public boolean isNullOrBlank(String value) {
|
||||
return value == null || value.isBlank();
|
||||
}
|
||||
|
||||
public boolean isString(Object value) {
|
||||
return value instanceof String;
|
||||
}
|
||||
|
||||
public boolean isNumber(Object value) {
|
||||
return value instanceof Number;
|
||||
}
|
||||
|
||||
public boolean isBoolean(Object value) {
|
||||
return value instanceof Boolean;
|
||||
}
|
||||
|
||||
public boolean isList(Object value) {
|
||||
return value instanceof List<?>;
|
||||
}
|
||||
|
||||
public boolean isMap(Object value) {
|
||||
return value instanceof Map<?, ?>;
|
||||
}
|
||||
|
||||
public String typeOf(Object value) {
|
||||
if (value == null) return "Null";
|
||||
if (value instanceof String) return "String";
|
||||
if (value instanceof Boolean) return "Boolean";
|
||||
if (value instanceof Number) return "Number";
|
||||
if (value instanceof List<?>) return "List";
|
||||
if (value instanceof Map<?, ?>) return "Map";
|
||||
return "Object";
|
||||
}
|
||||
|
||||
public String authType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void error(String message) {
|
||||
throw new VtlErrorSignal(message, "Unknown", null, null);
|
||||
}
|
||||
|
||||
public void error(String message, String errorType) {
|
||||
throw new VtlErrorSignal(message, errorType, null, null);
|
||||
}
|
||||
|
||||
public void error(String message, String errorType, Object data) {
|
||||
throw new VtlErrorSignal(message, errorType, data, null);
|
||||
}
|
||||
|
||||
public void error(String message, String errorType, Object data, Object errorInfo) {
|
||||
throw new VtlErrorSignal(message, errorType, data, errorInfo);
|
||||
}
|
||||
|
||||
public void appendError(String message) {
|
||||
// In VTL context, this adds to $context.error without halting
|
||||
// For standalone usage, this is a no-op
|
||||
}
|
||||
|
||||
public void appendError(String message, String errorType) {
|
||||
// No-op for standalone usage
|
||||
}
|
||||
|
||||
public void appendError(String message, String errorType, Object data) {
|
||||
// No-op for standalone usage
|
||||
}
|
||||
|
||||
public void appendError(String message, String errorType, Object data, Object errorInfo) {
|
||||
// No-op for standalone usage
|
||||
}
|
||||
|
||||
public void unauthorized() {
|
||||
throw new VtlErrorSignal("Not Authorized", "Unauthorized", null, null);
|
||||
}
|
||||
|
||||
public void validate(boolean condition, String message) {
|
||||
validate(condition, message, "Unknown");
|
||||
}
|
||||
|
||||
public void validate(boolean condition, String message, String errorType) {
|
||||
validate(condition, message, errorType, null);
|
||||
}
|
||||
|
||||
public void validate(boolean condition, String message, String errorType, Object data) {
|
||||
if (!condition) {
|
||||
throw new VtlErrorSignal(message, errorType, data, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class DynamoDbUtil {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public DynamoDbUtil(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public Map<String, Object> toDynamoDB(Object value) {
|
||||
if (value == null) {
|
||||
return Collections.singletonMap("NULL", true);
|
||||
}
|
||||
if (value instanceof String s) {
|
||||
return Collections.singletonMap("S", s);
|
||||
}
|
||||
if (value instanceof Boolean b) {
|
||||
return Collections.singletonMap("BOOL", b);
|
||||
}
|
||||
if (value instanceof Number n) {
|
||||
return Collections.singletonMap("N", n.toString());
|
||||
}
|
||||
if (value instanceof List<?> list) {
|
||||
List<Object> converted = new ArrayList<>(list.size());
|
||||
for (Object item : list) {
|
||||
converted.add(toDynamoDB(item));
|
||||
}
|
||||
return Collections.singletonMap("L", converted);
|
||||
}
|
||||
if (value instanceof Map<?, ?> map) {
|
||||
Map<String, Object> converted = new LinkedHashMap<>(map.size());
|
||||
for (Map.Entry<?, ?> entry : map.entrySet()) {
|
||||
converted.put(entry.getKey().toString(), toDynamoDB(entry.getValue()));
|
||||
}
|
||||
return Collections.singletonMap("M", converted);
|
||||
}
|
||||
return Collections.singletonMap("S", value.toString());
|
||||
}
|
||||
|
||||
public String toDynamoDBJson(Object value) {
|
||||
return toJson(toDynamoDB(value));
|
||||
}
|
||||
|
||||
public Map<String, Object> toString(String value) {
|
||||
return Collections.singletonMap("S", value);
|
||||
}
|
||||
|
||||
public String toStringJson(String value) {
|
||||
return toJson(toString(value));
|
||||
}
|
||||
|
||||
public Map<String, Object> toNumber(Number value) {
|
||||
return Collections.singletonMap("N", value.toString());
|
||||
}
|
||||
|
||||
public String toNumberJson(Number value) {
|
||||
return toJson(toNumber(value));
|
||||
}
|
||||
|
||||
public Map<String, Object> toBinary(String value) {
|
||||
return Collections.singletonMap("B", value);
|
||||
}
|
||||
|
||||
public String toBinaryJson(String value) {
|
||||
return toJson(toBinary(value));
|
||||
}
|
||||
|
||||
public Map<String, Object> toBoolean(Boolean value) {
|
||||
return Collections.singletonMap("BOOL", value);
|
||||
}
|
||||
|
||||
public String toBooleanJson(Boolean value) {
|
||||
return toJson(toBoolean(value));
|
||||
}
|
||||
|
||||
public Map<String, Object> toNull() {
|
||||
return Collections.singletonMap("NULL", true);
|
||||
}
|
||||
|
||||
public String toNullJson() {
|
||||
return toJson(toNull());
|
||||
}
|
||||
|
||||
public Map<String, Object> toList(List<?> value) {
|
||||
List<Object> converted = new ArrayList<>(value.size());
|
||||
for (Object item : value) {
|
||||
converted.add(toDynamoDB(item));
|
||||
}
|
||||
return Collections.singletonMap("L", converted);
|
||||
}
|
||||
|
||||
public String toListJson(List<?> value) {
|
||||
return toJson(toList(value));
|
||||
}
|
||||
|
||||
public Map<String, Object> toMap(Map<String, Object> value) {
|
||||
Map<String, Object> converted = new LinkedHashMap<>(value.size());
|
||||
for (Map.Entry<String, Object> entry : value.entrySet()) {
|
||||
converted.put(entry.getKey(), toDynamoDB(entry.getValue()));
|
||||
}
|
||||
return Collections.singletonMap("M", converted);
|
||||
}
|
||||
|
||||
public String toMapJson(Map<String, Object> value) {
|
||||
return toJson(toMap(value));
|
||||
}
|
||||
|
||||
public Map<String, Object> toMapValues(Map<String, Object> value) {
|
||||
Map<String, Object> converted = new LinkedHashMap<>(value.size());
|
||||
for (Map.Entry<String, Object> entry : value.entrySet()) {
|
||||
converted.put(entry.getKey(), toDynamoDB(entry.getValue()));
|
||||
}
|
||||
return converted;
|
||||
}
|
||||
|
||||
public String toMapValuesJson(Map<String, Object> value) {
|
||||
return toJson(toMapValues(value));
|
||||
}
|
||||
|
||||
public Map<String, Object> toStringSet(List<String> value) {
|
||||
return Collections.singletonMap("SS", new ArrayList<>(value));
|
||||
}
|
||||
|
||||
public String toStringSetJson(List<String> value) {
|
||||
return toJson(toStringSet(value));
|
||||
}
|
||||
|
||||
public Map<String, Object> toNumberSet(List<? extends Number> value) {
|
||||
List<String> strings = new ArrayList<>(value.size());
|
||||
for (Number n : value) {
|
||||
strings.add(n.toString());
|
||||
}
|
||||
return Collections.singletonMap("NS", strings);
|
||||
}
|
||||
|
||||
public String toNumberSetJson(List<? extends Number> value) {
|
||||
return toJson(toNumberSet(value));
|
||||
}
|
||||
|
||||
public Map<String, Object> toBinarySet(List<String> value) {
|
||||
return Collections.singletonMap("BS", new ArrayList<>(value));
|
||||
}
|
||||
|
||||
public String toBinarySetJson(List<String> value) {
|
||||
return toJson(toBinarySet(value));
|
||||
}
|
||||
|
||||
private String toJson(Map<String, Object> map) {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(map);
|
||||
} catch (JsonProcessingException e) {
|
||||
return "{}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ListUtil {
|
||||
|
||||
public List<Object> copyAndRetainAll(List<?> list, List<?> keep) {
|
||||
if (list == null || keep == null) {
|
||||
return list != null ? new ArrayList<>(list) : new ArrayList<>();
|
||||
}
|
||||
Set<Object> keepSet = new HashSet<>(keep);
|
||||
return list.stream()
|
||||
.filter(keepSet::contains)
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
}
|
||||
|
||||
public List<Object> copyAndRemoveAll(List<?> list, List<?> remove) {
|
||||
if (list == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
if (remove == null) {
|
||||
return new ArrayList<>(list);
|
||||
}
|
||||
Set<Object> removeSet = new HashSet<>(remove);
|
||||
return list.stream()
|
||||
.filter(item -> !removeSet.contains(item))
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Object> sortList(List<?> list, boolean descending, String fieldName) {
|
||||
if (list == null || list.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
List<Object> sorted = new ArrayList<>(list);
|
||||
if (fieldName == null || fieldName.isEmpty()) {
|
||||
sorted.sort((a, b) -> compareObjects(a, b, descending));
|
||||
} else {
|
||||
sorted.sort((a, b) -> {
|
||||
Object valA = getFieldValue(a, fieldName);
|
||||
Object valB = getFieldValue(b, fieldName);
|
||||
return compareObjects(valA, valB, descending);
|
||||
});
|
||||
}
|
||||
return sorted;
|
||||
}
|
||||
|
||||
private int compareObjects(Object a, Object b, boolean descending) {
|
||||
int result;
|
||||
if (a == null && b == null) {
|
||||
result = 0;
|
||||
} else if (a == null) {
|
||||
result = -1;
|
||||
} else if (b == null) {
|
||||
result = 1;
|
||||
} else if (a instanceof Comparable<?> && b.getClass().isAssignableFrom(a.getClass())) {
|
||||
result = ((Comparable<Object>) a).compareTo(b);
|
||||
} else {
|
||||
result = a.toString().compareTo(b.toString());
|
||||
}
|
||||
return descending ? -result : result;
|
||||
}
|
||||
|
||||
private Object getFieldValue(Object obj, String fieldName) {
|
||||
if (obj instanceof Map<?, ?> map) {
|
||||
return map.get(fieldName);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MapUtil {
|
||||
|
||||
public Map<String, Object> copyAndRetainAllKeys(Map<String, Object> map, List<?> keys) {
|
||||
if (map == null || keys == null) {
|
||||
return map != null ? new LinkedHashMap<>(map) : new LinkedHashMap<>();
|
||||
}
|
||||
Set<Object> keySet = new HashSet<>(keys);
|
||||
return map.entrySet().stream()
|
||||
.filter(entry -> keySet.contains(entry.getKey()))
|
||||
.collect(Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
Map.Entry::getValue,
|
||||
(a, b) -> a,
|
||||
LinkedHashMap::new));
|
||||
}
|
||||
|
||||
public Map<String, Object> copyAndRemoveAllKeys(Map<String, Object> map, List<?> keys) {
|
||||
if (map == null) {
|
||||
return new LinkedHashMap<>();
|
||||
}
|
||||
if (keys == null) {
|
||||
return new LinkedHashMap<>(map);
|
||||
}
|
||||
Set<Object> keySet = new HashSet<>(keys);
|
||||
return map.entrySet().stream()
|
||||
.filter(entry -> !keySet.contains(entry.getKey()))
|
||||
.collect(Collectors.toMap(
|
||||
Map.Entry::getKey,
|
||||
Map.Entry::getValue,
|
||||
(a, b) -> a,
|
||||
LinkedHashMap::new));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class MathUtil {
|
||||
|
||||
public Integer roundNum(Double value) {
|
||||
if (value == null) return 0;
|
||||
return BigDecimal.valueOf(value).setScale(0, RoundingMode.HALF_UP).intValue();
|
||||
}
|
||||
|
||||
public Double minVal(Double a, Double b) {
|
||||
if (a == null) return b != null ? b : 0.0;
|
||||
if (b == null) return a;
|
||||
return Math.min(a, b);
|
||||
}
|
||||
|
||||
public Double maxVal(Double a, Double b) {
|
||||
if (a == null) return b != null ? b : 0.0;
|
||||
if (b == null) return a;
|
||||
return Math.max(a, b);
|
||||
}
|
||||
|
||||
public Double randomDouble() {
|
||||
return ThreadLocalRandom.current().nextDouble();
|
||||
}
|
||||
|
||||
public Integer randomWithinRange(Integer min, Integer max) {
|
||||
if (min == null || max == null) return 0;
|
||||
return ThreadLocalRandom.current().nextInt(min, max + 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import java.text.Normalizer;
|
||||
|
||||
public class StrUtil {
|
||||
|
||||
public String toUpper(String s) {
|
||||
if (s == null) return "";
|
||||
return s.toUpperCase();
|
||||
}
|
||||
|
||||
public String toLower(String s) {
|
||||
if (s == null) return "";
|
||||
return s.toLowerCase();
|
||||
}
|
||||
|
||||
public String toReplace(String s, String target, String replacement) {
|
||||
if (s == null || target == null || replacement == null) return s != null ? s : "";
|
||||
return s.replace(target, replacement);
|
||||
}
|
||||
|
||||
public String normalize(String s, String form) {
|
||||
if (s == null) return "";
|
||||
if (form == null) form = "nfc";
|
||||
return Normalizer.normalize(s, Normalizer.Form.valueOf(form.toUpperCase()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeFormatterBuilder;
|
||||
import java.time.temporal.ChronoField;
|
||||
|
||||
public class TimeUtil {
|
||||
|
||||
private static final DateTimeFormatter ISO_FORMATTER = new DateTimeFormatterBuilder()
|
||||
.appendPattern("yyyy-MM-dd'T'HH:mm:ss")
|
||||
.appendFraction(ChronoField.MILLI_OF_SECOND, 3, 3, true)
|
||||
.appendOffsetId()
|
||||
.toFormatter()
|
||||
.withZone(ZoneOffset.UTC);
|
||||
|
||||
public String nowISO8601() {
|
||||
return ISO_FORMATTER.format(Instant.now());
|
||||
}
|
||||
|
||||
public long nowEpochSeconds() {
|
||||
return Instant.now().getEpochSecond();
|
||||
}
|
||||
|
||||
public long nowEpochMilliSeconds() {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public String nowFormatted(String format) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
||||
return formatter.format(ZonedDateTime.now(ZoneOffset.UTC));
|
||||
}
|
||||
|
||||
public String nowFormatted(String format, String offset) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
||||
ZoneOffset zoneOffset = ZoneOffset.of(offset);
|
||||
return formatter.format(ZonedDateTime.now(zoneOffset));
|
||||
}
|
||||
|
||||
public long parseFormattedToEpochMilliSeconds(String timestamp, String format) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
||||
var parsed = java.time.LocalDateTime.parse(timestamp, formatter);
|
||||
return parsed.toInstant(ZoneOffset.UTC).toEpochMilli();
|
||||
}
|
||||
|
||||
public long parseFormattedToEpochMilliSeconds(String timestamp, String format, String timezone) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
||||
ZoneOffset zoneOffset = ZoneOffset.of(timezone);
|
||||
var parsed = java.time.LocalDateTime.parse(timestamp, formatter);
|
||||
return parsed.toInstant(zoneOffset).toEpochMilli();
|
||||
}
|
||||
|
||||
public long parseISO8601ToEpochMilliSeconds(String timestamp) {
|
||||
return Instant.parse(timestamp).toEpochMilli();
|
||||
}
|
||||
|
||||
public long epochMilliSecondsToSeconds(long epochMilliSeconds) {
|
||||
return epochMilliSeconds / 1000;
|
||||
}
|
||||
|
||||
public String epochMilliSecondsToISO8601(long epochMilliSeconds) {
|
||||
return ISO_FORMATTER.format(Instant.ofEpochMilli(epochMilliSeconds));
|
||||
}
|
||||
|
||||
public String epochMilliSecondsToFormatted(long epochMilliSeconds, String format) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
||||
ZonedDateTime zdt = Instant.ofEpochMilli(epochMilliSeconds).atZone(ZoneOffset.UTC);
|
||||
return formatter.format(zdt);
|
||||
}
|
||||
|
||||
public String epochMilliSecondsToFormatted(long epochMilliSeconds, String format, String offset) {
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
||||
ZoneOffset zoneOffset = ZoneOffset.of(offset);
|
||||
ZonedDateTime zdt = Instant.ofEpochMilli(epochMilliSeconds).atZone(zoneOffset);
|
||||
return formatter.format(zdt);
|
||||
}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class TransformUtil {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public TransformUtil(ObjectMapper objectMapper) {
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public Map<String, Object> toDynamoDBFilterExpression(Object input) {
|
||||
Map<String, Object> source = parseInput(input);
|
||||
if (source.isEmpty()) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
if (source.containsKey("expression")) {
|
||||
result.put("FilterExpression", source.get("expression"));
|
||||
}
|
||||
if (source.containsKey("expressionNames")) {
|
||||
result.put("ExpressionAttributeNames", source.get("expressionNames"));
|
||||
}
|
||||
if (source.containsKey("expressionValues")) {
|
||||
result.put("ExpressionAttributeValues", source.get("expressionValues"));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Map<String, Object> toElasticsearchQueryDSL(Object input) {
|
||||
return parseInput(input);
|
||||
}
|
||||
|
||||
public Map<String, Object> toSubscriptionFilter(Object input) {
|
||||
return parseInput(input);
|
||||
}
|
||||
|
||||
public Map<String, Object> toSubscriptionFilter(Object input, List<String> filterKeys) {
|
||||
Map<String, Object> result = new HashMap<>(toSubscriptionFilter(input));
|
||||
if (filterKeys != null && !filterKeys.isEmpty()) {
|
||||
result.put("filterKeys", filterKeys);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Map<String, Object> toSubscriptionFilter(Object input, List<String> filterKeys,
|
||||
Map<String, String> headerOverrides) {
|
||||
Map<String, Object> result = new HashMap<>(toSubscriptionFilter(input, filterKeys));
|
||||
if (headerOverrides != null && !headerOverrides.isEmpty()) {
|
||||
result.put("headerOverrides", headerOverrides);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String, Object> parseInput(Object input) {
|
||||
if (input == null) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
if (input instanceof Map<?, ?> map) {
|
||||
return (Map<String, Object>) map;
|
||||
}
|
||||
if (input instanceof String s) {
|
||||
try {
|
||||
return objectMapper.readValue(s, Map.class);
|
||||
} catch (Exception e) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
}
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package io.github.hectorvent.floci.services.appsync.graphql.util;
|
||||
|
||||
public class VtlErrorSignal extends RuntimeException {
|
||||
|
||||
private final String errorType;
|
||||
private final Object data;
|
||||
private final Object errorInfo;
|
||||
|
||||
public VtlErrorSignal(String message, String errorType, Object data, Object errorInfo) {
|
||||
super(message);
|
||||
this.errorType = errorType;
|
||||
this.data = data;
|
||||
this.errorInfo = errorInfo;
|
||||
}
|
||||
|
||||
public String getErrorType() {
|
||||
return errorType;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public Object getErrorInfo() {
|
||||
return errorInfo;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user