Skip to content

Commit 033a4de

Browse files
committed
Дамп информации о модулях в yaml
1 parent fd92fa3 commit 033a4de

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

src/main/java/com/annimon/ownlang/utils/ModulesInfoCreator.java

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
import java.util.Collections;
1313
import java.util.Comparator;
1414
import java.util.HashMap;
15+
import java.util.LinkedHashMap;
1516
import java.util.List;
1617
import java.util.Map;
1718
import java.util.Set;
1819
import java.util.stream.Collectors;
1920
import java.util.stream.Stream;
2021
import org.json.JSONArray;
22+
import org.json.JSONException;
2123
import org.json.JSONObject;
24+
import org.yaml.snakeyaml.DumperOptions;
25+
import org.yaml.snakeyaml.Yaml;
2226

2327
public final class ModulesInfoCreator {
2428

@@ -48,11 +52,8 @@ public static void main(String[] args) throws InstantiationException, IllegalAcc
4852
moduleInfos.add(moduleInfo);
4953
}
5054

51-
final JSONArray modulesJson = new JSONArray();
52-
for (ModuleInfo moduleInfo : moduleInfos) {
53-
modulesJson.put(moduleInfo.toJSON());
54-
}
55-
System.out.println(modulesJson.toString(2));
55+
// printAsJson(moduleInfos);
56+
printAsYaml(moduleInfos);
5657

5758
System.out.println("Total modules: " + moduleInfos.size());
5859
System.out.println("Total functions: " + moduleInfos.stream()
@@ -65,6 +66,26 @@ public static void main(String[] args) throws InstantiationException, IllegalAcc
6566
);
6667
}
6768

69+
private static void printAsJson(List<ModuleInfo> moduleInfos) throws JSONException {
70+
final JSONArray modulesJson = new JSONArray();
71+
for (ModuleInfo moduleInfo : moduleInfos) {
72+
modulesJson.put(new JSONObject(moduleInfo.info()));
73+
}
74+
System.out.println(modulesJson.toString(2));
75+
}
76+
77+
private static void printAsYaml(List<ModuleInfo> moduleInfos) {
78+
DumperOptions options = new DumperOptions();
79+
options.setIndent(2);
80+
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
81+
82+
final List<Map<String, Object>> infos = new ArrayList<>();
83+
for (ModuleInfo moduleInfo : moduleInfos) {
84+
infos.add(moduleInfo.info());
85+
}
86+
System.out.println(new Yaml(options).dump(infos));
87+
}
88+
6889
private static List<String> listValues(Class moduleClass) {
6990
return Arrays.stream(moduleClass.getDeclaredClasses())
7091
.filter(clazz -> getAllInterfaces(clazz).stream().anyMatch(i -> i.equals(Value.class)))
@@ -91,14 +112,27 @@ public ModuleInfo(String name) {
91112
types = new ArrayList<>();
92113
}
93114

94-
public JSONArray constantsJSON() {
95-
final JSONArray result = new JSONArray();
115+
public List<Map<String, Object>> functions() {
116+
return functions.stream().sorted()
117+
.map(f -> {
118+
final Map<String, Object> function = new LinkedHashMap<>();
119+
function.put("name", f);
120+
function.put("args", "");
121+
function.put("desc", "");
122+
function.put("desc_ru", "");
123+
return function;
124+
})
125+
.collect(Collectors.toList());
126+
}
127+
128+
public List<Map<String, Object>> constants() {
129+
final List<Map<String, Object>> result = new ArrayList<>();
96130
constants.entrySet().stream()
97131
.sorted(Comparator.comparing(e -> e.getKey()))
98132
.forEach(entry -> {
99133
final Value value = entry.getValue();
100134

101-
final JSONObject constant = new JSONObject();
135+
final Map<String, Object> constant = new LinkedHashMap<>();
102136
constant.put("name", entry.getKey());
103137
constant.put("type", value.type());
104138
constant.put("typeName", Types.typeToString(value.type()));
@@ -112,26 +146,27 @@ public JSONArray constantsJSON() {
112146
} else {
113147
constant.put("value", value.asString());
114148
}
115-
result.put(constant);
149+
result.add(constant);
116150
});
117151
return result;
118152
}
119153

120-
public JSONObject toJSON() {
121-
final JSONObject json = new JSONObject();
122-
json.put("name", name);
123-
json.put("functions", functions.stream().sorted().toArray());
124-
json.put("constants", constantsJSON());
154+
public Map<String, Object> info() {
155+
final Map<String, Object> result = new LinkedHashMap<>();
156+
result.put("name", name);
157+
result.put("scope", "both");
158+
result.put("constants", constants());
159+
result.put("functions", functions());
125160
if (!types.isEmpty()) {
126-
json.put("types", types.stream().sorted()
161+
result.put("types", types.stream().sorted()
127162
.map(s -> {
128-
final JSONObject type = new JSONObject();
163+
final Map<String, String> type = new HashMap<>();
129164
type.put("name", s);
130165
return type;
131166
})
132167
.toArray());
133168
}
134-
return json;
169+
return result;
135170
}
136171
}
137172
}

0 commit comments

Comments
 (0)