-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify ListSerializer to use a single hook
ListSerializer really only needs a single hook that is invoked before each value is written, and by making this hook an IntComsumer that can inspect if the first value or subsequent values are being written, there's no need for a between-values hook. To know if any values were written, the ListSerializer now exposes its position which can be queried after values are serialized.
- Loading branch information
Showing
7 changed files
with
85 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
core/src/test/java/software/amazon/smithy/java/runtime/core/serde/ListSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.java.runtime.core.serde; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.contains; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import software.amazon.smithy.java.runtime.core.schema.PreludeSchemas; | ||
import software.amazon.smithy.java.runtime.core.schema.SdkSchema; | ||
|
||
public class ListSerializerTest { | ||
@Test | ||
public void incrementsPosition() { | ||
List<Integer> positions = new ArrayList<>(); | ||
List<String> strings = new ArrayList<>(); | ||
|
||
var delegate = new SpecificShapeSerializer() { | ||
@Override | ||
public void writeString(SdkSchema schema, String value) { | ||
strings.add(value); | ||
} | ||
}; | ||
|
||
ListSerializer listSerializer = new ListSerializer(delegate, positions::add); | ||
|
||
listSerializer.writeString(PreludeSchemas.STRING, "1"); | ||
listSerializer.writeString(PreludeSchemas.STRING, "2"); | ||
|
||
assertThat(positions, contains(0, 1)); | ||
assertThat(strings, contains("1", "2")); | ||
assertThat(listSerializer.position(), is(2)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters