Skip to content

Commit

Permalink
chore: repolymorph to revert opaque string (#929)
Browse files Browse the repository at this point in the history
* chore: repolymorph to revert opaque string
  • Loading branch information
ajewellamz authored Oct 31, 2024
1 parent a952fc3 commit 139f903
Show file tree
Hide file tree
Showing 44 changed files with 83 additions and 365 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,7 @@ module {:extern "software.amazon.cryptography.materialproviders.internaldafny.ty
// || (!exit(A(I)) && !access(B(I)))
| CollectionOfErrors(list: seq<Error>, nameonly message: string)
// The Opaque error, used for native, extern, wrapped or unknown errors
| Opaque(obj: object, alt_text : string := "")
| Opaque(obj: object)
type OpaqueError = e: Error | e.Opaque? witness *
}
abstract module AbstractAwsCryptographyMaterialProvidersService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ module {:extern "software.amazon.cryptography.keystore.internaldafny.types" } Aw
// || (!exit(A(I)) && !access(B(I)))
| CollectionOfErrors(list: seq<Error>, nameonly message: string)
// The Opaque error, used for native, extern, wrapped or unknown errors
| Opaque(obj: object, alt_text : string := "")
| Opaque(obj: object)
type OpaqueError = e: Error | e.Opaque? witness *
}
abstract module AbstractAwsCryptographyKeyStoreService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,11 @@ public static Error Error(RuntimeException nativeValue) {
if (nativeValue instanceof CollectionOfErrors) {
return ToDafny.Error((CollectionOfErrors) nativeValue);
}
return Error.create_Opaque(
nativeValue,
dafny.DafnySequence.asString(
java.util.Objects.nonNull(nativeValue.getMessage())
? nativeValue.getMessage()
: ""
)
);
return Error.create_Opaque(nativeValue);
}

public static Error Error(OpaqueError nativeValue) {
return Error.create_Opaque(
nativeValue.obj(),
dafny.DafnySequence.asString(
java.util.Objects.nonNull(nativeValue.altText())
? nativeValue.altText()
: ""
)
);
return Error.create_Opaque(nativeValue.obj());
}

