-
Notifications
You must be signed in to change notification settings - Fork 24
Server validation #546
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
Server validation #546
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 |
|---|---|---|
|
|
@@ -228,10 +228,10 @@ public void run() { | |
| memberSymbol.expectProperty(SymbolProperties.COLLECTION_IMMUTABLE_WRAPPER)); | ||
| writer.putContext("collections", Collections.class); | ||
| writer.write( | ||
| "this.${memberName:L} = ${?nullable}builder.${memberName:L} == null ? null : ${/nullable}${collections:T}.${wrapper:L}(builder.${memberName:L});"); | ||
| "this.${memberName:L} = builder.${memberName:L} == null ? null : ${collections:T}.${wrapper:L}(builder.${memberName:L});"); | ||
|
Member
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. Why is this changing?
Contributor
Author
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. The null checks are already present in the builder and we need to be able to construct a POJO with null lists during deserialization. |
||
| } else if (target.isBlobShape() && !CodegenUtils.isStreamingBlob(target)) { | ||
| writer.write( | ||
| "this.${memberName:L} = ${?nullable}builder.${memberName:L} == null ? null : ${/nullable}builder.${memberName:L}.duplicate();"); | ||
| "this.${memberName:L} = builder.${memberName:L} == null ? null : builder.${memberName:L}.duplicate();"); | ||
| } else { | ||
| writer.write("this.${memberName:L} = builder.${memberName:L};"); | ||
| } | ||
|
|
@@ -692,6 +692,7 @@ protected void generateProperties(JavaWriter writer) { | |
|
|
||
| // Add presence tracker if any members are required by validation. | ||
| if (shape.members().stream().anyMatch(CodegenUtils::isRequiredWithNoDefault)) { | ||
| writer.write("private boolean disableChecks = false;"); | ||
| writer.putContext("tracker", PresenceTracker.class); | ||
| writer.write("private final ${tracker:T} tracker = ${tracker:T}.of($$SCHEMA);"); | ||
| } | ||
|
|
@@ -728,11 +729,13 @@ protected void generateSetters(JavaWriter writer) { | |
| writer.putContext("objects", Objects.class); | ||
| writer.putContext("throwable", Throwable.class); | ||
| for (var member : shape.members()) { | ||
| boolean isTracked = CodegenUtils.isRequiredWithNoDefault(member); | ||
| boolean requiresNullCheck = CodegenUtils.requiresSetterNullCheck(symbolProvider, member); | ||
| writer.pushState(new BuilderSetterSection(member)); | ||
| writer.putContext("memberName", symbolProvider.toMemberName(member)); | ||
| writer.putContext("memberSymbol", symbolProvider.toSymbol(member)); | ||
| writer.putContext("tracked", CodegenUtils.isRequiredWithNoDefault(member)); | ||
| writer.putContext("check", CodegenUtils.requiresSetterNullCheck(symbolProvider, member)); | ||
| writer.putContext("tracked", isTracked); | ||
| writer.putContext("check", requiresNullCheck); | ||
| writer.putContext("schemaName", CodegenUtils.toMemberSchemaName(symbolProvider.toMemberName(member))); | ||
|
|
||
| writer.write( | ||
|
|
@@ -743,6 +746,16 @@ protected void generateSetters(JavaWriter writer) { | |
| return this; | ||
| } | ||
| """); | ||
|
|
||
| if (isTracked || requiresNullCheck) { | ||
| writer.write( | ||
| """ | ||
| private Builder ${memberName:L}NoCheck(${memberSymbol:T} ${memberName:L}) { | ||
| this.${memberName:L} = ${memberName:L}; | ||
| return this; | ||
| } | ||
| """); | ||
| } | ||
| writer.popState(); | ||
| } | ||
| if (shape.hasTrait(ErrorTrait.class)) { | ||
|
|
@@ -775,7 +788,9 @@ protected void generateBuild(JavaWriter writer) { | |
| writer.write(""" | ||
| @Override | ||
| public ${shape:N} build() {${?hasRequiredMembers} | ||
| tracker.validate();${/hasRequiredMembers} | ||
| if (!disableChecks) { | ||
|
Member
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. What is this and how does it work? Can you now create shapes that don't validate required members are present (so for example, an
Contributor
Author
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. This is used to disable the checks in the builder if we are in deserialization mode. |
||
| tracker.validate(); | ||
| }${/hasRequiredMembers} | ||
| return new ${shape:T}(this); | ||
| } | ||
| """); | ||
|
|
||
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.
This all feels like it really needs some comments to explain what's going on