|
| 1 | +package io.codelens.tools.thorntail; |
| 2 | + |
| 3 | +import javax.json.Json; |
| 4 | +import javax.json.JsonObjectBuilder; |
| 5 | +import javax.json.JsonValue; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Optional; |
| 10 | + |
| 11 | +public class Node { |
| 12 | + |
| 13 | + public static final String KEY = "KEY"; |
| 14 | + |
| 15 | + static final String SCHEMA = "$schema"; |
| 16 | + static final String ID = "id"; |
| 17 | + static final String TITLE = "title"; |
| 18 | + static final String TYPE = "type"; |
| 19 | + static final String DOT = "."; |
| 20 | + static final String DESCR = "description"; |
| 21 | + static final String PROPERTIES = "properties"; |
| 22 | + static final String ADDITIONAL_PROPERTIES = "additionalProperties"; |
| 23 | + |
| 24 | + private List<String> keySuggestions = new ArrayList<>(); |
| 25 | + private List<Node> children = new ArrayList<>(); |
| 26 | + private Node parent; |
| 27 | + private String name; |
| 28 | + private String description; |
| 29 | + private NodeType javaType; |
| 30 | + |
| 31 | + public boolean isLeaf() { |
| 32 | + return children.isEmpty(); |
| 33 | + } |
| 34 | + |
| 35 | + public boolean isKey() { |
| 36 | + return KEY.equals(name); |
| 37 | + } |
| 38 | + |
| 39 | + public List<Node> getChildren() { |
| 40 | + return children; |
| 41 | + } |
| 42 | + |
| 43 | + public Node getParent() { |
| 44 | + return parent; |
| 45 | + } |
| 46 | + |
| 47 | + public String getName() { |
| 48 | + return name; |
| 49 | + } |
| 50 | + |
| 51 | + public NodeType getJavaType() { |
| 52 | + return javaType; |
| 53 | + } |
| 54 | + |
| 55 | + protected void setJavaType(NodeType type) { |
| 56 | + this.javaType = type; |
| 57 | + } |
| 58 | + |
| 59 | + public String getDescription() { |
| 60 | + return description; |
| 61 | + } |
| 62 | + |
| 63 | + protected void setDescription(String description) { |
| 64 | + this.description = description; |
| 65 | + } |
| 66 | + |
| 67 | + public String getPath() { |
| 68 | + return parent != null ? parent.getPath() + DOT + name : name; |
| 69 | + } |
| 70 | + |
| 71 | + public void addKeySuggestion(String suggestion) { |
| 72 | + keySuggestions.add(suggestion); |
| 73 | + } |
| 74 | + |
| 75 | + protected void appendChild(Node node) { |
| 76 | + node.parent = this; |
| 77 | + children.add(node); |
| 78 | + } |
| 79 | + |
| 80 | + protected boolean isChildKey() { |
| 81 | + return !children.isEmpty() && children.size() == 1 && children.get(0).isKey(); |
| 82 | + } |
| 83 | + |
| 84 | + protected List<Node> getKeyChildren() { |
| 85 | + return children.get(0).children; |
| 86 | + } |
| 87 | + |
| 88 | + protected Optional<Node> getChildByName(String name) { |
| 89 | + return children.stream().filter(node -> node.getName().equals(name)).findFirst(); |
| 90 | + } |
| 91 | + |
| 92 | + protected JsonObjectBuilder toJsonObjectBuilder(boolean writeDescription) { |
| 93 | + JsonObjectBuilder nodeDescriptorObjectBuilder = Json.createObjectBuilder(); |
| 94 | + nodeDescriptorObjectBuilder.add(TYPE, javaType.getJsonString()); |
| 95 | + if (writeDescription && description != null && !description.trim().isEmpty()) { |
| 96 | + nodeDescriptorObjectBuilder.add(DESCR, description); |
| 97 | + } |
| 98 | + |
| 99 | + if (!isLeaf()) { |
| 100 | + if (isChildKey()) { |
| 101 | + processChildKeys(nodeDescriptorObjectBuilder, writeDescription); |
| 102 | + } else { |
| 103 | + processSingles(nodeDescriptorObjectBuilder, writeDescription); |
| 104 | + } |
| 105 | + } |
| 106 | + return Json.createObjectBuilder().add(name, nodeDescriptorObjectBuilder); |
| 107 | + } |
| 108 | + |
| 109 | + private void processSingles(JsonObjectBuilder nodeDescriptorObjectBuilder, boolean writeDescription) { |
| 110 | + JsonObjectBuilder properties = Json.createObjectBuilder(); |
| 111 | + for (Node child : children) { |
| 112 | + addAll(properties, child.toJsonObjectBuilder(writeDescription).build()); |
| 113 | + } |
| 114 | + nodeDescriptorObjectBuilder.add(PROPERTIES, properties); |
| 115 | + nodeDescriptorObjectBuilder.add(ADDITIONAL_PROPERTIES, Boolean.FALSE); |
| 116 | + } |
| 117 | + |
| 118 | + private void processChildKeys(JsonObjectBuilder nodeDescriptorObjectBuilder, boolean writeDescription) { |
| 119 | + JsonObjectBuilder properties = Json.createObjectBuilder(); |
| 120 | + for (Node child : getKeyChildren()) { |
| 121 | + addAll(properties, child.toJsonObjectBuilder(writeDescription).build()); |
| 122 | + } |
| 123 | + List<String> childKeySuggestions = children.get(0).keySuggestions; |
| 124 | + if (!childKeySuggestions.isEmpty()) { |
| 125 | + JsonObjectBuilder suggestedProperties = Json.createObjectBuilder(); |
| 126 | + for (String suggestion : childKeySuggestions) { |
| 127 | + Node suggestionNode = Node.createSuggestionNode(suggestion); |
| 128 | + suggestionNode.children = getKeyChildren(); |
| 129 | + addAll(suggestedProperties, suggestionNode.toJsonObjectBuilder(writeDescription).build()); |
| 130 | + } |
| 131 | + nodeDescriptorObjectBuilder.add(PROPERTIES, suggestedProperties); |
| 132 | + } |
| 133 | + JsonObjectBuilder additionalProperties = Json.createObjectBuilder(). |
| 134 | + add(TYPE, NodeType.OBJECT.getJsonString()). |
| 135 | + add(PROPERTIES, properties). |
| 136 | + add(ADDITIONAL_PROPERTIES, Boolean.FALSE); |
| 137 | + nodeDescriptorObjectBuilder.add(ADDITIONAL_PROPERTIES, additionalProperties); |
| 138 | + } |
| 139 | + |
| 140 | + private static void addAll(JsonObjectBuilder objectBuilder, Map<String, JsonValue> items) { |
| 141 | + items.forEach(objectBuilder::add); |
| 142 | + } |
| 143 | + |
| 144 | + protected static Node createNode(String name, String description, NodeType type) { |
| 145 | + Node node = new Node(); |
| 146 | + node.name = name; |
| 147 | + node.description = description; |
| 148 | + node.javaType = type; |
| 149 | + return node; |
| 150 | + } |
| 151 | + |
| 152 | + private static Node createSuggestionNode(String name) { |
| 153 | + Node node = new Node(); |
| 154 | + node.name = name; |
| 155 | + node.javaType = NodeType.OBJECT; |
| 156 | + return node; |
| 157 | + } |
| 158 | +} |
0 commit comments