Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify schematic struct methods #77

Merged
merged 2 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/core/src/smithy4s/Schematic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ trait StubSchematic[F[_]] extends Schematic[F] with schematic.StubSchematic[F] {
def document: F[Document] = default
def timestamp: F[Timestamp] = default
def withHints[A](fa: F[A], hints: Hints): F[A] = default
def genericStruct[S](fields: Vector[Field[F, S, _]])(
def struct[S](fields: Vector[Field[F, S, _]])(
const: Vector[Any] => S
): F[S] = default
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private[smithy4s] object SchematicErrorCode
): HttpCode[B] =
(b, hints) => f(from(b), hints)

override def genericStruct[S](
override def struct[S](
fields: Vector[Field[HttpCode, S, _]]
)(f: Vector[Any] => S): HttpCode[S] = (_, hints) =>
hints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package http
package internals

import schematic._
import schematic.struct.GenericAritySchematic
import smithy4s.internals.Hinted
import smithy4s.internals.InputOutput

Expand All @@ -41,7 +40,6 @@ private[http] object SchematicMetadataReader
*/
private[http] class SchematicMetadataReader(constraints: Constraints)
extends Schematic[MetaDecode.Make]
with GenericAritySchematic[MetaDecode.Make]
with ScalaCompat {

def short: MetaDecode.Make[Short] =
Expand Down Expand Up @@ -212,7 +210,7 @@ private[http] class SchematicMetadataReader(constraints: Constraints)
binding: HttpBinding,
update: (Metadata, PutField) => Unit
)
def genericStruct[S](
def struct[S](
fields: Vector[Field[MetaDecode.Make, S, _]]
)(const: Vector[Any] => S): MetaDecode.Make[S] =
Hinted[MetaDecode].onHintOpt[InputOutput, S] { maybeInputOutput =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package http
package internals

import schematic._
import schematic.struct.GenericAritySchematic
import smithy4s.http.internals.MetaEncode._
import smithy4s.internals.Hinted
import smithy4s.internals.InputOutput
Expand All @@ -42,8 +41,7 @@ import HttpBinding._
*
*/
private[http] object SchematicMetadataWriter
extends Schematic[MetaEncode.Make]
with GenericAritySchematic[MetaEncode.Make] {
extends Schematic[MetaEncode.Make] {

def short: MetaEncode.Make[Short] = MetaEncode.Make.fromToString

Expand Down Expand Up @@ -171,7 +169,7 @@ private[http] object SchematicMetadataWriter
): MetaEncode.Make[B] =
f.contramap(from)

def genericStruct[S](
def struct[S](
fields: Vector[Field[MetaEncode.Make, S, _]]
)(f: Vector[Any] => S): MetaEncode.Make[S] =
Hinted[MetaEncode].onHintOpt[InputOutput, S] { maybeInputOutput =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ object SchematicPathEncoder
}

override val unit: PathEncode.Make[Unit] =
genericStruct(Vector.empty)(_ => ())
struct(Vector.empty)(_ => ())

override def genericStruct[S](fields: Vector[Field[PathEncode.Make, S, _]])(
override def struct[S](fields: Vector[Field[PathEncode.Make, S, _]])(
const: Vector[Any] => S
): PathEncode.Make[S] = {
type Writer = (StringBuilder, S) => Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ private[smithy4s] object StringAndBlobCodecSchematic {

private[smithy4s] class StringAndBlobCodecSchematic(constraints: Constraints)
extends Schematic[Result]
with StubSchematic[Result]
with schematic.struct.GenericAritySchematic[Result] {
with StubSchematic[Result] {

def default[A]: Result[A] = noop[A]

Expand Down Expand Up @@ -119,7 +118,7 @@ private[smithy4s] class StringAndBlobCodecSchematic(constraints: Constraints)
}
}

override def genericStruct[S](fields: Vector[Field[Result, S, _]])(
override def struct[S](fields: Vector[Field[Result, S, _]])(
const: Vector[Any] => S
): Result[S] = {
type CodecResultOpt[A] = CodecResult[Option[A]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ object DocumentDecoder {
}

object SchematicDocumentDecoder
extends smithy4s.Schematic[DocumentDecoderMake]
with schematic.struct.GenericAritySchematic[DocumentDecoderMake] {
extends smithy4s.Schematic[DocumentDecoderMake] {

object FlexibleNumber {
def unapply(doc: Document): Option[BigDecimal] = doc match {
Expand Down Expand Up @@ -287,7 +286,7 @@ object SchematicDocumentDecoder
}
}

def genericStruct[S](fields: Vector[Field[DocumentDecoderMake, S, _]])(
def struct[S](fields: Vector[Field[DocumentDecoderMake, S, _]])(
const: Vector[Any] => S
): DocumentDecoderMake[S] = Hinted.static {
def jsonLabel[A](field: Field[DocumentDecoderMake, S, A]): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ object DocumentEncoder {
}

object SchematicDocumentEncoder
extends smithy4s.Schematic[DocumentEncoderMake]
with schematic.struct.GenericAritySchematic[DocumentEncoderMake] {
extends smithy4s.Schematic[DocumentEncoderMake] {

def from[A](f: A => Document): DocumentEncoderMake[A] = Hinted.static {
new DocumentEncoder[A] {
Expand Down Expand Up @@ -162,7 +161,7 @@ object SchematicDocumentEncoder
}
}

def genericStruct[S](fields: Vector[Field[DocumentEncoderMake, S, _]])(
def struct[S](fields: Vector[Field[DocumentEncoderMake, S, _]])(
const: Vector[Any] => S
): DocumentEncoderMake[S] =
Hinted[DocumentEncoder].onHintOpt[DiscriminatedUnionMember, S] {
Expand Down
5 changes: 2 additions & 3 deletions modules/core/src/smithy4s/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,8 @@ package object syntax
val uuid: Schema[java.util.UUID] =
schematic.uuid.Schema.withHints(ShapeId("smithy4s.api", "UUID"))

def constant[A](make: => A): Schema[A] = new Schema[A] {
def compile[F[_]](s: Schematic[F]): F[A] = s.struct(make)
}
def constant[A](make: => A): Schema[A] =
new schematic.struct.Schema[Schematic, A](Vector.empty, _ => make)

implicit class withHintsSyntax[A](val schema: Schema[A]) extends AnyVal {
def withHints(hints: Hints): Schema[A] = schema match {
Expand Down
2 changes: 1 addition & 1 deletion modules/core/test/src/smithy4s/DocumentSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ object DocumentSpec extends FunSuite {
val left = struct(string.required[Foo]("str", _.str))(Foo.apply)
.oneOf[Either[Foo, Baz]]("foo", (f: Foo) => Left(f))

val right = genericStruct(Vector.empty)(_ => Baz())
val right = struct(Baz())
.oneOf[Either[Foo, Baz]]("baz", (b: Baz) => Right(b))

union(left, right) {
Expand Down
2 changes: 1 addition & 1 deletion modules/core/test/src/smithy4s/ShapeIdHintsSmokeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object ShapeIdHintsSmokeSpec extends weaver.FunSuite {
from: B => A
): ToShapeIds[B] = f

override def genericStruct[S](fields: Vector[Field[ToShapeIds, S, _]])(
override def struct[S](fields: Vector[Field[ToShapeIds, S, _]])(
const: Vector[Any] => S
): ToShapeIds[S] =
fields.flatMap(_.instance).toList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ object FromMetadata {
Left("Only structs are supported")
}

object FromMetadataSchematic
extends Schematic[FromMetadata]
with schematic.struct.GenericAritySchematic[FromMetadata] {
object FromMetadataSchematic extends Schematic[FromMetadata] {
def short: FromMetadata[Short] = FromMetadata.default

def int: FromMetadata[Int] = FromMetadata.default
Expand Down Expand Up @@ -103,7 +101,7 @@ object FromMetadataSchematic

def withHints[A](fa: FromMetadata[A], hints: Hints): FromMetadata[A] = fa

def genericStruct[S](fields: Vector[Field[FromMetadata, S, _]])(
def struct[S](fields: Vector[Field[FromMetadata, S, _]])(
const: Vector[Any] => S
): FromMetadata[S] = { (metadata: Map[String, Any]) =>
fields
Expand Down
9 changes: 5 additions & 4 deletions modules/json/src/smithy4s/http/json/SchematicJCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ import scala.collection.mutable.{Map => MMap}
import JCodec.JCodecMake

private[smithy4s] class SchematicJCodec(constraints: Constraints, maxArity: Int)
extends Schematic[JCodecMake]
with struct.GenericAritySchematic[JCodecMake] {
extends Schematic[JCodecMake] {

private val emptyMetadata: MMap[String, Any] = MMap.empty

Expand Down Expand Up @@ -438,7 +437,9 @@ private[smithy4s] class SchematicJCodec(constraints: Constraints, maxArity: Int)
type KV = (K, V)
val kField = Field.required[JCodecMake, KV, K]("key", k, _._1)
val vField = Field.required[JCodecMake, KV, V]("value", v, _._2)
val kvCodec = struct(kField, vField)(Tuple2.apply)
val kvCodec = struct(Vector(kField, vField))(vec =>
(vec(0).asInstanceOf[K], vec(1).asInstanceOf[V])
)
vector(kvCodec).transform(_.biject(_.toMap, _.toVector))
}

Expand Down Expand Up @@ -872,7 +873,7 @@ private[smithy4s] class SchematicJCodec(constraints: Constraints, maxArity: Int)
out.encodeError("Cannot use products as keys")
}

def genericStruct[Z](
def struct[Z](
fields: Vector[Field[JCodecMake, Z, _]]
)(const: Vector[Any] => Z): JCodecMake[Z] =
Hinted[JCodec].onHintsOpt[InputOutput, DiscriminatedUnionMember, Z] {
Expand Down
Loading