-
Notifications
You must be signed in to change notification settings - Fork 46
Introduce unformatted toString #239
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,4 +71,6 @@ default Conjunctable asConjunctable() { | |
|
||
@Override | ||
String toString(); | ||
|
||
String toString(boolean pretty); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new interface method |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,15 +171,27 @@ private static void validateThen(String label, @Nullable Conjunction<? extends P | |
|
||
@Override | ||
public String toString() { | ||
return toString(true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. default is that we pretty print |
||
} | ||
|
||
@Override | ||
public String toString(boolean pretty) { | ||
StringBuilder rule = new StringBuilder("" + RULE + SPACE + label); | ||
if (when != null) { | ||
rule.append(COLON).append(NEW_LINE); | ||
StringBuilder body = new StringBuilder(); | ||
body.append(WHEN).append(SPACE).append(when.toString(true)).append(NEW_LINE); | ||
body.append(THEN).append(SPACE).append(CURLY_OPEN).append(NEW_LINE); | ||
body.append(indent(then)).append(SEMICOLON); | ||
body.append(NEW_LINE).append(CURLY_CLOSE); | ||
rule.append(indent(body)); | ||
if (pretty) { | ||
rule.append(COLON).append(NEW_LINE); | ||
StringBuilder body = new StringBuilder(); | ||
body.append(WHEN).append(SPACE).append(when.toString(pretty)).append(NEW_LINE); | ||
body.append(THEN).append(SPACE).append(CURLY_OPEN).append(NEW_LINE); | ||
body.append(indent(then)).append(SEMICOLON); | ||
body.append(NEW_LINE).append(CURLY_CLOSE); | ||
rule.append(indent(body)); | ||
} else { | ||
rule.append(COLON); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new branch |
||
String content = String.valueOf(WHEN) + SPACE + when.toString(pretty) + THEN + SPACE + CURLY_OPEN + | ||
then.toString(true) + SEMICOLON + CURLY_CLOSE; | ||
rule.append(content); | ||
} | ||
} | ||
return rule.toString(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
|
||
import static com.vaticle.typeql.lang.common.TypeQLToken.Char.COMMA; | ||
import static com.vaticle.typeql.lang.common.TypeQLToken.Char.COMMA_NEW_LINE; | ||
import static com.vaticle.typeql.lang.common.TypeQLToken.Char.SPACE; | ||
import static com.vaticle.typeql.lang.common.exception.ErrorMessage.ILLEGAL_CONSTRAINT_REPETITION; | ||
|
@@ -120,9 +121,6 @@ String hasSyntax() { | |
return has().stream().map(ThingConstraint.Has::toString).collect(COMMA_NEW_LINE.joiner()); | ||
} | ||
|
||
@Override | ||
public abstract String toString(); | ||
|
||
@Override | ||
public final boolean equals(Object o) { | ||
if (this == o) return true; | ||
|
@@ -162,19 +160,23 @@ private String thingSyntax() { | |
} | ||
|
||
@Override | ||
public String toString() { | ||
public String toString(boolean pretty) { | ||
StringBuilder thing = new StringBuilder(); | ||
if (isVisible()) thing.append(reference.syntax()); | ||
String constraints = Stream.of(thingSyntax(), hasSyntax()) | ||
.filter(s -> !s.isEmpty()).collect(COMMA_NEW_LINE.joiner()); | ||
constraints = indent(constraints).trim(); | ||
String constraints; | ||
if (pretty) { | ||
constraints = Stream.of(thingSyntax(), hasSyntax()).filter(s -> !s.isEmpty()).collect(COMMA_NEW_LINE.joiner()); | ||
constraints = indent(constraints).trim(); | ||
} else { | ||
constraints = Stream.of(thingSyntax(), hasSyntax()).filter(s -> !s.isEmpty()).collect(COMMA.joiner()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new branch |
||
} | ||
if (!constraints.isEmpty()) thing.append(SPACE).append(constraints); | ||
return thing.toString(); | ||
} | ||
} | ||
|
||
public static class Relation extends ThingVariable<Relation> implements ThingVariableBuilder.Relation, | ||
ThingVariableBuilder.Common<Relation> { | ||
ThingVariableBuilder.Common<Relation> { | ||
|
||
Relation(Reference reference, ThingConstraint.Relation relationConstraint) { | ||
super(reference); | ||
|
@@ -194,14 +196,19 @@ public ThingVariable.Relation constrain(ThingConstraint.Relation.RolePlayer role | |
} | ||
|
||
@Override | ||
public String toString() { | ||
public String toString(boolean pretty) { | ||
assert relation().isPresent(); | ||
StringBuilder relation = new StringBuilder(); | ||
if (isVisible()) relation.append(reference.syntax()).append(SPACE); | ||
relation.append(relation().get()); | ||
String constraints = Stream.of(isaSyntax(), hasSyntax()) | ||
.filter(s -> !s.isEmpty()).collect(COMMA_NEW_LINE.joiner()); | ||
constraints = indent(constraints).trim(); | ||
String constraints; | ||
if (pretty) { | ||
constraints = Stream.of(isaSyntax(), hasSyntax()) | ||
.filter(s -> !s.isEmpty()).collect(COMMA_NEW_LINE.joiner()); | ||
constraints = indent(constraints).trim(); | ||
} else { | ||
constraints = Stream.of(isaSyntax(), hasSyntax()).filter(s -> !s.isEmpty()).collect(COMMA.joiner()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new branch |
||
} | ||
if (!constraints.isEmpty()) relation.append(SPACE).append(constraints); | ||
return relation.toString(); | ||
} | ||
|
@@ -221,14 +228,20 @@ ThingVariable.Attribute getThis() { | |
} | ||
|
||
@Override | ||
public String toString() { | ||
public String toString(boolean pretty) { | ||
assert value().isPresent(); | ||
StringBuilder attribute = new StringBuilder(); | ||
if (isVisible()) attribute.append(reference.syntax()).append(SPACE); | ||
attribute.append(value().get()); | ||
String constraints = Stream.of(isaSyntax(), hasSyntax()) | ||
.filter(s -> !s.isEmpty()).collect(COMMA_NEW_LINE.joiner()); | ||
constraints = indent(constraints).trim(); | ||
String constraints; | ||
if (pretty) { | ||
constraints = Stream.of(isaSyntax(), hasSyntax()) | ||
.filter(s -> !s.isEmpty()).collect(COMMA_NEW_LINE.joiner()); | ||
constraints = indent(constraints).trim(); | ||
} else { | ||
constraints = Stream.of(isaSyntax(), hasSyntax()) | ||
.filter(s -> !s.isEmpty()).collect(COMMA.joiner()); | ||
} | ||
if (!constraints.isEmpty()) attribute.append(SPACE).append(constraints); | ||
return attribute.toString(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reformatting