-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Python: Misc codegen/ changes (#498)
- Loading branch information
1 parent
0609ad1
commit 4c32e93
Showing
24 changed files
with
401 additions
and
220 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
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
2 changes: 1 addition & 1 deletion
2
...smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python/requirements.txt
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
awscrt>=0.15,<1.0 | ||
aiohttp>=3.8.3,<3.9.0 | ||
aiohttp>=3.9.0 |
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
23 changes: 23 additions & 0 deletions
23
...hy-dafny-codegen/src/main/java/software/amazon/polymorph/smithypython/README.md
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,23 @@ | ||
TODO-Python: Add more content here | ||
|
||
Top-level file overview: | ||
|
||
``` | ||
├── awssdk - Generates a boto3 wrapper to call from Dafny-generated Python code | ||
├── common - Common code across generation targets | ||
├── localservice - Generates a Smithy client that wraps a Dafny-generated Python localService implementation | ||
└── wrappedlocalservice - Generates a wrapper for the `localservice` code to call the Smithy client from Dafny-generated Python code | ||
``` | ||
|
||
Each subfolder follows a similar structure: | ||
|
||
``` | ||
├── customize - Classes referenced from a plugin's `PythonIntegration.customize` function. | ||
│ Generates new files or adds new code to Smithy-Python generated files. | ||
├── extensions - Classes that extend or replace Smithy-Python codegen components. | ||
├── nameresolver - Utility classes to map Smithy model shapes to strings used in generated code. | ||
└── shapevisitor - Classes that generate code to convert to/from Smithy client Python shapes | ||
│ (or AWS SDK shapes) and Dafny implementation shapes. | ||
└── conversionwriter - Classes that generate functions that convert to/from Smithy client Python shapes | ||
(or AWS SDK shapes) and Dafny implementation shapes for StructureShapes and UnionShapes. | ||
``` |
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
149 changes: 149 additions & 0 deletions
149
...en/src/main/java/software/amazon/polymorph/smithypython/localservice/ConstraintUtils.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,149 @@ | ||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package software.amazon.polymorph.smithypython.localservice; | ||
|
||
import software.amazon.polymorph.utils.ConstrainTraitUtils; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.shapes.MemberShape; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
import software.amazon.smithy.model.traits.LengthTrait; | ||
import software.amazon.smithy.model.traits.RangeTrait; | ||
import software.amazon.smithy.python.codegen.PythonWriter; | ||
|
||
public class ConstraintUtils { | ||
public static void writeInitMethodConstraintsChecksForMember( | ||
PythonWriter writer, Model model, MemberShape member, String memberName) { | ||
// RangeTrait | ||
Shape targetShape = model.expectShape(member.getTarget()); | ||
if (targetShape.hasTrait(RangeTrait.class)) { | ||
RangeTrait rangeTrait = targetShape.getTrait(RangeTrait.class).get(); | ||
if (rangeTrait.getMin().isPresent()) { | ||
writeRangeTraitMinCheckForMember(writer, model, member, memberName, rangeTrait); | ||
} | ||
if (rangeTrait.getMax().isPresent()) { | ||
writeRangeTraitMaxCheckForMember(writer, model, member, memberName, rangeTrait); | ||
} | ||
} | ||
|
||
// LengthTrait | ||
if (targetShape.hasTrait(LengthTrait.class)) { | ||
LengthTrait lengthTrait = targetShape.getTrait(LengthTrait.class).get(); | ||
if (lengthTrait.getMin().isPresent()) { | ||
writeLengthTraitMinCheckForMember(writer, memberName, lengthTrait); | ||
} | ||
if (lengthTrait.getMax().isPresent()) { | ||
writeLengthTraitMaxCheckForMember(writer, memberName, lengthTrait); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Write validation for {@link LengthTrait} min value. Called from __init__. | ||
* | ||
* @param memberName | ||
* @param lengthTrait | ||
*/ | ||
protected static void writeLengthTraitMinCheckForMember( | ||
PythonWriter writer, String memberName, LengthTrait lengthTrait) { | ||
String min = ConstrainTraitUtils.LengthTraitUtils.min(lengthTrait); | ||
writer.openBlock( | ||
"if ($1L is not None) and (len($1L) < $2L):", | ||
"", | ||
memberName, | ||
min, | ||
() -> { | ||
writer.write( | ||
""" | ||
raise ValueError("The size of $1L must be greater than or equal to $2L") | ||
""", | ||
memberName, | ||
min); | ||
}); | ||
} | ||
|
||
/** | ||
* Write validation for {@link LengthTrait} max value. Called from __init__. | ||
* | ||
* @param memberName | ||
* @param lengthTrait | ||
*/ | ||
protected static void writeLengthTraitMaxCheckForMember( | ||
PythonWriter writer, String memberName, LengthTrait lengthTrait) { | ||
String max = ConstrainTraitUtils.LengthTraitUtils.max(lengthTrait); | ||
writer.openBlock( | ||
"if ($1L is not None) and (len($1L) > $2L):", | ||
"", | ||
memberName, | ||
max, | ||
() -> { | ||
writer.write( | ||
""" | ||
raise ValueError("The size of $1L must be less than or equal to $2L") | ||
""", | ||
memberName, | ||
max); | ||
}); | ||
} | ||
|
||
/** | ||
* Write validation for {@link RangeTrait} min value. Called from __init__. | ||
* | ||
* @param member | ||
* @param memberName | ||
* @param rangeTrait | ||
*/ | ||
protected static void writeRangeTraitMinCheckForMember( | ||
PythonWriter writer, | ||
Model model, | ||
MemberShape member, | ||
String memberName, | ||
RangeTrait rangeTrait) { | ||
String min = | ||
ConstrainTraitUtils.RangeTraitUtils.minAsShapeType( | ||
model.expectShape(member.getTarget()), rangeTrait); | ||
writer.openBlock( | ||
"if ($1L is not None) and ($1L < $2L):", | ||
"", | ||
memberName, | ||
min, | ||
() -> { | ||
writer.write( | ||
""" | ||
raise ValueError("$1L must be greater than or equal to $2L") | ||
""", | ||
memberName, | ||
min); | ||
}); | ||
} | ||
|
||
/** | ||
* Write validation for {@link RangeTrait} max value. Called from __init__. | ||
* | ||
* @param member | ||
* @param memberName | ||
* @param rangeTrait | ||
*/ | ||
protected static void writeRangeTraitMaxCheckForMember( | ||
PythonWriter writer, | ||
Model model, | ||
MemberShape member, | ||
String memberName, | ||
RangeTrait rangeTrait) { | ||
String max = | ||
ConstrainTraitUtils.RangeTraitUtils.maxAsShapeType( | ||
model.expectShape(member.getTarget()), rangeTrait); | ||
writer.openBlock( | ||
"if ($1L is not None) and ($1L > $2L):", | ||
"", | ||
memberName, | ||
max, | ||
() -> { | ||
writer.write( | ||
""" | ||
raise ValueError("$1L must be less than or equal to $2L") | ||
""", | ||
memberName, | ||
max); | ||
}); | ||
} | ||
} |
Oops, something went wrong.