Skip to content

Commit

Permalink
remove unneeded case
Browse files Browse the repository at this point in the history
If the union only has ref values,
then we don’t need an extra case.

Also, some linear format
  • Loading branch information
seebees committed Oct 3, 2024
1 parent 1632f80 commit 8cc3729
Showing 1 changed file with 50 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ private TokenTree OperationMemberValidState(
// then we can not prove freshness of these items
final TokenTree removeInputs = direction == InputOutput.OUTPUT
? OperationModifiesInputs(operationShape.getId(), implementationType)
.prependSeperated(Token.of("-"))
.prependSeperated(Token.of("\n -"))
: TokenTree.empty();

// We need to do 3 things here
Expand Down Expand Up @@ -1327,12 +1327,15 @@ private TokenTree OperationMemberValidState(
.lineSeparated();
} else if (isUnion && member.isRequired()) {
// Required Union

final UnionShape union = memberShape
.asUnionShape()
.get();

return TokenTree
.of(
TokenTree.of("\n && (match %s ".formatted(varName)),
TokenTree.of(memberShape
.asUnionShape()
.get()
TokenTree.of(union
.members()
.stream()
.filter(this::OnlyReferenceStructures)
Expand All @@ -1358,19 +1361,30 @@ private TokenTree OperationMemberValidState(
)
: TokenTree.empty()
)
.flatten()
.dropEmpty()
.lineSeparated()))
.lineSeparated(),
TokenTree.of("case _ => true)")
TokenTree.of(
union
.members()
.stream()
.allMatch(this::OnlyReferenceStructures)
? ")"
: "case _ => true)")
)
.lineSeparated();
} else if (isUnion && !member.isRequired()) {
// Optional Union

final UnionShape union = memberShape
.asUnionShape()
.get();

return TokenTree
.of(
TokenTree.of("\n && ( %1$s.Some? \n ==> match %1$s.value ".formatted(varName)),
TokenTree.of(memberShape
.asUnionShape()
.get()
TokenTree.of(union
.members()
.stream()
.filter(this::OnlyReferenceStructures)
Expand All @@ -1396,9 +1410,17 @@ private TokenTree OperationMemberValidState(
)
: TokenTree.empty()
)
.flatten()
.dropEmpty()
.lineSeparated()))
.lineSeparated(),
TokenTree.of("case _ => true)")
TokenTree.of(
union
.members()
.stream()
.allMatch(this::OnlyReferenceStructures)
? ")"
: "case _ => true)")
)
.lineSeparated();
} else {
Expand Down Expand Up @@ -1537,7 +1559,15 @@ private TokenTree OperationMemberModifies(
"case %s(o) => o.Modifies"
.formatted(s.getMemberName())
))),
Token.of("case _ => {})")
TokenTree.of(
memberShape
.asUnionShape()
.get()
.members()
.stream()
.allMatch(this::OnlyReferenceStructures)
? ")"
: "case _ => {})")
)
.flatten()
.dropEmpty()
Expand All @@ -1555,7 +1585,7 @@ private TokenTree OperationMemberModifies(
return TokenTree.of(
TokenTree
.of(
Token.of("(if %1$s.Some? then match %1$s.value ".formatted(varName)),
Token.of("(if %1$s.Some? then \n match %1$s.value ".formatted(varName)),
Token.of(memberShape
.asUnionShape()
.get()
Expand All @@ -1567,7 +1597,15 @@ private TokenTree OperationMemberModifies(
"case %s(o) => o.Modifies"
.formatted(s.getMemberName())
))),
Token.of("case _ => {}"),
TokenTree.of(
memberShape
.asUnionShape()
.get()
.members()
.stream()
.allMatch(this::OnlyReferenceStructures)
? ""
: "case _ => {}"),
Token.of("else {})")
)
.flatten()
Expand Down

0 comments on commit 8cc3729

Please sign in to comment.