public static Error Error(CollectionOfErrors nativeValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ public class OpaqueError extends RuntimeException {
*/
private final Object obj;

/**
* A best effort text representation of obj.
*/
private final String altText;

protected OpaqueError(BuilderImpl builder) {
super(messageFromBuilder(builder), builder.cause());
this.altText = builder.altText();
this.obj = builder.obj();
}

Expand Down Expand Up @@ -52,13 +46,6 @@ public Object obj() {
return this.obj;
}

/**
* @return A best effort text representation of obj.
*/
public String altText() {
return this.altText;
}

public Builder toBuilder() {
return new BuilderImpl(this);
}
Expand Down Expand Up @@ -98,16 +85,6 @@ public interface Builder {
*/
Object obj();

/**
* @param altText A best effort text representation of obj.
*/
Builder altText(String altText);

/**
* @return A best effort text representation of obj.
*/
String altText();

OpaqueError build();
}

Expand All @@ -119,8 +96,6 @@ static class BuilderImpl implements Builder {

protected Object obj;

protected String altText;

protected BuilderImpl() {}

protected BuilderImpl(OpaqueError model) {
Expand Down Expand Up @@ -156,15 +131,6 @@ public Object obj() {
return this.obj;
}

public Builder altText(String altText) {
this.altText = altText;
return this;
}

public String altText() {
return this.altText;
}

public OpaqueError build() {
if (
this.obj != null && this.cause == null && this.obj instanceof Throwable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,25 +173,11 @@ public static Error Error(RuntimeException nativeValue) {
if (nativeValue instanceof CollectionOfErrors) {
return ToDafny.Error((CollectionOfErrors) nativeValue);
}
return Error.create_Opaque(
nativeValue,
dafny.DafnySequence.asString(
java.util.Objects.nonNull(nativeValue.getMessage())
? nativeValue.getMessage()
: ""
)
);
return Error.create_Opaque(nativeValue);
}

public static Error Error(OpaqueError nativeValue) {
return Error.create_Opaque(
nativeValue.obj(),
dafny.DafnySequence.asString(
java.util.Objects.nonNull(nativeValue.altText())
? nativeValue.altText()
: ""
)
);
return Error.create_Opaque(nativeValue.obj());
}

public static Error Error(CollectionOfErrors nativeValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ public class OpaqueError extends RuntimeException {
*/
private final Object obj;

/**
* A best effort text representation of obj.
*/
private final String altText;

protected OpaqueError(BuilderImpl builder) {
super(messageFromBuilder(builder), builder.cause());
this.altText = builder.altText();
this.obj = builder.obj();
}

Expand Down Expand Up @@ -52,13 +46,6 @@ public Object obj() {
return this.obj;
}

/**
* @return A best effort text representation of obj.
*/
public String altText() {
return this.altText;
}

public Builder toBuilder() {
return new BuilderImpl(this);
}
Expand Down Expand Up @@ -98,16 +85,6 @@ public interface Builder {
*/
Object obj();

/**
* @param altText A best effort text representation of obj.
*/
Builder altText(String altText);

/**
* @return A best effort text representation of obj.
*/
String altText();

OpaqueError build();
}

Expand All @@ -119,8 +96,6 @@ static class BuilderImpl implements Builder {

protected Object obj;

protected String altText;

protected BuilderImpl() {}

protected BuilderImpl(OpaqueError model) {
Expand Down Expand Up @@ -156,15 +131,6 @@ public Object obj() {
return this.obj;
}

public Builder altText(String altText) {
this.altText = altText;
return this;
}

public String altText() {
return this.altText;
}

public OpaqueError build() {
if (
this.obj != null && this.cause == null && this.obj instanceof Throwable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3976,12 +3976,12 @@ public static software.amazon.cryptography.materialproviders.internaldafny.types
);
// OpaqueError is redundant, but listed for completeness.
case OpaqueError exception:
return new software.amazon.cryptography.materialproviders.internaldafny.types.Error_Opaque(exception, Dafny.Sequence<char>.FromString(exception.ToString()));
return new software.amazon.cryptography.materialproviders.internaldafny.types.Error_Opaque(exception);
case System.Exception exception:
return new software.amazon.cryptography.materialproviders.internaldafny.types.Error_Opaque(exception, Dafny.Sequence<char>.FromString(exception.ToString()));
return new software.amazon.cryptography.materialproviders.internaldafny.types.Error_Opaque(exception);
default:
// The switch MUST be complete for System.Exception, so `value` MUST NOT be an System.Exception. (How did you get here?)
return new software.amazon.cryptography.materialproviders.internaldafny.types.Error_Opaque(value, Dafny.Sequence<char>.FromString(value.ToString()));
return new software.amazon.cryptography.materialproviders.internaldafny.types.Error_Opaque(value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,12 +785,12 @@ public static software.amazon.cryptography.keystore.internaldafny.types._IError
);
// OpaqueError is redundant, but listed for completeness.
case OpaqueError exception:
return new software.amazon.cryptography.keystore.internaldafny.types.Error_Opaque(exception, Dafny.Sequence<char>.FromString(exception.ToString()));
return new software.amazon.cryptography.keystore.internaldafny.types.Error_Opaque(exception);
case System.Exception exception:
return new software.amazon.cryptography.keystore.internaldafny.types.Error_Opaque(exception, Dafny.Sequence<char>.FromString(exception.ToString()));
return new software.amazon.cryptography.keystore.internaldafny.types.Error_Opaque(exception);
default:
// The switch MUST be complete for System.Exception, so `value` MUST NOT be an System.Exception. (How did you get here?)
return new software.amazon.cryptography.keystore.internaldafny.types.Error_Opaque(value, Dafny.Sequence<char>.FromString(value.ToString()));
return new software.amazon.cryptography.keystore.internaldafny.types.Error_Opaque(value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def _deserialize_get_beacon_key(input: DafnyResponse, config: Config):

def _deserialize_error(error: Error) -> ServiceError:
if error.is_Opaque:
return OpaqueError(obj=error.obj, alt_text=error.alt__text)
return OpaqueError(obj=error.obj)
elif error.is_CollectionOfErrors:
return CollectionOfErrors(
message=_dafny.string_of(error.message),
Expand All @@ -117,4 +117,4 @@ def _deserialize_error(error: Error) -> ServiceError:
message=_dafny.string_of(error.ComAmazonawsDynamodb.message)
)
else:
return OpaqueError(obj=error, alt_text=repr(error))
return OpaqueError(obj=error)
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,9 @@ class OpaqueError(ApiError[Literal["OpaqueError"]]):
code: Literal["OpaqueError"] = "OpaqueError"
obj: Any # As an OpaqueError, type of obj is unknown

def __init__(self, *, obj, alt_text):
def __init__(self, *, obj):
super().__init__("")
self.obj = obj
self.alt_text = alt_text

def as_dict(self) -> Dict[str, Any]:
"""Converts the OpaqueError to a dictionary.
Expand All @@ -167,7 +166,6 @@ def as_dict(self) -> Dict[str, Any]:
"message": self.message,
"code": self.code,
"obj": self.obj,
"alt_text": self.alt_text,
}

@staticmethod
Expand All @@ -178,11 +176,7 @@ def from_dict(d: Dict[str, Any]) -> "OpaqueError":
than the parameter names as keys to be mostly compatible with
boto3.
"""
kwargs: Dict[str, Any] = {
"message": d["message"],
"obj": d["obj"],
"alt_text": d["alt_text"],
}
kwargs: Dict[str, Any] = {"message": d["message"], "obj": d["obj"]}

return OpaqueError(**kwargs)

Expand All @@ -191,7 +185,7 @@ def __repr__(self) -> str:
result += f"message={self.message},"
if self.message is not None:
result += f"message={repr(self.message)}"
result += f"obj={self.alt_text}"
result += f"obj={self.obj}"
result += ")"
return result

Expand Down Expand Up @@ -235,18 +229,10 @@ def _smithy_error_to_dafny_error(e: ServiceError):

if isinstance(e, OpaqueError):
return aws_cryptographic_material_providers.internaldafny.generated.AwsCryptographyKeyStoreTypes.Error_Opaque(
obj=e.obj, alt__text=e.alt_text
obj=e.obj
)

else:
return aws_cryptographic_material_providers.internaldafny.generated.AwsCryptographyKeyStoreTypes.Error_Opaque(
obj=e,
alt__text=_dafny.Seq(
"".join(
[
chr(int.from_bytes(pair, "big"))
for pair in zip(*[iter(repr(e).encode("utf-16-be"))] * 2)
]
)
),
obj=e
)
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def _deserialize_validate_commitment_policy_on_decrypt(

def _deserialize_error(error: Error) -> ServiceError:
if error.is_Opaque:
return OpaqueError(obj=error.obj, alt_text=error.alt__text)
return OpaqueError(obj=error.obj)
elif error.is_CollectionOfErrors:
return CollectionOfErrors(
message=_dafny.string_of(error.message),
Expand Down Expand Up @@ -372,4 +372,4 @@ def _deserialize_error(error: Error) -> ServiceError:
message=_dafny.string_of(error.ComAmazonawsDynamodb.message)
)
else:
return OpaqueError(obj=error, alt_text=repr(error))
return OpaqueError(obj=error)
Loading

0 comments on commit 139f903

Please sign in to comment.