diff --git a/src/com/annimon/ownlang/utils/ModulesInfoCreator.java b/src/com/annimon/ownlang/utils/ModulesInfoCreator.java index 6523d0c..6e6a91b 100644 --- a/src/com/annimon/ownlang/utils/ModulesInfoCreator.java +++ b/src/com/annimon/ownlang/utils/ModulesInfoCreator.java @@ -2,15 +2,21 @@ package com.annimon.ownlang.utils; import com.annimon.ownlang.annotations.Modules; import com.annimon.ownlang.lib.Functions; +import com.annimon.ownlang.lib.MapValue; import com.annimon.ownlang.lib.Types; import com.annimon.ownlang.lib.Value; import com.annimon.ownlang.lib.Variables; import com.annimon.ownlang.lib.modules.Module; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.json.JSONArray; import org.json.JSONObject; @@ -32,6 +38,7 @@ public final class ModulesInfoCreator { final ModuleInfo moduleInfo = new ModuleInfo(moduleClass.getSimpleName()); moduleInfo.functions.addAll(Functions.getFunctions().keySet()); moduleInfo.constants.putAll(Variables.variables()); + moduleInfo.types.addAll(listValues(moduleClass)); moduleInfos.add(moduleInfo); } @@ -52,15 +59,30 @@ public final class ModulesInfoCreator { ); } + private static List listValues(Class moduleClass) { + return Arrays.stream(moduleClass.getDeclaredClasses()) + .filter(clazz -> getAllInterfaces(clazz).stream().anyMatch(i -> i.equals(Value.class))) + .map(Class::getSimpleName) + .collect(Collectors.toList()); + } + + private static Set getAllInterfaces(Class clazz) { + if (clazz.getSuperclass() == null) return Collections.emptySet(); + return Stream.concat(Arrays.stream(clazz.getInterfaces()), getAllInterfaces(clazz.getSuperclass()).stream()) + .collect(Collectors.toSet()); + } + static class ModuleInfo { private final String name; List functions; Map constants; + List types; public ModuleInfo(String name) { this.name = name; functions = new ArrayList<>(); constants = new HashMap<>(); + types = new ArrayList<>(); } public JSONArray constantsJSON() { @@ -74,7 +96,16 @@ public final class ModulesInfoCreator { constant.put("name", entry.getKey()); constant.put("type", value.type()); constant.put("typeName", Types.typeToString(value.type())); - constant.put("value", value.asString()); + if (value.type() == Types.MAP) { + String text = ((Map) value.raw()).entrySet().stream() + .sorted(Comparator.comparing( + e -> ((MapValue)value).size() > 16 ? e.getKey() : e.getValue())) + .map(s -> s.toString()) + .collect(Collectors.joining(", ", "{", "}")); + constant.put("value", text); + } else { + constant.put("value", value.asString()); + } result.put(constant); }); return result; @@ -85,6 +116,15 @@ public final class ModulesInfoCreator { json.put("name", name); json.put("functions", functions.stream().sorted().toArray()); json.put("constants", constantsJSON()); + if (!types.isEmpty()) { + json.put("types", types.stream().sorted() + .map(s -> { + final JSONObject type = new JSONObject(); + type.put("name", s); + return type; + }) + .toArray()); + } return json; } }