Skip to content

Commit

Permalink
Add default values for optional properties on EntityTemplate creation…
Browse files Browse the repository at this point in the history
… and prevent writing of empty properties
  • Loading branch information
Vogel612 committed Sep 2, 2020
1 parent ee3069b commit 9fa9343
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
Expand Up @@ -16,7 +16,6 @@ import { EntityTypesModel } from './entityTypesModel';
import { CapabilityDefinitionModel } from './capabilityDefinitionModel';
import { RequirementDefinitionModel } from './requirementDefinitonModel';
import { EntityType, TPolicyType } from './ttopology-template';
import { QName } from '../../../../shared/src/app/model/qName';
import { PropertyDefinitionType } from './enums';

export class InheritanceUtils {
Expand Down Expand Up @@ -207,17 +206,13 @@ export class InheritanceUtils {
}

static getYamlProperties(type: EntityType): any {
const newProperies = {};
const newProperties = {};
const definedProperties = type.full.serviceTemplateOrNodeTypeOrNodeTypeImplementation[0].propertiesDefinition.properties;
for (const obj of definedProperties) {
const name = obj.name;
if (obj.required) {
newProperies[name] = obj.defaultValue;
} else {
newProperies[name] = null;
}
// set default value, regardless of whether the property is required
newProperties[obj.name] = obj.defaultValue;
}
return newProperies;
return newProperties;
}

/**
Expand Down
Expand Up @@ -428,7 +428,7 @@ public YamlPrinter visit(TPropertyAssignment node, Parameter parameter) {
// nested assignments are implemented by calling #printMap for Map values that are not property functions
YamlPrinter printer = new YamlPrinter(parameter.getIndent());
if (node.getValue() instanceof Map) {
Map<String, Object> value = (Map<String, Object>)node.getValue();
Map<String, TPropertyAssignment> value = (Map<String, TPropertyAssignment>)node.getValue();
// special casing for property functions to always be a single-line map value
if (value.size() == 1 && Arrays.stream(PROPERTY_FUNCTIONS).anyMatch(value::containsKey)) {
String key = value.keySet().iterator().next();
Expand Down Expand Up @@ -586,18 +586,21 @@ private <T> YamlPrinter printList(String keyValue, List<T> list, Parameter param
return printer;
}

private <T> YamlPrinter printMap(String keyValue, Map<String, T> map, Parameter parameter) {
private <T extends VisitorNode> YamlPrinter printMap(String keyValue, Map<String, T> map, Parameter parameter) {
YamlPrinter printer = new YamlPrinter(parameter.getIndent());
if (map == null || map.isEmpty()) {
if (map == null) {
return printer;
}
map.values().removeIf(Objects::isNull);
if (map.isEmpty()) {
return printer;
}
printer.printKey(keyValue)
.print(map.entrySet().stream()
.filter(entry -> entry.getValue() instanceof VisitorNode)
.map((entry) -> {
YamlPrinter p = new YamlPrinter(parameter.getIndent() + INDENT_SIZE)
.print(
printVisitorNode((VisitorNode) entry.getValue(),
printVisitorNode(entry.getValue(),
new Parameter(parameter.getIndent() + INDENT_SIZE).addContext(entry.getKey())
)
);
Expand Down
Expand Up @@ -214,7 +214,7 @@ public Map<String, TPropertyAssignment> convert(TEntityTemplate.Properties node)
if (Objects.isNull(node)) return null;
if (node instanceof TEntityTemplate.YamlProperties) {
Map<String, Object> propertiesKV = ((TEntityTemplate.YamlProperties) node).getProperties();
return propertiesKV.entrySet().stream()
Map<String, TPropertyAssignment> assignments = propertiesKV.entrySet().stream()
.map(entry ->
new LinkedHashMap.SimpleEntry<>(
String.valueOf(entry.getKey()),
Expand All @@ -225,6 +225,11 @@ public Map<String, TPropertyAssignment> convert(TEntityTemplate.Properties node)
Map.Entry::getKey,
Map.Entry::getValue
));
// remove assignments without a value
assignments.values().removeIf(Objects::isNull);
assignments.values().removeIf(tpa -> tpa.getValue() == null);
// TODO consider traversing the assignment tree to successively remove leaves that don't have an assignment
return assignments;
}
// FIXME deal with converting WineryKVProperties and XmlProperties
return null;
Expand Down

0 comments on commit 9fa9343

Please sign in to comment.