fix(compiler): enforce deterministic semantic facts in IR contract
Intrepid/Prometeu/Studio/pipeline/pr-master This commit looks good
Test / Tests and coverage
Intrepid/Prometeu/Studio/pipeline/head This commit looks good

This commit is contained in:
2026-11-11 09:34:51 +00:00
parent 1471b427f1
commit e1e13b47ac
3 changed files with 86 additions and 1 deletions
@@ -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