-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Symbol provider to inject custom symbols (#3209)
## Description Add a SymbolProvider that uses a custom trait to return the symbol. ## Testing Unit tests. ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._ --------- Signed-off-by: Daniele Ahmed <ahmeddan@amazon.de>
- Loading branch information
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...ain/kotlin/software/amazon/smithy/rust/codegen/server/smithy/CustomShapeSymbolProvider.kt
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,26 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rust.codegen.server.smithy | ||
|
||
import software.amazon.smithy.codegen.core.Symbol | ||
import software.amazon.smithy.model.node.Node | ||
import software.amazon.smithy.model.shapes.Shape | ||
import software.amazon.smithy.model.shapes.ShapeId | ||
import software.amazon.smithy.model.traits.AnnotationTrait | ||
import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProvider | ||
import software.amazon.smithy.rust.codegen.core.smithy.WrappingSymbolProvider | ||
import software.amazon.smithy.rust.codegen.core.util.getTrait | ||
|
||
class SyntheticCustomShapeTrait(val ID: ShapeId, val symbol: Symbol) : AnnotationTrait(ID, Node.objectNode()) | ||
|
||
/** | ||
* This SymbolProvider honors [`SyntheticCustomShapeTrait`] and returns this trait's symbol when available. | ||
*/ | ||
class CustomShapeSymbolProvider(private val base: RustSymbolProvider) : WrappingSymbolProvider(base) { | ||
override fun toSymbol(shape: Shape): Symbol { | ||
return shape.getTrait<SyntheticCustomShapeTrait>()?.symbol ?: base.toSymbol(shape) | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
...kotlin/software/amazon/smithy/rust/codegen/server/smithy/CustomShapeSymbolProviderTest.kt
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,63 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.rust.codegen.server.smithy | ||
|
||
import io.kotest.matchers.shouldBe | ||
import org.junit.jupiter.api.Test | ||
import software.amazon.smithy.codegen.core.Symbol | ||
import software.amazon.smithy.model.shapes.MemberShape | ||
import software.amazon.smithy.model.shapes.ServiceShape | ||
import software.amazon.smithy.model.shapes.Shape | ||
import software.amazon.smithy.model.shapes.ShapeId | ||
import software.amazon.smithy.model.transform.ModelTransformer | ||
import software.amazon.smithy.rust.codegen.core.rustlang.RustType | ||
import software.amazon.smithy.rust.codegen.core.smithy.rustType | ||
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel | ||
import software.amazon.smithy.rust.codegen.core.util.lookup | ||
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverTestSymbolProvider | ||
|
||
class CustomShapeSymbolProviderTest { | ||
private val baseModel = | ||
""" | ||
namespace test | ||
service TestService { | ||
version: "1" | ||
operations: [TestOperation] | ||
} | ||
operation TestOperation { | ||
input: TestInputOutput | ||
output: TestInputOutput | ||
} | ||
structure TestInputOutput { | ||
myString: String, | ||
} | ||
""".asSmithyModel(smithyVersion = "2.0") | ||
private val serviceShape = baseModel.lookup<ServiceShape>("test#TestService") | ||
private val rustType = RustType.Opaque("fake-type") | ||
private val symbol = Symbol.builder() | ||
.name("fake-symbol") | ||
.rustType(rustType) | ||
.build() | ||
private val model = ModelTransformer.create() | ||
.mapShapes(baseModel) { | ||
if (it is MemberShape) { | ||
it.toBuilder().addTrait(SyntheticCustomShapeTrait(ShapeId.from("some#id"), symbol)).build() | ||
} else { | ||
it | ||
} | ||
} | ||
private val symbolProvider = serverTestSymbolProvider(baseModel, serviceShape) | ||
.let { CustomShapeSymbolProvider(it) } | ||
|
||
@Test | ||
fun `override with custom symbol`() { | ||
val shape = model.lookup<Shape>("test#TestInputOutput\$myString") | ||
symbolProvider.toSymbol(shape) shouldBe symbol | ||
} | ||
} |