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

Fix tostring formatter for child fields #313

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ target/

target/
.bsp/

.uuid
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ public class DefaultToStringFormatter implements ToStringFormatter {
@Override
@NotNull
public String formatField(@NotNull Field field) {
StringBuilder builder = new StringBuilder();

if (field.attributes().containsKey(TOSTRING_FORMAT)) {
FieldVisitor fieldVisitor = field.attributes().get(TOSTRING_FORMAT);
assert fieldVisitor != null;
field = fieldVisitor.visit(field);
}

if (isElided(field)) {
return "";
}

StringBuilder builder = new StringBuilder();
if (!isValueOnly(field)) {
formatName(builder, field.name(), field.attributes());
}

if (field.attributes().containsKey(TOSTRING_FORMAT)) {
FieldVisitor fieldVisitor = field.attributes().get(TOSTRING_FORMAT);
assert fieldVisitor != null;
field = fieldVisitor.visit(field);
}
formatValue(builder, field.value(), field.attributes());
return builder.toString();
}
Expand Down Expand Up @@ -107,6 +106,12 @@ private void formatObject(@NotNull StringBuilder b, @NotNull Value.ObjectValue v
if (!isValueOnly(field)) {
formatName(b, field.name(), field.attributes());
}

if (field.attributes().containsKey(TOSTRING_FORMAT)) {
FieldVisitor fieldVisitor = field.attributes().get(TOSTRING_FORMAT);
assert fieldVisitor != null;
field = fieldVisitor.visit(field);
}
formatValue(b, field.value(), field.attributes());
displayComma = i < fieldList.size();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.tersesystems.echopraxia.api;

import static com.tersesystems.echopraxia.api.Value.array;
import static com.tersesystems.echopraxia.api.Value.string;
import static org.assertj.core.api.Assertions.assertThat;

import java.time.Duration;
import java.util.*;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;

Expand All @@ -17,21 +20,71 @@ public void testSimpleFormat() {
assertThat(field.value().asString().raw()).isEqualTo("PT24H");
}

@Test
public void testNestedSimpleFormat() {
MyFieldBuilder fb = MyFieldBuilder.instance();

Field nameField = fb.string("name", "event name");
Field durationField = fb.duration("duration", Duration.ofDays(1));
Field eventField = fb.keyValue("event", Value.object(nameField, durationField));

var s = eventField.toString();
assertThat(s).isEqualTo("event={name=event name, duration=1 day}");
}

@Test
public void testArraySimpleFormat() {
MyFieldBuilder fb = MyFieldBuilder.instance();

Duration[] durationsArray = {Duration.ofDays(1), Duration.ofDays(2)};
var visitor =
new SimpleFieldVisitor() {
@Override
public @NotNull ArrayVisitor visitArray() {
return new SimpleArrayVisitor() {
@Override
public @NotNull Field done() {
return fieldCreator.create(
name, array(f -> string(formatDuration(f)), durationsArray), attributes);
}
};
}
};

var durationsField =
fb.array("durations", array(f -> string(f.toString()), durationsArray))
.withToStringFormat(visitor);
var s = durationsField.toString();
assertThat(s).isEqualTo("durations=[1 day, 2 days]");
}

static class MyFieldBuilder implements PresentationFieldBuilder {
static MyFieldBuilder instance() {
return new MyFieldBuilder();
}

public PresentationField duration(String name, Duration duration) {
return string(name, duration.toString())
.asValueOnly()
.withToStringFormat(
new SimpleFieldVisitor() {
@Override
public @NotNull Field visitString(@NotNull Value<String> stringValue) {
return string(name, duration.toDays() + " day");
return string(name, formatDuration(duration));
}
});
}
}

private static String formatDuration(Duration duration) {
List<String> parts = new ArrayList<>();
long days = duration.toDaysPart();
if (days > 0) {
parts.add(plural(days, "day"));
}
return String.join(", ", parts);
}

private static String plural(long num, String unit) {
return num + " " + unit + (num == 1 ? "" : "s");
}
}
Loading