-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow generating shapes without a service
- Loading branch information
1 parent
3b9f6f6
commit 46b6ba3
Showing
5 changed files
with
281 additions
and
31 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
127 changes: 127 additions & 0 deletions
127
...thon-codegen/src/main/java/software/amazon/smithy/python/codegen/PythonShapeSettings.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,127 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.python.codegen; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
import software.amazon.smithy.model.node.ObjectNode; | ||
import software.amazon.smithy.model.node.StringNode; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
import software.amazon.smithy.utils.SmithyBuilder; | ||
import software.amazon.smithy.utils.SmithyUnstableApi; | ||
import software.amazon.smithy.utils.ToSmithyBuilder; | ||
|
||
/** | ||
* Settings used by {@link PythonClientCodegenPlugin}. | ||
* | ||
* @param service The id of the service that is being generated. | ||
* @param moduleName The name of the module to generate. | ||
* @param moduleVersion The version of the module to generate. | ||
* @param moduleDescription The optional module description for the module that will be generated. | ||
*/ | ||
@SmithyUnstableApi | ||
public record PythonShapeSettings( | ||
Optional<ShapeId> service, | ||
String moduleName, | ||
String moduleVersion, | ||
String moduleDescription | ||
) implements ToSmithyBuilder<PythonShapeSettings> { | ||
|
||
private static final String SERVICE = "service"; | ||
private static final String MODULE_NAME = "module"; | ||
private static final String MODULE_DESCRIPTION = "moduleDescription"; | ||
private static final String MODULE_VERSION = "moduleVersion"; | ||
|
||
private PythonShapeSettings(Builder builder) { | ||
this( | ||
Optional.ofNullable(builder.service), | ||
builder.moduleName, | ||
builder.moduleVersion, | ||
builder.moduleDescription | ||
); | ||
} | ||
|
||
@Override | ||
public Builder toBuilder() { | ||
Builder builder = builder() | ||
.moduleName(moduleName) | ||
.moduleVersion(moduleVersion) | ||
.moduleDescription(moduleDescription); | ||
service.ifPresent(builder::service); | ||
return builder; | ||
} | ||
|
||
public PythonSettings toPythonSettings(ShapeId service) { | ||
return PythonSettings.builder() | ||
.service(service) | ||
.moduleName(moduleName) | ||
.moduleVersion(moduleVersion) | ||
.moduleDescription(moduleDescription) | ||
.artifactType(PythonSettings.ArtifactType.SHAPES) | ||
.build(); | ||
} | ||
|
||
public PythonSettings toPythonSettings() { | ||
return toPythonSettings(service.get()); | ||
} | ||
|
||
/** | ||
* Create a settings object from a configuration object node. | ||
* | ||
* @param config Config object to load. | ||
* @return Returns the extracted settings. | ||
*/ | ||
public static PythonShapeSettings fromNode(ObjectNode config) { | ||
config.warnIfAdditionalProperties(Arrays.asList(SERVICE, MODULE_NAME, MODULE_DESCRIPTION, MODULE_VERSION)); | ||
|
||
String moduleName = config.expectStringMember(MODULE_NAME).getValue(); | ||
Builder builder = builder() | ||
.moduleName(moduleName) | ||
.moduleVersion(config.expectStringMember(MODULE_VERSION).getValue()); | ||
config.getStringMember(SERVICE).map(StringNode::expectShapeId).ifPresent(builder::service); | ||
config.getStringMember(MODULE_DESCRIPTION).map(StringNode::getValue).ifPresent(builder::moduleDescription); | ||
return builder.build(); | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder implements SmithyBuilder<PythonShapeSettings> { | ||
|
||
private ShapeId service; | ||
private String moduleName; | ||
private String moduleVersion; | ||
private String moduleDescription; | ||
|
||
@Override | ||
public PythonShapeSettings build() { | ||
SmithyBuilder.requiredState("moduleName", moduleName); | ||
SmithyBuilder.requiredState("moduleVersion", moduleVersion); | ||
return new PythonShapeSettings(this); | ||
} | ||
|
||
public Builder service(ShapeId service) { | ||
this.service = service; | ||
return this; | ||
} | ||
|
||
public Builder moduleName(String moduleName) { | ||
this.moduleName = moduleName; | ||
return this; | ||
} | ||
|
||
public Builder moduleVersion(String moduleVersion) { | ||
this.moduleVersion = moduleVersion; | ||
return this; | ||
} | ||
|
||
public Builder moduleDescription(String moduleDescription) { | ||
this.moduleDescription = moduleDescription; | ||
return this; | ||
} | ||
} | ||
} |