-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a way to get packed inputs in operations (#122)
* Adds a way to get packed inputs in operations * Adds a way for users to tweak rendering of services methods, and substitute a list of parameters in methods for a single case-class parameter (which smithy4s uses under the hood anyway), corresponding to the smithy shape of the operation's input (when present). This might offer better UX in some occurences. * Adds documentation * Change test name * adoptium => temurin * Update modules/docs/src/04-codegen/01-customisation.md Co-authored-by: Jeff Lewis <[email protected]> Co-authored-by: Jeff Lewis <[email protected]>
- Loading branch information
Showing
13 changed files
with
253 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2021 Disney Streaming | ||
* | ||
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://disneystreaming.github.io/TOST-1.0.txt | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package smithy4s.codegen | ||
|
||
trait PostProcessor extends (CompilationUnit => CompilationUnit) {} | ||
|
||
object PostProcessor extends PostProcessor { | ||
|
||
val all: List[PostProcessor] = List(PackedInputsShift) | ||
|
||
def apply(unit: CompilationUnit): CompilationUnit = { | ||
all.foldLeft(unit)((acc, f) => f(acc)) | ||
} | ||
|
||
} | ||
|
||
object PackedInputsShift extends PostProcessor { | ||
def apply(unit: CompilationUnit): CompilationUnit = { | ||
val newDecls = unit.declarations.map { | ||
case s: Service => transformService(s) | ||
case other => other | ||
} | ||
unit.copy(declarations = newDecls) | ||
} | ||
|
||
def transformService(s: Service): Service = { | ||
if (s.hints.contains(Hint.PackedInputs)) { | ||
val newOps = s.ops.map { op => | ||
op.copy(hints = Hint.PackedInputs :: op.hints) | ||
} | ||
s.copy(ops = newOps) | ||
} else s | ||
} | ||
|
||
} |
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
32 changes: 32 additions & 0 deletions
32
modules/core/test/src/smithy4s/PackedInputsSmokeSpec.scala
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,32 @@ | ||
/* | ||
* Copyright 2021 Disney Streaming | ||
* | ||
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://disneystreaming.github.io/TOST-1.0.txt | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package smithy4s | ||
|
||
import weaver._ | ||
import cats.Id | ||
import smithy4s.example.{PackedInputsService, PackedInput} | ||
|
||
object PackedInputsSmokeSpec extends FunSuite { | ||
|
||
test("Methods with packed inputs have a single case-class parameter") { | ||
val service: PackedInputsService[Id] = new PackedInputsService[Id] { | ||
def packedInputOperation(input: PackedInput): Unit = () | ||
} | ||
expect.same(service.packedInputOperation(PackedInput(key = "foo")), ()) | ||
} | ||
|
||
} |
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,64 @@ | ||
--- | ||
sidebar_label: Protocols and smithy4s | ||
title: Protocols and smithy4s | ||
--- | ||
|
||
Smithy4s is opinionated in what the generated code look like, there are a few things that can be tweaked. | ||
|
||
#### Packed inputs | ||
|
||
By default, smithy4s generates methods the parameters of which map to the fields of the input structure of the corresponding operation. | ||
|
||
For instance : | ||
|
||
```kotlin | ||
service PackedInputsService { | ||
version: "1.0.0", | ||
operations: [PackedInputOperation] | ||
} | ||
|
||
operation PackedInputOperation { | ||
input: PackedInput, | ||
} | ||
|
||
structure PackedInput { | ||
@required | ||
a: String, | ||
@required | ||
b: String | ||
} | ||
``` | ||
|
||
leads to something conceptually equivalent to : | ||
|
||
```scala | ||
trait PackedInputServiceGen[F[_]] { | ||
|
||
def packedInputOperation(a: String, b: String) : F[Unit] | ||
|
||
} | ||
``` | ||
|
||
It is however possible to annotate the service (or operation) definition with the `smithy4s.meta#packedInputs` trait, in order for the rendered method to contain a single parameter, typed with actual input case class of the operation. | ||
|
||
For instance : | ||
|
||
```scala | ||
use smithy4s.meta#packedInputs | ||
|
||
@packedInputs | ||
service PackedInputsService { | ||
version: "1.0.0", | ||
operations: [PackedInputOperation] | ||
} | ||
``` | ||
|
||
will produce the following Scala code | ||
|
||
```scala | ||
trait PackedInputServiceGen[F[_]] { | ||
|
||
def packedInputOperation(input: PackedInput) : F[Unit] | ||
|
||
} | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
smithy4s.smithy | ||
smithy4s.meta.smithy |
15 changes: 15 additions & 0 deletions
15
modules/protocol/resources/META-INF/smithy/smithy4s.meta.smithy
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,15 @@ | ||
$version: "1.0" | ||
|
||
metadata suppressions = [ | ||
{ | ||
id: "UnreferencedShape", | ||
namespace: "smithy4s.meta", | ||
reason: "This is a library namespace." | ||
} | ||
] | ||
|
||
|
||
namespace smithy4s.meta | ||
|
||
@trait(selector: ":is(service, operation)") | ||
structure packedInputs {} |
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,47 @@ | ||
/* | ||
* Copyright 2021 Disney Streaming | ||
* | ||
* Licensed under the Tomorrow Open Source Technology License, Version 1.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://disneystreaming.github.io/TOST-1.0.txt | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package smithy4s.meta; | ||
|
||
import software.amazon.smithy.model.node.Node; | ||
import software.amazon.smithy.model.node.ObjectNode; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
import software.amazon.smithy.model.traits.AnnotationTrait; | ||
import software.amazon.smithy.model.traits.AbstractTrait; | ||
|
||
public class PackedInputsTrait extends AnnotationTrait { | ||
|
||
public static ShapeId ID = ShapeId.from("smithy4s.meta#packedInputs"); | ||
|
||
public PackedInputsTrait(ObjectNode node) { | ||
super(ID, node); | ||
} | ||
|
||
public PackedInputsTrait() { | ||
super(ID, Node.objectNode()); | ||
} | ||
|
||
public static final class Provider extends AbstractTrait.Provider { | ||
public Provider() { | ||
super(ID); | ||
} | ||
|
||
@Override | ||
public PackedInputsTrait createTrait(ShapeId target, Node node) { | ||
return new PackedInputsTrait(node.expectObjectNode()); | ||
} | ||
} | ||
} |
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,18 @@ | ||
namespace smithy4s.example | ||
|
||
use smithy4s.meta#packedInputs | ||
|
||
@packedInputs | ||
service PackedInputsService { | ||
version: "1.0.0", | ||
operations: [PackedInputOperation] | ||
} | ||
|
||
operation PackedInputOperation { | ||
input: PackedInput, | ||
} | ||
|
||
structure PackedInput { | ||
@required | ||
key: String | ||
} |