fix(compiler): enforce deterministic semantic facts in IR contract
This commit is contained in:
@@ -10,6 +10,28 @@ public final class ImmutableJson {
|
||||
if (source != null) source.forEach((key, value) -> result.put(Objects.requireNonNull(key), value(value)));
|
||||
return Collections.unmodifiableMap(result);
|
||||
}
|
||||
/** Copies JSON data with sorted object keys at every depth and ordered arrays only. */
|
||||
public static Map<String, Object> canonicalObject(Map<String, ?> source) {
|
||||
return canonicalMap(source == null ? Map.of() : source);
|
||||
}
|
||||
private static Map<String, Object> canonicalMap(Map<?, ?> source) {
|
||||
var result = new TreeMap<String, Object>();
|
||||
source.forEach((key, value) -> {
|
||||
if (!(key instanceof String name)) {
|
||||
throw new IllegalArgumentException("Expected JSON object key to be a string");
|
||||
}
|
||||
result.put(name, canonicalValue(value));
|
||||
});
|
||||
return Collections.unmodifiableMap(result);
|
||||
}
|
||||
private static Object canonicalValue(Object source) {
|
||||
if (source instanceof Map<?, ?> values) return canonicalMap(values);
|
||||
if (source instanceof List<?> values) return values.stream().map(ImmutableJson::canonicalValue).toList();
|
||||
if (source instanceof Collection<?>) {
|
||||
throw new IllegalArgumentException("Expected ordered JSON array as a list");
|
||||
}
|
||||
return value(source);
|
||||
}
|
||||
public static Object value(Object source) {
|
||||
if (source == null || source instanceof String || source instanceof Boolean || source instanceof Byte
|
||||
|| source instanceof Short || source instanceof Integer || source instanceof Long || source instanceof Float
|
||||
|
||||
Reference in New Issue
Block a user