Skip to content

Commit

Permalink
Small refactoring to suggestion and some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronweissler committed Mar 22, 2022
1 parent a4643e2 commit 1a9a2ce
Showing 1 changed file with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class HighlightRulesMapController extends AbstractBaseController {
Expand Down Expand Up @@ -64,16 +65,26 @@ public class HighlightRulesMapController extends AbstractBaseController {
@VisibleForTesting
static final String KEY_OBJECT_ATTRIBUTES = "object-attributes";

/**
* Generates a Map that describes the structure of a class, as of now only used with {@link InspectitConfig},
* and its fields recursively for usage in Syntax Highlighting in the config-server UI.
*
* @param currentClass The class whose fields should be described.
*
* @return A Map describing the structure of the class.
*/
@VisibleForTesting
Map<String, Object> generateMap(Class<?> currentClass) {
Map<String, Object> currentClassMap = new HashMap<>();

ReflectionUtils.doWithFields(currentClass, field -> {

// Static fields are ignored, since they are not configured in the UI.
if (Modifier.isStatic(field.getModifiers())) {
return;
}

// Create new map that will contain the data for the current field.
Map<String, Object> innerMap = new HashMap<>();

if (field.getType().equals(java.util.Map.class)) {
Expand All @@ -93,25 +104,28 @@ Map<String, Object> generateMap(Class<?> currentClass) {
generateMapCollections(innerMap, listContentType, KEY_LIST_CONTENT_TYPE, KEY_LIST_CONTENTS);

} else if (field.getType().equals(java.lang.Object.class)) {

// If the field is an Object, we can not make any assumptions about its contents and
// instead any arbitrary YAML will be allowed in the highlighting.
innerMap.put(KEY_TYPE, VALUE_TYPE_YAML);

} else if (field.getType().isEnum()) {

innerMap.put(KEY_TYPE, VALUE_TYPE_ENUM);
innerMap.put(KEY_ENUM_VALUES, extractEnumValues(field.getType()));

} else if (field.getType().getName().startsWith("rocks.inspectit.ocelot.config.model")) {

// If the field is of a class of this project, this method will be called again recursively to describe
// the contents of that class as well.
innerMap.put(KEY_TYPE, VALUE_TYPE_OBJECT);
innerMap.put(KEY_OBJECT_ATTRIBUTES, generateMap(field.getType()));

} else if (currentClass.equals(GenericActionSettings.class) && (field.getName()
.equals("value") || field.getName().equals("valueBody"))) {

// These two fields contain a special kind of String, i.e. Java code, so they get a special type for highlighting.
innerMap.put(KEY_TYPE, VALUE_TYPE_JAVA);

} else {

// If the field is of any other class, any contents will simply be highlighted as text.
innerMap.put(KEY_TYPE, VALUE_TYPE_TEXT);

}
Expand All @@ -121,6 +135,15 @@ Map<String, Object> generateMap(Class<?> currentClass) {
return currentClassMap;
}

/**
* Helper function for {@link this#generateMap} to set contents for collections in the described classes,
* i.e. Lists or Maps. Works analogous to {@link this#generateMap}.
*
* @param innerMap The inner map created in {@link this#generateMap} for the current field.
* @param contentType The type of the values of the Collection.
* @param keyContentType Key for the content-type in the generated map, e.g. KEY_MAP_CONTENT_TYPE.
* @param keyContents Key for the contents in the generated map, e.g. KEY_MAP_CONTENTS.
*/
private void generateMapCollections(Map<String, Object> innerMap, Class<?> contentType, String keyContentType, String keyContents) {
if (contentType.equals(Object.class)) {
innerMap.put(keyContentType, VALUE_TYPE_YAML);
Expand All @@ -135,12 +158,15 @@ private void generateMapCollections(Map<String, Object> innerMap, Class<?> conte
}
}

/**
* Returns a list of names of all possible values for the given enum.
*
* @param currentEnum Enum whose values should be returned.
*
* @return List of names of all possible values for the enum.
*/
private List<String> extractEnumValues(Class<?> currentEnum) {
List<String> enumValues = new ArrayList<>();
for (Field enumField : currentEnum.getFields()) {
enumValues.add(enumField.getName());
}
return enumValues;
return Arrays.stream(currentEnum.getFields()).map(Field::getName).collect(Collectors.toList());
}

@ApiOperation(value = "Get JSON for Highlight Rules Generation", notes = "")
Expand Down

0 comments on commit 1a9a2ce

Please # to comment.