Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Python): Support orphaned shapes, update Config shape generation #626

Merged
merged 81 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
4e7240e
m
lucasmcdonald3 Oct 7, 2024
bf0df25
m
lucasmcdonald3 Oct 7, 2024
689cee0
m
lucasmcdonald3 Oct 7, 2024
35427ab
m
lucasmcdonald3 Oct 7, 2024
04bd5ec
m
lucasmcdonald3 Oct 7, 2024
495ee9f
m
lucasmcdonald3 Oct 7, 2024
9fc74b6
m
lucasmcdonald3 Oct 7, 2024
a8a4555
m
lucasmcdonald3 Oct 7, 2024
4c100b5
m
lucasmcdonald3 Oct 7, 2024
523e3c3
m
lucasmcdonald3 Oct 7, 2024
0d00cd9
m
lucasmcdonald3 Oct 7, 2024
6a65d57
m
lucasmcdonald3 Oct 7, 2024
f1c3fca
m
lucasmcdonald3 Oct 7, 2024
cfe7a3c
m
lucasmcdonald3 Oct 7, 2024
3ce6972
m
lucasmcdonald3 Oct 7, 2024
21293bf
m
lucasmcdonald3 Oct 7, 2024
66f4aa3
m
lucasmcdonald3 Oct 7, 2024
ec48abf
m
lucasmcdonald3 Oct 7, 2024
d07fcf8
m
lucasmcdonald3 Oct 7, 2024
a66909e
m
lucasmcdonald3 Oct 7, 2024
e2e5205
m
lucasmcdonald3 Oct 7, 2024
18c56cf
m
lucasmcdonald3 Oct 7, 2024
0034c85
m
lucasmcdonald3 Oct 7, 2024
237feb2
m
lucasmcdonald3 Oct 7, 2024
38ff2ef
m
lucasmcdonald3 Oct 8, 2024
7602df4
m
lucasmcdonald3 Oct 8, 2024
6d2c296
m
lucasmcdonald3 Oct 8, 2024
26b0be1
m
lucasmcdonald3 Oct 8, 2024
b56c840
m
lucasmcdonald3 Oct 8, 2024
1e83075
m
lucasmcdonald3 Oct 8, 2024
5f8a83f
m
lucasmcdonald3 Oct 8, 2024
d3942f7
m
lucasmcdonald3 Oct 8, 2024
21b13a2
m
lucasmcdonald3 Oct 8, 2024
fb10473
m
lucasmcdonald3 Oct 8, 2024
e79fe41
m
lucasmcdonald3 Oct 8, 2024
49cb706
m
lucasmcdonald3 Oct 8, 2024
e1de790
m
lucasmcdonald3 Oct 8, 2024
139d5e8
m
lucasmcdonald3 Oct 8, 2024
121a71b
m
lucasmcdonald3 Oct 8, 2024
be0ec9d
m
lucasmcdonald3 Oct 8, 2024
67a10e0
m
lucasmcdonald3 Oct 8, 2024
24c14a9
m
lucasmcdonald3 Oct 8, 2024
6a3e833
m
lucasmcdonald3 Oct 8, 2024
ef3040e
m
lucasmcdonald3 Oct 8, 2024
a2672bb
m
lucasmcdonald3 Oct 8, 2024
91cc535
m
lucasmcdonald3 Oct 8, 2024
e7a024a
m
lucasmcdonald3 Oct 8, 2024
8d72061
m
lucasmcdonald3 Oct 8, 2024
f088d98
m
lucasmcdonald3 Oct 8, 2024
cfb9c42
m
lucasmcdonald3 Oct 8, 2024
bf8b764
m
lucasmcdonald3 Oct 8, 2024
1596a4b
m
lucasmcdonald3 Oct 8, 2024
bae78c4
m
lucasmcdonald3 Oct 8, 2024
8ced3e8
m
lucasmcdonald3 Oct 8, 2024
697d509
m
lucasmcdonald3 Oct 8, 2024
f81785d
m
lucasmcdonald3 Oct 8, 2024
d52c026
m
lucasmcdonald3 Oct 8, 2024
adb8ea8
m
lucasmcdonald3 Oct 8, 2024
2e68fdc
m
lucasmcdonald3 Oct 8, 2024
458a5b9
m
lucasmcdonald3 Oct 8, 2024
09c3cab
m
lucasmcdonald3 Oct 8, 2024
fbbd736
Merge branch 'main-1.x' into config-unions
lucasmcdonald3 Oct 8, 2024
d97fdd8
m
lucasmcdonald3 Oct 9, 2024
c8129ea
m
lucasmcdonald3 Oct 9, 2024
02c7c72
m
lucasmcdonald3 Oct 9, 2024
6d4d90b
m
lucasmcdonald3 Oct 9, 2024
d62d4b2
m
lucasmcdonald3 Oct 9, 2024
f1944b7
m
lucasmcdonald3 Oct 9, 2024
435f6bc
m
lucasmcdonald3 Oct 9, 2024
aaf7703
m
lucasmcdonald3 Oct 9, 2024
aa3f39c
m
lucasmcdonald3 Oct 9, 2024
aca1d18
m
lucasmcdonald3 Oct 9, 2024
8cc1633
m
lucasmcdonald3 Oct 9, 2024
a79d973
m
lucasmcdonald3 Oct 9, 2024
288d6ff
m
lucasmcdonald3 Oct 9, 2024
58056e9
Merge branch 'main-1.x' into config-unions
lucasmcdonald3 Oct 9, 2024
d3dc8b1
m
lucasmcdonald3 Oct 9, 2024
b288469
m
lucasmcdonald3 Oct 9, 2024
f5937e0
Merge branch 'main-1.x' into config-unions
lucasmcdonald3 Oct 10, 2024
4f7a90b
Merge branch 'main-1.x' into config-unions
lucasmcdonald3 Oct 10, 2024
3dd18c6
Merge branch 'main-1.x' into config-unions
lucasmcdonald3 Oct 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions SmithyDafnyMakefile.mk
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ transpile_dependencies_test:

