Skip to content

Commit

Permalink
fix a smaller issue with subvalues
Browse files Browse the repository at this point in the history
if a (sub)values have same property names with should be put into the mdc,
we get an exception. also properties without the full path would be added,
but this means we have no posibility to say we needs the from a give value.
  • Loading branch information
sebastian-toepfer committed May 10, 2023
1 parent 92d0a76 commit 3e13d84
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,19 @@ public MDCMedia withValue(final String name, final String value) {

@Override
public MDCMedia withValue(final String name, final Printable value) {
final MDCMedia result;
if (mdcNames.keySet().stream().anyMatch(mdcName -> mdcName.startsWith(String.format("%s.", name)))) {
result =
new MDCMedia(
mdcNames,
new CopyMap.MergeOperator<String, String>()
.apply(mdcValues, new CopyMap<>(printValue(value).mdcValues()))
);
} else {
result = this;
}
return result;
return new MDCMedia(
mdcNames,
new CopyMap.MergeOperator<String, String>()
.apply(mdcValues, new CopyMap<>(printValue(name, value).mdcValues()))
);
}

private MDCMedia printValue(final Printable value) {
private MDCMedia printValue(final String name, final Printable value) {
return value.printOn(
mdcNames
.entrySet()
.stream()
.filter(mdcName -> mdcName.getKey().startsWith(name))
.map(entry ->
Map.entry(entry.getKey().substring(entry.getKey().indexOf('.') + 1), entry.getValue())
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,22 @@ void should_write_values_to_mdc_with_othername() {
"username",
"user.int",
"int",
"unser.long",
"user.long",
"long",
"unser.double",
"user.double",
"double",
"unser.b1",
"user.b1",
"true",
"unser.b2",
"user.b2",
"false",
"test",
"other_value"
"user.test",
"other_value",
"user.friend.user_id",
"username_of_friend",
"bigi",
"bigi",
"bigd",
"tina"
)
)
.withValue(
Expand All @@ -164,7 +170,11 @@ public <T extends Media<T>> T printOn(final T media) {
.withValue("b2", false)
.withValue("col", List.of())
.withValue("bigi", BigInteger.ONE)
.withValue("bigd", BigDecimal.TEN);
.withValue("bigd", BigDecimal.TEN)
.withValue(
"friend",
new TestPrintable(Map.of("name", "Maura", "user_id", "[email protected]"))
);
}
}
)
Expand All @@ -189,7 +199,9 @@ public <T extends Media<T>> T printOn(final T media) {
"true",
"true",
"false",
"false"
"false",
"username_of_friend",
"[email protected]"
)
)
)
Expand Down

0 comments on commit 3e13d84

Please sign in to comment.