|
| 1 | +/* |
| 2 | + * Copyright 2021-2025 Disney Streaming |
| 3 | + * |
| 4 | + * Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://disneystreaming.github.io/TOST-1.0.txt |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import software.amazon.smithy.build.ProjectionTransformer |
| 18 | +import software.amazon.smithy.build.TransformContext |
| 19 | +import software.amazon.smithy.model.Model |
| 20 | +import software.amazon.smithy.model.transform.ModelTransformer |
| 21 | +import java.util.function.BiFunction |
| 22 | +import software.amazon.smithy.model.traits.Trait |
| 23 | +import software.amazon.smithy.model.shapes.Shape |
| 24 | +import software.amazon.smithy.model.traits.DocumentationTrait |
| 25 | +import software.amazon.smithy.model.shapes.ShapeId |
| 26 | +import bsp.traits.DataTrait |
| 27 | + |
| 28 | +class MyTransformation extends ProjectionTransformer { |
| 29 | + def getName(): String = "my-transformation" |
| 30 | + |
| 31 | + // Replace traits#jsonRPC with documentation |
| 32 | + def transform(context: TransformContext): Model = { |
| 33 | + // this would fail if the class wasn't present on the classpath. |
| 34 | + |
| 35 | + // external shape - regression test for #336 |
| 36 | + context |
| 37 | + .getModel() |
| 38 | + .expectShape(ShapeId.from("bsp#BuildTargetData")) |
| 39 | + .expectTrait(classOf[DataTrait]) |
| 40 | + |
| 41 | + // local shape |
| 42 | + context |
| 43 | + .getModel() |
| 44 | + .expectShape(ShapeId.from("my.input#MyShape")) |
| 45 | + .expectTrait(classOf[DataTrait]) |
| 46 | + |
| 47 | + ModelTransformer |
| 48 | + .create() |
| 49 | + .mapTraits( |
| 50 | + context.getModel(), |
| 51 | + { |
| 52 | + case (_, _: DataTrait) => |
| 53 | + new DocumentationTrait("what's up doc") |
| 54 | + case (_, trt) => trt |
| 55 | + }: BiFunction[Shape, Trait, Trait] |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments