Skip to content

Commit

Permalink
Support bijection in OptionDefaultVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
daddykotex committed Oct 31, 2023
1 parent b18c7bf commit 01df1ab
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package smithy4s.http

import munit._
import smithy4s.schema.Schema
import smithy.api
// import smithy4s.schema.Field

final class MetadataDecoderSpec extends FunSuite {
test("Optional header") {
case class Foo(deviceType: Option[String])
val schema =
Schema
.struct(
Schema.string
.optional[Foo]("deviceType", _.deviceType)
.addHints(api.HttpHeader("x-device-type"))
.addHints(api.Input())
)(Foo(_))
.addHints(smithy4s.internals.InputOutput.Input.widen)

val decoder = Metadata.Decoder.fromSchema(schema)
val result = decoder.decode(Metadata())

assertEquals(result, Right(Foo(None)))
}

test("Optional bijection header") {
case class Foo(name: Option[String])
val schema: Schema[Foo] = {
val field = Schema.string.option
.biject[Option[String]](identity[Option[String]](_))(identity(_))
.required[Foo]("name", _.name)
.addHints(smithy.api.HttpHeader("X-Name"))
Schema
.struct(field)(Foo(_))
.addHints(smithy4s.internals.InputOutput.Input.widen)
}

val decoder = Metadata.Decoder.fromSchema(schema)
val result = decoder.decode(Metadata())

assertEquals(result, Right(Foo(None)))
}
}
2 changes: 2 additions & 0 deletions modules/core/src/smithy4s/schema/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ object Schema {
private object OptionDefaultVisitor extends SchemaVisitor.Default[Option] {
def default[A] : Option[A] = None
override def option[A](schema: Schema[A]) : Option[Option[A]] = Some(None)
override def biject[A, B](schema: Schema[A], bijection: Bijection[A, B]): Option[B] =
this.apply(schema).map(bijection.to)
}

def operation(id: ShapeId): OperationSchema[Unit, Nothing, Unit, Nothing, Nothing] =
Expand Down

0 comments on commit 01df1ab

Please sign in to comment.