########################## Code-Gen targets

mvn_local_deploy_polymorph_dependencies:
$(MAKE) -C $(SMITHY_DAFNY_ROOT) mvn_local_deploy_polymorph_dependencies

# The OUTPUT variables are created this way
# so that it is possible to run _parts_ of polymorph.
# Otherwise it is difficult to run/test only a Dafny change.
Expand All @@ -269,6 +272,7 @@ transpile_dependencies_test:
# make polymorph_code_gen CODEGEN_CLI_ROOT=/path/to/smithy-dafny/codegen/smithy-dafny-codegen-cli
# StandardLibrary is filtered out from dependent-model patsubst list;
# Its model is contained in $(LIBRARY_ROOT)/model, not $(LIBRARY_ROOT)/../StandardLibrary/Model.
_polymorph: mvn_local_deploy_polymorph_dependencies
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

_polymorph:
cd $(CODEGEN_CLI_ROOT); \
./../gradlew run --args="\
Expand All @@ -294,6 +298,7 @@ _polymorph:
$(POLYMORPH_OPTIONS) \
";

_polymorph_wrapped: mvn_local_deploy_polymorph_dependencies
_polymorph_wrapped:
@: $(if ${CODEGEN_CLI_ROOT},,$(error You must pass the path CODEGEN_CLI_ROOT: CODEGEN_CLI_ROOT=/path/to/smithy-dafny/codegen/smithy-dafny-codegen-cli));
cd $(CODEGEN_CLI_ROOT); \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ protected boolean isOptionalDefault(MemberShape member) {
&& (target.isDocumentShape() || target.isListShape() || target.isMapShape());
}

