-
Notifications
You must be signed in to change notification settings - Fork 35
feat: Add schema + code generation #989
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
base: epic/sbs
Are you sure you want to change the base?
Conversation
| import software.amazon.smithy.swift.codegen.SwiftWriter | ||
| import software.amazon.smithy.swift.codegen.swiftmodules.SmithyTypes | ||
| import kotlin.jvm.optionals.getOrNull | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Shape.schemaVar() method generates the Swift variable name for referencing a schema.
Depending on the namespace of the shape being referenced, it may either resolve to a code-generated schema or to a schema contained in the prelude.
The prelude schemas are in the Smithy runtime module and are defined below in Prelude.swift.
| } | ||
|
|
||
| private fun ShapeId.schemaVarName(): String { | ||
| assert(this.member.getOrNull() == null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Members are never generated into their own variable; they are only generated inline into another schema's members array, hence this assertion.
| @@ -0,0 +1,12 @@ | |||
| package software.amazon.smithy.swift.codegen.integration.serde.schema | |||
|
|
|||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Below is the list of traits that are included in the schema. There is a list in the spec of all the ones that should eventually be included, but for now I'm including only the most basic ones.
|
|
||
| LOGGER.info("Generating Swift client for service ${directive.settings().service}") | ||
|
|
||
| var shouldGenerateTestTarget = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused property
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type holds the actual content of a trait.
| .testTarget( | ||
| name: "SmithyTests", | ||
| dependencies: ["Smithy"] | ||
| ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a test target to the Smithy target since none previously existed.
|
|
||
| private fun resolveInputShapes(ctx: ProtocolGenerator.GenerationContext): Map<Shape, Map<ShapeMetadata, Any>> { | ||
| var shapesInfo: MutableMap<Shape, Map<ShapeMetadata, Any>> = mutableMapOf() | ||
| val shapesInfo: MutableMap<Shape, Map<ShapeMetadata, Any>> = mutableMapOf() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixes a warning about an unmodified var.
| return resolved | ||
| } | ||
|
|
||
| private fun resolveShapesNeedingSchema(ctx: ProtocolGenerator.GenerationContext): Set<Shape> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function (and the private function below it) gets all the shapes needing schemas: inputs, outputs, errors, and their nested descendants, excluding schemas that are already provided in the Smithy prelude.
| package software.amazon.smithy.swift.codegen.utils | ||
|
|
||
| import software.amazon.smithy.swift.codegen.SwiftSettings | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like Models, schemas may either be rendered into individual files or one big Schemas.swift file.
| // | ||
|
|
||
| import XCTest | ||
| import Smithy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simple test on ShapeID to ensure it renders to String properly.
| import software.amazon.smithy.swift.codegen.swiftmodules.SmithyTypes | ||
| import kotlin.jvm.optionals.getOrNull | ||
|
|
||
| class SchemaGenerator( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This type code-generates a schema for a Smithy model shape. A Schema is just a Swift-native type that holds info about a Smithy shape that is needed for serialization.
A couple notes:
- Schemas are generated as computed, global variables, with a naming scheme that ensures each schema has a unique name. Computed properties and reference types are used to avoid circular reference compile errors on self-referencing Smithy shapes.
- Schema and related types are being marked
Sendableso that they can be assigned to global vars, but I don't expect them to have any sort of async access or functions.
Co-authored-by: Sichan Yoo <[email protected]>
Co-authored-by: Sichan Yoo <[email protected]>
Description of changes
Schematype, and related types for representing the Smithy model such asShapeIDandNode.To illustrate, here's the generated schema for
com.amazonaws.arcregionswitch#Approval, a two-case Smithy enum:Scope
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.