Skip to content

Commit 9bfe3e5

Browse files
ShubhamChaturvedi7Shubham Chaturvedi
and
Shubham Chaturvedi
authored
fix(Go): Empty Blob fix (#797)
*Issue #, if available:* *Description of changes:* Adds the fix for empty blob, based on the test case here: https://github.com/aws/private-aws-encryption-sdk-javascript-staging/pull/768/files By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice. Co-authored-by: Shubham Chaturvedi <scchatur@amazon.com>
1 parent 5f405ef commit 9bfe3e5

File tree

7 files changed

+35
-2
lines changed

7 files changed

+35
-2
lines changed

Diff for: TestModels/SimpleTypes/SimpleBlob/runtimes/go/TestsFromDafny-go/go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ require (
1010
)
1111

1212
replace github.com/smithy-lang/smithy-dafny/TestModels/SimpleTypes/SimpleBlob v0.0.0 => ../ImplementationFromDafny-go
13+
1314
replace github.com/aws/aws-cryptographic-material-providers-library/releases/go/smithy-dafny-standard-library => ../../../../../dafny-dependencies/StandardLibrary/runtimes/go/ImplementationFromDafny-go/

Diff for: TestModels/SimpleTypes/SimpleBlob/test/SimpleBlobImplTest.dfy

+22
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module SimpleBlobImplTest {
1111
method{:test} GetBlob(){
1212
var client :- expect SimpleBlob.SimpleBlob();
1313
TestGetBlob(client);
14+
TestGetBlobWithEmptyBlob(client);
1415
TestGetBlobKnownValueTest(client);
1516
}
1617

@@ -35,6 +36,27 @@ module SimpleBlobImplTest {
3536
print ret;
3637
}
3738

39+
method TestGetBlobWithEmptyBlob(client: ISimpleTypesBlobClient)
40+
requires client.ValidState()
41+
modifies client.Modifies
42+
ensures client.ValidState()
43+
{
44+
var s: seq<UInt.uint8> := [];
45+
var convertedBlobInput: GetBlobInput := SimpleBlob.Types.GetBlobInput(value:= Some(s));
46+
SimpleBlobImpl.ValidateBlobType(convertedBlobInput.value.value);
47+
// Validate values of convertedBlobInput are same as input values
48+
expect convertedBlobInput.value.value == s;
49+
50+
var ret :- expect client.GetBlob(convertedBlobInput);
51+
52+
// Expect output of GetBlob has same value as input sequence
53+
// i.e. GetBlob(GetBlobInput(seq)) == GetBlobInput(seq)
54+
expect ret.value.UnwrapOr([0x0]) == convertedBlobInput.value.value;
55+
// From above, GetBlobInput(seq) value == seq, so the below should be redundant
56+
expect ret.value.UnwrapOr([0x0]) == s;
57+
print ret;
58+
}
59+
3860
method TestGetBlobKnownValueTest(client: ISimpleTypesBlobClient)
3961
requires client.ValidState()
4062
modifies client.Modifies

Diff for: TestModels/SimpleTypes/SimpleBlob/test/WrappedSimpleBlobTest.dfy

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module WrappedSimpleTypesBlobTest {
1010
method{:test} GetBlob() {
1111
var client :- expect WrappedSimpleTypesBlobService.WrappedSimpleBlob();
1212
SimpleBlobImplTest.TestGetBlob(client);
13+
SimpleBlobImplTest.TestGetBlobWithEmptyBlob(client);
1314
SimpleBlobImplTest.TestGetBlobKnownValueTest(client);
1415
}
1516
}

Diff for: codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/awssdk/shapevisitor/AwsSdkToDafnyShapeVisitor.java

+2
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,8 @@ var enum interface{}
439439
shape.toShapeId().getName()
440440
);
441441
}
442+
443+
writer.addImportFromModule(SMITHY_DAFNY_STD_LIB_GO, "UTF8");
442444
final var underlyingType =
443445
"""
444446
func () dafny.Sequence {

Diff for: codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/awssdk/shapevisitor/DafnyToAwsSdkShapeVisitor.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public String blobShape(final BlobShape shape) {
133133
: dataSource;
134134
return """
135135
func () []byte {
136-
var b []byte
136+
b := []byte{}
137137
if %s == nil {
138138
return nil
139139
}
@@ -488,6 +488,7 @@ public String stringShape(final StringShape shape) {
488488
}
489489
}
490490

491+
writer.addImportFromModule(SMITHY_DAFNY_STD_LIB_GO, "UTF8");
491492
return """
492493
func() (%sstring) {
493494
%s

Diff for: codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/DafnyToSmithyShapeVisitor.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package software.amazon.polymorph.smithygo.localservice.shapevisitor;
22

33
import static software.amazon.polymorph.smithygo.utils.Constants.DAFNY_RUNTIME_GO_LIBRARY_MODULE;
4+
import static software.amazon.polymorph.smithygo.utils.Constants.SMITHY_DAFNY_STD_LIB_GO;
45

56
import java.util.LinkedHashMap;
67
import java.util.Map;
@@ -286,7 +287,7 @@ public String blobShape(final BlobShape shape) {
286287
// Blob shape is inherently value type
287288
return """
288289
return func () []byte {
289-
var b []byte
290+
b := []byte{}
290291
if %s == nil {
291292
return nil
292293
}
@@ -666,6 +667,8 @@ public String stringShape(final StringShape shape) {
666667
667668
s := string(dafny.ToByteArray(%s.(dafny.Sequence)))
668669
""".formatted(dataSource);
670+
} else {
671+
writer.addImportFromModule(SMITHY_DAFNY_STD_LIB_GO, "UTF8");
669672
}
670673

671674
return """

Diff for: codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/SmithyToDafnyShapeVisitor.java

+3
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,10 @@ var enum interface{}
560560

561561
if (shape.hasTrait(DafnyUtf8BytesTrait.class)) {
562562
writer.addUseImports(SmithyGoDependency.stdlib("unicode/utf8"));
563+
} else {
564+
writer.addImportFromModule(SMITHY_DAFNY_STD_LIB_GO, "UTF8");
563565
}
566+
564567
final var underlyingType = shape.hasTrait(DafnyUtf8BytesTrait.class)
565568
? """
566569
dafny.SeqOf(func () []interface{} {

0 commit comments

Comments
 (0)