private void writeClassDocs(boolean isError) {
protected void writeClassDocs(boolean isError) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, this change means another

make mvn_local_deploy_polymorph_python_dependencies

on merging this from main, but I think this is better than duplicating the code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm inclined to add mvn_local_deploy_polymorph_dependencies as a dependency of all polymorph targets, given it should be cheap and hopefully Gradle-cached anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I'll include this

if (hasDocs()) {
writer.writeDocs(() -> {
shape.getTrait(DocumentationTrait.class).ifPresent(trait -> {
Expand Down Expand Up @@ -326,7 +326,7 @@ private boolean hasDocs() {
return false;
}

private void writeMemberDocs(MemberShape member) {
protected void writeMemberDocs(MemberShape member) {
member.getMemberTrait(model, DocumentationTrait.class).ifPresent(trait -> {
String memberName = symbolProvider.toMemberName(member);
String docs = writer.formatDocs(String.format(":param %s: %s", memberName, trait.getValue()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,6 @@ public void customizeFileForServiceShape(

writer.write(
"""
class $L(Config):
""\"
Smithy-modelled localService Config shape for this localService.
""\"
${C|}

def __init__(
self,
${C|}
):
${C|}
super().__init__()
${C|}

def dafny_config_to_smithy_config(dafny_config) -> $L:
""\"
Converts the provided Dafny shape for this localService's config
Expand All @@ -87,31 +73,6 @@ def smithy_config_to_dafny_config(smithy_config) -> $L:
${C|}
""",
configShape.getId().getName(),
writer.consumer(w ->
generateConfigClassFields(configShape, codegenContext, w)
),
writer.consumer(w ->
generateConfigConstructorParameters(
configShape,
codegenContext,
w
)
),
writer.consumer(w ->
generateConfigConstructorDocumentation(
configShape,
codegenContext,
w
)
),
writer.consumer(w ->
generateConfigConstructorFieldAssignments(
configShape,
codegenContext,
w
)
),
configShape.getId().getName(),
writer.consumer(w ->
generateDafnyConfigToSmithyConfigFunctionBody(
configShape,
Expand All @@ -132,149 +93,6 @@ def smithy_config_to_dafny_config(smithy_config) -> $L:
);
}

/**
* Generates the members of the Smithy-modelled localService Config shape's class. Called when
* writing the class.
*
* @param configShape
* @param codegenContext
* @param writer
*/
private void generateConfigClassFields(
StructureShape configShape,
GenerationContext codegenContext,
PythonWriter writer
) {
Map<String, MemberShape> memberShapeSet = configShape.getAllMembers();
NullableIndex index = NullableIndex.of(codegenContext.model());
for (Entry<
String,
MemberShape
> memberShapeEntry : memberShapeSet.entrySet()) {
MemberShape memberShape = memberShapeEntry.getValue();
final Shape targetShape = codegenContext
.model()
.expectShape(memberShape.getTarget());
Symbol targetShapeSymbol = codegenContext
.symbolProvider()
.toSymbol(targetShape);
if (index.isMemberNullable(memberShape)) {
writer.addStdlibImport("typing", "Optional");
writer.write(
"$L: Optional[$T]",
CaseUtils.toSnakeCase(memberShape.getMemberName()),
targetShapeSymbol
);
} else {
writer.write(
"$L: $T",
CaseUtils.toSnakeCase(memberShape.getMemberName()),
targetShapeSymbol
);
}
}
}

/**
* Generates constructor parameters for the localService's Config class. Called when writing
* parameters for the Config class' constructor (__init__ method).
*
* @param configShape
* @param codegenContext
* @param writer
*/
private void generateConfigConstructorParameters(
StructureShape configShape,
GenerationContext codegenContext,
PythonWriter writer
) {
Map<String, MemberShape> memberShapeSet = configShape.getAllMembers();
NullableIndex index = NullableIndex.of(codegenContext.model());
for (MemberShape memberShape : memberShapeSet.values()) {
final Shape targetShape = codegenContext
.model()
.expectShape(memberShape.getTarget());
Symbol targetShapeSymbol = codegenContext
.symbolProvider()
.toSymbol(targetShape);
if (index.isMemberNullable(memberShape)) {
writer.addStdlibImport("typing", "Optional");
writer.write(
"$L: Optional[$T] = None,",
CaseUtils.toSnakeCase(memberShape.getMemberName()),
targetShapeSymbol
);
} else {
writer.write(
"$L: $T,",
CaseUtils.toSnakeCase(memberShape.getMemberName()),
targetShapeSymbol
);
}
}
}

/**
* Generates constructor documentation for the localService's Config class. Called when writing
* parameters for the Config class' constructor (__init__ method).
*
* @param configShape
* @param codegenContext
* @param writer
*/
private void generateConfigConstructorDocumentation(
StructureShape configShape,
GenerationContext codegenContext,
PythonWriter writer
) {
Map<String, MemberShape> memberShapeSet = configShape.getAllMembers();
writer.writeDocs(() -> {
var constructorDocs = configShape
.getTrait(DocumentationTrait.class)
.map(StringTrait::getValue)
.orElse(
String.format("Constructor for %s.", configShape.getId().getName())
);
writer.write(constructorDocs + "\n");
for (MemberShape memberShape : memberShapeSet.values()) {
memberShape
.getMemberTrait(codegenContext.model(), DocumentationTrait.class)
.ifPresent(trait -> {
String memberName = codegenContext
.symbolProvider()
.toMemberName(memberShape);
String memberDocs = writer.formatDocs(
String.format(":param %s: %s", memberName, trait.getValue())
);
writer.write(memberDocs);
});
}
});
}

/**
* Generates assignments to fields for the localService's Config class. Called when writing the
* Config class' constructor.
*
* @param configShape
* @param codegenContext
* @param writer
*/
private void generateConfigConstructorFieldAssignments(
StructureShape configShape,
GenerationContext codegenContext,
PythonWriter writer
) {
Map<String, MemberShape> memberShapeSet = configShape.getAllMembers();
for (String memberName : memberShapeSet.keySet()) {
writer.write(
"self.$L = $L",
CaseUtils.toSnakeCase(memberName),
CaseUtils.toSnakeCase(memberName)
);
}
}

/**
* Generates the body converting the Dafny Config class (from internaldafny code) to the
* Smithy-modelled Config class defined in this file.
Expand Down
Loading
Loading