Skip to content

Commit

Permalink
Fix KMS
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-aws committed Jan 31, 2024
1 parent 18736b2 commit ef0a330
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/library_java_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ on:
jobs:
testJava:
strategy:
fail-fast: false
matrix:
library:
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static software.amazon.smithy.dafny.conversion.ToDafny.Simple.CharacterSequence;
import static software.amazon.smithy.dafny.conversion.ToNative.Simple.String;

import StandardLibraryInterop_Compile.WrappersInterop;
import Wrappers_Compile.Option;
import Wrappers_Compile.Result;
import dafny.DafnySequence;
Expand Down Expand Up @@ -40,12 +41,12 @@ public static Result<IKMSClient, Error> KMSClient() {
)
.build();
final IKMSClient shim = new Shim(client, region);
return Result.create_Success(shim);
return CreateSuccessOfClient(shim);
} catch (Exception e) {
Error dafny_error = Error.create_KMSInternalException(
Option.create_Some(CharacterSequence(e.getMessage()))
);
return Result.create_Failure(dafny_error);
return CreateFailureOfError(dafny_error);
}
}

Expand Down Expand Up @@ -76,12 +77,12 @@ public static Result<IKMSClient, Error> KMSClientForRegion(
)
.build();
final IKMSClient shim = new Shim(client, regionString);
return Result.create_Success(shim);
return CreateSuccessOfClient(shim);
} catch (Exception e) {
Error dafny_error = Error.create_KMSInternalException(
Option.create_Some(CharacterSequence(e.getMessage()))
);
return Result.create_Failure(dafny_error);
return CreateFailureOfError(dafny_error);
}
}

Expand All @@ -98,14 +99,14 @@ public static Wrappers_Compile.Option<Boolean> RegionMatch(
// have no way to determine what region it is
// configured with.
if (shim.region() == null) {
return Option.create_None();
return WrappersInterop.CreateBooleanNone());
}

// Otherwise we kept record of the region
// when we created the client.
String shimRegion = shim.region();
String regionStr = String(region);
return Option.create_Some(regionStr.equals(shimRegion));
return WrappersInterop.CreateBooleanSome(regionStr.equals(shimRegion));
}

private static String UserAgentSuffix() {
Expand Down
48 changes: 48 additions & 0 deletions StandardLibrary/src/WrappersInterop.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

include "../../libraries/src/Wrappers.dfy"

//
// Helper functions for extern code to call in order to create common wrapper types.
//
// This is currently necessary to abstract away from differences in TypeDescriptor usage
// in the Java backend across different versions of Dafny:
// after Dafny 4.2 methods like Option.create_Some()
// also require explicit TypeDescriptor arguments.
// If we declare a `CreateSome<T>(t: T)` function,
// the compiled version may or may not need a type descriptor,
// which means externs would need to have different Java code for different Dafny versions.
// But if we define a non-generic version for a specific type,
// Dafny will emit the right type descriptor instances inside the compiled method,
// so the calling signature is the same across Dafny versions.
//
// These may be useful for other target languages in the future as well,
// to similarly abstract away from Dafny compilation internal details.
//
// See also DafnyApiCodegen.generateResultOfClientHelperFunctions() in smithy-dafny,
// which solves the same problem by emitting similar helper methods for each client type.
//
module StandardLibraryInterop {

import opened Wrappers

class WrappersInterop {

static function method CreateStringSome(s: string): Option<string> {
Some(s)
}

static function method CreateStringNone(): Option<string> {
None
}

static function method CreateBooleanSome(b: bool): Option<bool> {
Some(b)
}

static function method CreateBooleanNone(): Option<bool> {
None
}
}
}

0 comments on commit ef0a330

Please sign in to comment.