11package software .amazon .cryptography .dbencryptionsdk .dynamodb .itemencryptor .internaldafny .legacy ;
22
3+ /**
4+ * This file does *NOT* import a lot of things.
5+ * This is because it is dealing with converting
6+ * between different versions of the same name.
7+ * The DynamoDbItemEncryptor module has Dafny and Java versions
8+ * of the same type.
9+ * This means that `EncryptItemOutput` for example
10+ * needs to be disambiguated between the Dafny version and the Java version.
11+ * In order to make it clearer at each call-site exactly what is happening
12+ * the full import is used.
13+ * IDEs tend to fight this so I'm sorry.
14+ */
15+
16+ import StandardLibraryInternal .InternalResult ;
317import Wrappers_Compile .Option ;
418import Wrappers_Compile .Result ;
519import com .amazonaws .services .dynamodbv2 .datamodeling .encryption .DynamoDBEncryptor ;
1630import software .amazon .cryptography .dbencryptionsdk .dynamodb .internaldafny .types .LegacyPolicy ;
1731import software .amazon .cryptography .dbencryptionsdk .dynamodb .itemencryptor .ToNative ;
1832import software .amazon .cryptography .dbencryptionsdk .dynamodb .itemencryptor .internaldafny .types .Error ;
19- /**
20- * This file does *NOT* import a lot of things.
21- * This is because it is dealing with converting
22- * between different versions of the same name.
23- * The DynamoDbItemEncryptor module has Dafny and Java versions
24- * of the same type.
25- * This means that `EncryptItemOutput` for example
26- * needs to be disambiguated between the Dafny version and the Java version.
27- * In order to make it clearer at each call-site exactly what is happening
28- * the full import is used.
29- * IDEs tend to fight this so I'm sorry.
30- */
31-
3233import software .amazon .cryptography .dbencryptionsdk .structuredencryption .internaldafny .types .CryptoAction ;
3334
34- public class InternalLegacyOverride {
35+ public class InternalLegacyOverride extends _ExternBase_InternalLegacyOverride {
3536
3637 private DynamoDBEncryptor encryptor ;
3738 private Map <String , Set <EncryptionFlags >> actions ;
@@ -69,7 +70,7 @@ public static TypeDescriptor<InternalLegacyOverride> _typeDescriptor() {
6970 );
7071 }
7172
72- public Boolean IsLegacyInput (
73+ public boolean IsLegacyInput (
7374 software .amazon .cryptography .dbencryptionsdk .dynamodb .itemencryptor .internaldafny .types .DecryptItemInput input
7475 ) {
7576 //= specification/dynamodb-encryption-client/decrypt-item.md#determining-legacy-items
@@ -94,7 +95,9 @@ > EncryptItem(
9495 ) {
9596 // Precondition: Policy MUST allow the caller to encrypt.
9697 if (!_policy .is_FORCE__LEGACY__ENCRYPT__ALLOW__LEGACY__DECRYPT ()) {
97- return createFailure ("Legacy Policy does not support encrypt." );
98+ return CreateEncryptItemFailure (
99+ createError ("Legacy Policy does not support encrypt." )
100+ );
98101 }
99102
100103 try {
@@ -124,9 +127,9 @@ > EncryptItem(
124127 software .amazon .cryptography .dbencryptionsdk .dynamodb .itemencryptor .ToDafny .EncryptItemOutput (
125128 nativeOutput
126129 );
127- return Result . create_Success (dafnyOutput );
130+ return CreateEncryptItemSuccess (dafnyOutput );
128131 } catch (Exception ex ) {
129- return Result . create_Failure (Error .create_Opaque (ex ));
132+ return CreateEncryptItemFailure (Error .create_Opaque (ex ));
130133 }
131134 }
132135
@@ -146,7 +149,9 @@ > DecryptItem(
146149 !_policy .is_FORCE__LEGACY__ENCRYPT__ALLOW__LEGACY__DECRYPT () &&
147150 !_policy .is_FORBID__LEGACY__ENCRYPT__ALLOW__LEGACY__DECRYPT ()
148151 ) {
149- return createFailure ("Legacy Policy does not support decrypt." );
152+ return CreateDecryptItemFailure (
153+ createError ("Legacy Policy does not support decrypt." )
154+ );
150155 }
151156 try {
152157 Map <
@@ -175,9 +180,9 @@ > DecryptItem(
175180 software .amazon .cryptography .dbencryptionsdk .dynamodb .itemencryptor .ToDafny .DecryptItemOutput (
176181 nativeOutput
177182 );
178- return Result . create_Success (dafnyOutput );
183+ return CreateDecryptItemSuccess (dafnyOutput );
179184 } catch (Exception ex ) {
180- return Result . create_Failure (Error .create_Opaque (ex ));
185+ return CreateDecryptItemFailure (Error .create_Opaque (ex ));
181186 }
182187 }
183188
@@ -186,7 +191,7 @@ public static Result<Option<InternalLegacyOverride>, Error> Build(
186191 ) {
187192 // Check for early return (Postcondition): If there is no legacyOverride there is nothing to do.
188193 if (encryptorConfig .dtor_legacyOverride ().is_None ()) {
189- return Result . create_Success ( Option . create_None ());
194+ return CreateBuildSuccess ( CreateInternalLegacyOverrideNone ());
190195 }
191196 final software .amazon .cryptography .dbencryptionsdk .dynamodb .internaldafny .types .LegacyOverride legacyOverride =
192197 encryptorConfig .dtor_legacyOverride ().dtor_value ();
@@ -198,43 +203,48 @@ public static Result<Option<InternalLegacyOverride>, Error> Build(
198203
199204 // Precondition: The encryptor MUST be a DynamoDBEncryptor
200205 if (!isDynamoDBEncryptor (maybeEncryptor )) {
201- return createFailure ("Legacy encryptor is not supported" );
206+ return CreateBuildFailure (
207+ createError ("Legacy encryptor is not supported" )
208+ );
202209 }
203210 // Preconditions: MUST be able to create valid encryption context
204- final Result <EncryptionContext , Error > maybeEncryptionContext =
211+ final InternalResult <EncryptionContext , Error > maybeEncryptionContext =
205212 legacyEncryptionContext (encryptorConfig );
206- if (maybeEncryptionContext .is_Failure ()) {
207- return Result . create_Failure (maybeEncryptionContext .dtor_error ());
213+ if (maybeEncryptionContext .isFailure ()) {
214+ return CreateBuildFailure (maybeEncryptionContext .error ());
208215 }
209216 // Precondition: All actions MUST be supported types
210- final Result <Map <String , Set <EncryptionFlags >>, Error > maybeActions =
211- legacyActions (legacyOverride .dtor_attributeActionsOnEncrypt ());
212- if (maybeActions .is_Failure ()) {
213- return Result .create_Failure (maybeEncryptionContext .dtor_error ());
217+ final InternalResult <
218+ Map <String , Set <EncryptionFlags >>,
219+ Error
220+ > maybeActions = legacyActions (
221+ legacyOverride .dtor_attributeActionsOnEncrypt ()
222+ );
223+ if (maybeActions .isFailure ()) {
224+ return CreateBuildFailure (maybeEncryptionContext .error ());
214225 }
215226
216227 final InternalLegacyOverride internalLegacyOverride =
217228 new InternalLegacyOverride (
218229 (DynamoDBEncryptor ) maybeEncryptor ,
219- maybeActions .dtor_value (),
220- maybeEncryptionContext .dtor_value (),
230+ maybeActions .value (),
231+ maybeEncryptionContext .value (),
221232 legacyOverride .dtor_policy ()
222233 );
223234
224- return Result .create_Success (Option .create_Some (internalLegacyOverride ));
235+ return CreateBuildSuccess (
236+ CreateInternalLegacyOverrideSome (internalLegacyOverride )
237+ );
225238 }
226239
227240 // Everything below this point is an implementation detail
228241
229- public static < T > Result < T , Error > createFailure (String message ) {
242+ public static Error createError (String message ) {
230243 final DafnySequence <Character > dafnyMessage =
231244 software .amazon .smithy .dafny .conversion .ToDafny .Simple .CharacterSequence (
232245 message
233246 );
234- final Error dafnyEx = Error .create_DynamoDbItemEncryptorException (
235- dafnyMessage
236- );
237- return Result .create_Failure (dafnyEx );
247+ return Error .create_DynamoDbItemEncryptorException (dafnyMessage );
238248 }
239249
240250 public static boolean isDynamoDBEncryptor (
@@ -253,7 +263,10 @@ public static DafnySequence<Character> ToDafnyString(String s) {
253263 );
254264 }
255265
256- public static Result <EncryptionContext , Error > legacyEncryptionContext (
266+ public static InternalResult <
267+ EncryptionContext ,
268+ Error
269+ > legacyEncryptionContext (
257270 software .amazon .cryptography .dbencryptionsdk .dynamodb .itemencryptor .internaldafny .types .DynamoDbItemEncryptorConfig config
258271 ) {
259272 try {
@@ -272,13 +285,16 @@ public static Result<EncryptionContext, Error> legacyEncryptionContext(
272285 .build ()
273286 : encryptionContextBuilder .build ();
274287
275- return Result . create_Success (encryptionContext );
288+ return InternalResult . success (encryptionContext );
276289 } catch (Exception ex ) {
277- return Result . create_Failure (Error .create_Opaque (ex ));
290+ return InternalResult . failure (Error .create_Opaque (ex ));
278291 }
279292 }
280293
281- public static Result <Map <String , Set <EncryptionFlags >>, Error > legacyActions (
294+ public static InternalResult <
295+ Map <String , Set <EncryptionFlags >>,
296+ Error
297+ > legacyActions (
282298 DafnyMap <
283299 ? extends DafnySequence <? extends Character >,
284300 ? extends CryptoAction
@@ -311,14 +327,14 @@ public static Result<Map<String, Set<EncryptionFlags>>, Error> legacyActions(
311327 }
312328 };
313329 attributeActionsOnEncrypt .forEach (buildLegacyActions );
314- return Result . create_Success (legacyActions );
330+ return InternalResult . success (legacyActions );
315331 } catch (IllegalArgumentException ex ) {
316332 final Error dafnyEx = Error .create_DynamoDbItemEncryptorException (
317333 ToDafnyString (ex .getMessage ())
318334 );
319- return Result . create_Failure (dafnyEx );
335+ return InternalResult . failure (dafnyEx );
320336 } catch (Exception ex ) {
321- return Result . create_Failure (Error .create_Opaque (ex ));
337+ return InternalResult . failure (Error .create_Opaque (ex ));
322338 }
323339 }
324340
0 commit comments