/
ConfigSectionGenerator.java
204 lines (166 loc) · 6.3 KB
/
ConfigSectionGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package io.bootique.help.config;
import io.bootique.help.ConsoleAppender;
import io.bootique.meta.MetadataNode;
import io.bootique.meta.config.ConfigListMetadata;
import io.bootique.meta.config.ConfigMapMetadata;
import io.bootique.meta.config.ConfigMetadataNode;
import io.bootique.meta.config.ConfigObjectMetadata;
import io.bootique.meta.config.ConfigMetadataVisitor;
import io.bootique.meta.config.ConfigValueMetadata;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @since 0.21
*/
class ConfigSectionGenerator implements ConfigMetadataVisitor<Object> {
static final int DEFAULT_OFFSET = DefaultConfigHelpGenerator.DEFAULT_OFFSET;
protected ConsoleAppender out;
public ConfigSectionGenerator(ConsoleAppender out) {
this.out = Objects.requireNonNull(out);
}
@Override
public Object visitObjectMetadata(ConfigObjectMetadata metadata) {
printNode(metadata, false);
List<ConfigObjectMetadata> subconfigs = metadata.getAllSubConfigs()
.map(md -> md.accept(new ConfigMetadataVisitor<ConfigObjectMetadata>() {
@Override
public ConfigObjectMetadata visitObjectMetadata(ConfigObjectMetadata metadata) {
return metadata.isAbstractType() || metadata.getProperties().isEmpty() ? null : metadata;
}
}))
.filter(md -> md != null)
.collect(Collectors.toList());
if (!subconfigs.isEmpty()) {
ConfigObjectMetadata last = subconfigs.get(subconfigs.size() - 1);
subconfigs.forEach(md -> {
printObjectNoSubclasses(md);
if (md != last) {
out.println();
}
});
}
return null;
}
@Override
public Object visitValueMetadata(ConfigValueMetadata metadata) {
printNode(metadata, true);
return null;
}
@Override
public Object visitListMetadata(ConfigListMetadata metadata) {
printNode(metadata, false);
metadata.getElementType().accept(new ConfigSectionListGenerator(out.withOffset(DEFAULT_OFFSET)));
return null;
}
@Override
public Object visitMapMetadata(ConfigMapMetadata metadata) {
printNode(metadata, false);
metadata.getValuesType().accept(
new ConfigSectionMapGenerator(metadata.getKeysType(), out.withOffset(DEFAULT_OFFSET)));
return null;
}
protected void printObjectNoSubclasses(ConfigObjectMetadata metadata) {
ConsoleAppender shifted = out.withOffset(DEFAULT_OFFSET);
if (metadata.getTypeLabel() != null) {
shifted.println("# Designator of subtype: ", typeLabel(metadata.getType()));
shifted.println("type: ", metadata.getTypeLabel());
shifted.println();
}
List<ConfigMetadataNode> sortedChildren = metadata.getProperties()
.stream()
.sorted(Comparator.comparing(MetadataNode::getName))
.collect(Collectors.toList());
ConfigMetadataNode last = sortedChildren.get(sortedChildren.size() - 1);
ConfigSectionGenerator childGenerator = new ConfigSectionGenerator(shifted);
sortedChildren.forEach(p -> {
p.accept(childGenerator);
if (p != last) {
out.println();
}
});
}
protected void printNode(ConfigValueMetadata metadata, boolean asValue) {
Type valueType = metadata.getType();
if (valueType != null) {
out.println("# Type: ", typeLabel(valueType));
}
if (metadata.getDescription() != null) {
out.println("# ", metadata.getDescription());
}
if (asValue) {
String valueLabel = metadata.getType() != null ? sampleValue(metadata.getType()) : "?";
out.println(metadata.getName(), ": ", valueLabel);
} else {
out.println(metadata.getName(), ":");
}
}
protected String sampleValue(Type type) {
// TODO: allow to provide sample values in metadata, so that we can display something useful
String typeName = type.getTypeName();
switch (typeName) {
case "boolean":
case "java.lang.Boolean":
return "<true|false>";
case "int":
case "java.lang.Integer":
return "<int>";
case "byte":
case "java.lang.Byte":
return "<byte>";
case "double":
case "java.lang.Double":
return "<double>";
case "float":
case "java.lang.Float":
return "<float>";
case "short":
case "java.lang.Short":
return "<short>";
case "long":
case "java.lang.Long":
return "<long>";
case "java.lang.String":
return "<string>";
default:
return "<value>";
}
}
protected String typeLabel(Type type) {
String typeName = type.getTypeName();
switch (typeName) {
case "java.lang.Boolean":
return "boolean";
case "java.lang.Integer":
return "int";
case "java.lang.Byte":
return "byte";
case "java.lang.Double":
return "double";
case "java.lang.Float":
return "float";
case "java.lang.Short":
return "short";
case "java.lang.Long":
return "long";
case "java.lang.String":
return "String";
default:
if (type instanceof Class) {
Class<?> classType = (Class<?>) type;
if (Map.class.isAssignableFrom(classType)) {
return "Map";
}
// TODO: decipher collection type... for now hardcoding List type
else if (Collection.class.isAssignableFrom(classType)) {
return "List";
}
}
return typeName;
}
}
}