Skip to content

Commit

Permalink
Merge pull request #155 from daddykotex/dfrancoeur/enum-httplable
Browse files Browse the repository at this point in the history
Allow enum as path param
  • Loading branch information
Baccata authored Mar 30, 2022
2 parents 4f70a44 + 52072b5 commit 67bea32
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ object SchematicPathEncoder
override val unit: PathEncode.Make[Unit] =
struct(Vector.empty)(_ => ())

override def enumeration[A](
to: A => (String, Int),
fromName: Map[String, A],
fromOrdinal: Map[Int, A]
): PathEncode.Make[A] = PathEncode.Make.from { a => to(a)._1 }

override def struct[S](fields: Vector[Field[PathEncode.Make, S, _]])(
const: Vector[Any] => S
): PathEncode.Make[S] = {
Expand Down
3 changes: 3 additions & 0 deletions modules/tests/src/smithy4s/tests/PizzaAdminServiceImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ object PizzaAdminServiceImpl {
class PizzaAdminServiceImpl(ref: Compat.Ref[IO, State])
extends PizzaAdminService[IO] {

def getEnum(theEnum: TheEnum): IO[GetEnumOutput] =
IO.pure(GetEnumOutput(result = Some(theEnum.value)))

def addMenuItem(
restaurant: String,
menuItem: MenuItem
Expand Down
12 changes: 12 additions & 0 deletions modules/tests/src/smithy4s/tests/PizzaSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,18 @@ abstract class PizzaSpec
expect(matchResult == None)
}

routerTest("Positive: can find enum endpoint by value") {
(client, uri, log) =>
for {
resValue <- client.send(
GET(uri / "get-enum" / "v1"),
log
)
} yield {
expect(resValue._1 == 200)
}
}

type Res = (Client[IO], Uri)
def sharedResource: Resource[IO, (Client[IO], Uri)] = for {
stateRef <- Resource.eval(
Expand Down
26 changes: 25 additions & 1 deletion sampleSpecs/pizza.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use smithy4s.api#simpleRestJson
service PizzaAdminService {
version: "1.0.0",
errors: [GenericServerError, GenericClientError],
operations: [AddMenuItem, GetMenu, Version, Health, HeaderEndpoint, RoundTrip]
operations: [AddMenuItem, GetMenu, Version, Health, HeaderEndpoint, RoundTrip, GetEnum]
}

@http(method: "POST", uri: "/restaurant/{restaurant}/menu/item", code: 201)
Expand Down Expand Up @@ -214,3 +214,27 @@ string UnknownServerErrorCode

@trait
document freeForm

@readonly
@http(method: "GET", uri: "/get-enum/{aa}", code: 200)
operation GetEnum {
input: GetEnumInput,
output: GetEnumOutput,
errors: [ UnknownServerError ]
}

structure GetEnumInput {
@required
@httpLabel
aa: TheEnum
}

structure GetEnumOutput {
result: String
}

@enum([
{value: "v1", name: "V1"},
{value: "v2", name: "V2"}
])
string TheEnum

0 comments on commit 67bea32

Please sign in to comment.