@Override public String value() { return treeNode.value(); }
@Override public Object map(TreeNode treeNode, Type type) { Properties properties = new Properties(); if (treeNode.type() == TreeNode.Type.MAP_NODE) { treeNode.namedNodes() .forEach(namedNode -> properties.setProperty(namedNode.name(), (String) coffig.getMapper().map(namedNode.node(), String.class))); } else { treeNode.nodes().forEach(item -> properties.setProperty(item.value(), "")); } return properties; }
@Override public Optional<TreeNode> get(String path) { return treeNode.get(path).map(UnmodifiableTreeNode::of); }
@Override public TreeNode evaluate(TreeNode rootNode, TreeNode valueNode) { if (valueNode.type() == TreeNode.Type.VALUE_NODE && !valueNode.isEmpty()) { try { return new ValueNode(processValue(rootNode, valueNode.value())); } catch (Exception e) { LOGGER.error("Error when evaluating configuration macro: {}", valueNode.value(), e); return new ValueNode(); } } else { return valueNode; } }
@Override public void process(MapNode configuration) { configuration.get("env").ifPresent(TreeNode::hide); configuration.get("sys").ifPresent(TreeNode::hide); configuration.walk() .filter(node -> node.type() == TreeNode.Type.MAP_NODE) .forEach(node -> node.namedNodes() .filter(this::isPotentialPassword) .forEach(namedNode -> namedNode.node().hide()) ); }
@Override @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", justification = "Cast is verified in canHandle() method") public Object map(TreeNode treeNode, Type type) { Class<?> rawType = rawClassOf(((ParameterizedType) type).getActualTypeArguments()[0]); if (treeNode.type() == TreeNode.Type.MAP_NODE) { Map<String, String> result = new HashMap<>(); treeNode.namedNodes() .filter(namedNode -> isValueNode(namedNode.node())) .forEach(namedNode -> result.put(namedNode.name(), namedNode.node().value())); return ClassConfiguration.of(rawType, result); } else { throw SeedException.createNew(CoreErrorCode.INVALID_CLASS_CONFIGURATION) .put("nodeType", treeNode.type()) .put("class", rawType.getName()); } }
private TreeNode processArgument(TreeNode tree, String value) throws Exception { if (value.startsWith("'") && value.endsWith("'")) { return new ValueNode(value.substring(1, value.length() - 1)); } CallSiteInfo callSiteInfo = findFunctionCall(value, 0); if (callSiteInfo == null) { TreeNode refNode = tree.get(value).orElse(new ValueNode("")); if (refNode.type() == TreeNode.Type.VALUE_NODE) { // References value nodes can be processed... return new ValueNode(processValue(tree, refNode.value())); } else { // ... whereas other node types are passed directly return refNode; } } else { return new ValueNode(processValue(tree, value)); } }
@Override @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", justification = "Cast is verified in canHandle() method") public Object map(TreeNode treeNode, Type type) { Type keyType = ((ParameterizedType) type).getActualTypeArguments()[0]; Type valueType = ((ParameterizedType) type).getActualTypeArguments()[1]; if (treeNode.type() == TreeNode.Type.MAP_NODE) { return treeNode.namedNodes() .collect(toMap( namedNode -> coffig.getMapper().map(new ValueNode(namedNode.name()), keyType), namedNode -> Optional.ofNullable(coffig.getMapper().map(namedNode.node(), valueType)) .orElseGet(() -> instantiateDefault((Class<?>) valueType)) )); } else { return treeNode.nodes() .collect(toMap( node -> coffig.getMapper().map(node, keyType), node -> instantiateDefault((Class<?>) valueType)) ); } }
private boolean isValueNode(TreeNode treeNode) { return treeNode != null && treeNode.type() == TreeNode.Type.VALUE_NODE; } }
T map(TreeNode rootNode) { if (rootNode.type() == TreeNode.Type.VALUE_NODE && valueFieldInfo != null) { Optional.ofNullable(coffig.getMapper().map(rootNode, valueFieldInfo.type)) .ifPresent(valueFieldInfo.consumer); } else if (rootNode.type() == TreeNode.Type.MAP_NODE) { fieldInfo.forEach(fieldInfo -> rootNode.get(fieldInfo.alias != null ? fieldInfo.alias : fieldInfo.name) .map(treeNode -> coffig.getMapper().map(treeNode, fieldInfo.type)) .ifPresent(fieldInfo.consumer)); } return holder; }
public Object map(TreeNode treeNode, Type type) { if (treeNode == null || (treeNode.type() == TreeNode.Type.VALUE_NODE && treeNode.isEmpty())) { return null; } for (ConfigurationMapper configurationMapper : items) { if (configurationMapper.canHandle(type)) { return configurationMapper.map(treeNode, type); } } ObjectMapper<?> objectMapper = new ObjectMapper<>(rawClassOf(type)); objectMapper.initialize(coffig); return objectMapper.map(treeNode); }
@Override public Stream<NamedNode> namedNodes() { return treeNode.namedNodes() .map(namedNode -> new NamedNode(namedNode.name(), UnmodifiableTreeNode.of(namedNode.node()))); }
@Override public TreeNode remove(String name) { Path path = new Path(name); if (path.hasTail()) { if (children.size() > path.getIndex()) { TreeNode treeNode = children.get(path.getIndex()); try { TreeNode removedNode = treeNode.remove(path.getTail()); if (treeNode.isEmpty()) { children.remove(path.getIndex()); } return removedNode; } catch (PropertyNotFoundException e) { throw new PropertyNotFoundException(e, path.getHead()); } } else { throw new PropertyNotFoundException(name); } } else { return children.remove(path.getIndex()); } }
@Override public boolean isEmpty() { return treeNode.isEmpty(); }
@Override public boolean isHidden() { return treeNode.isHidden(); }
@Override public TreeNode merge(TreeNode otherNode) { if (otherNode instanceof MapNode) { ((MapNode) otherNode).children.forEach((nodeName, treeNode) -> { TreeNode node = this.children.containsKey(nodeName) ? this.children.get(nodeName) .merge(treeNode) : treeNode; this.children.put(nodeName, node); }); return this; } else { throw ConfigurationException.createNew(ConfigurationErrorCode.ILLEGAL_TREE_MERGE) .put("firstNodeType", otherNode.getClass().getSimpleName()) .put("secondNodeType", getClass().getSimpleName()); } }
@Override public void process(MapNode configuration) { Set<String> activeProfiles = parseProfiles(System.getProperty(SEEDSTACK_PROFILES_PROPERTY, "")); Map<MapNode, List<String>> toRemove = new HashMap<>(); Map<TreeNode, Map<String, String>> moves = new HashMap<>(); configuration.walk() .filter(node -> node instanceof MapNode) .map(MapNode.class::cast) .forEach(mapNode -> mapNode.namedNodes().forEach(namedNode -> { Matcher matcher = keyWithProfilePattern.matcher(namedNode.name()); if (matcher.matches()) { if (parseProfiles(matcher.group(2)).stream().noneMatch(activeProfiles::contains)) { toRemove.computeIfAbsent(mapNode, k -> new ArrayList<>()).add(namedNode.name()); } else { moves.computeIfAbsent(mapNode, k -> new HashMap<>()) .put(matcher.group(0), matcher.group(1)); } } })); // Remove keys not in the active profiles toRemove.forEach((node, list) -> list.forEach(node::remove)); // Remove the profiles from remaining keys containing them for (Map.Entry<TreeNode, Map<String, String>> movesEntry : moves.entrySet()) { for (Map.Entry<String, String> moveEntry : movesEntry.getValue().entrySet()) { movesEntry.getKey().move(moveEntry.getKey(), moveEntry.getValue()); } } }
@Override public TreeNode evaluate(TreeNode rootNode, TreeNode valueNode) { if (valueNode.type() == TreeNode.Type.VALUE_NODE && !valueNode.isEmpty()) { try { return new ValueNode(processValue(rootNode, valueNode.value())); } catch (Exception e) { LOGGER.error("Error when evaluating configuration function: {}", valueNode.value(), e); return new ValueNode(); } } else { return valueNode; } }
@Override public Type type() { return treeNode.type(); }