Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ensure unique labels in import-dictionaries #1932

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/main/java/sirius/biz/importer/format/ImportDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class ImportDictionary {

private final Map<String, FieldDefinition> fields = new LinkedHashMap<>();
private final Map<String, String> aliases = new LinkedHashMap<>();
private final Set<String> fieldLabels = new HashSet<>();
private List<String> mappingFunction;
private final List<Function<String, FieldDefinition>> computedFieldLookups = new ArrayList<>();
private boolean hasIdentityMapping;
Expand All @@ -83,6 +84,8 @@ public ImportDictionary addField(FieldDefinition field) {
field.getName()));
}

ensureFieldLabelsUnique(field);

this.fields.put(field.getName(), field);

Set<String> aliasDuplicatesToRemove = new HashSet<>();
Expand All @@ -95,6 +98,14 @@ public ImportDictionary addField(FieldDefinition field) {
return this;
}

private void ensureFieldLabelsUnique(FieldDefinition field) {
// if the label is already present and would no longer be unique, qualify the label including the field name
if (fieldLabels.contains(field.getLabel())) {
field.withLabel(field.getLabel() + "_" + field.getName());
}
fieldLabels.add(field.getLabel());
}

private void addCheckedAlias(FieldDefinition field, String alias, Consumer<String> duplicateConsumer) {
String previousField = aliases.putIfAbsent(normalize(alias), field.getName());
if (Strings.isFilled(previousField) && !Strings.areEqual(field.getName(), previousField)) {
Expand Down Expand Up @@ -127,7 +138,7 @@ public ImportDictionary withCustomFieldLookup(Function<String, FieldDefinition>
/**
* Returns a list of all fields in this record.
* <p>
* Note that this is not neccessarily in the expected order. Use the <tt>mapping function</tt> to specify the order.
* Note that this is not necessarily in the expected order. Use the <tt>mapping function</tt> to specify the order.
*
* @return the fields in this dictionary
*/
Expand Down