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

Move Op from parameter to member in smithy4s.Service #567

Merged
merged 22 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 11 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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ on:
branches: ["main"]
tags: ["v*"]
pull_request:
branches: ["*"]
branches: ["**"]

concurrency:
group: ci-${{ github.ref }}
Expand Down
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version = "3.1.2"
runner.dialect = scala213
project {
excludePaths = [
"glob:**/core/src-*/generated/*.scala",
"glob:**/core/src-*/generated/**/*.scala",
"glob:**/example/src/smithy4s/**/*.scala",
"glob:**/schematic-core/src/generated/*.scala",
"glob:**/schematic-scalacheck/src/generated/*.scala"
Expand Down
28 changes: 14 additions & 14 deletions modules/aws/src/smithy4s/aws/AwsClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ import smithy4s.kinds.PolyFunction5

object AwsClient {

def apply[Alg[_[_, _, _, _, _]], Op[_, _, _, _, _], F[_]: MonadThrow](
service: smithy4s.Service[Alg, Op],
def apply[Alg[_[_, _, _, _, _]], F[_]: MonadThrow](
service: smithy4s.Service[Alg],
awsEnv: AwsEnvironment[F]
): Resource[F, AwsClient[Alg, F]] =
prepare(service).map(_.interpret(awsEnv)).liftTo[Resource[F, *]]
prepare(service).map(_.build(awsEnv)).liftTo[Resource[F, *]]

def prepare[Alg[_[_, _, _, _, _]], Op[_, _, _, _, _]](
service: smithy4s.Service[Alg, Op]
): Either[Throwable, AWSInterpreterBuilder[Alg, Op]] =
def prepare[Alg[_[_, _, _, _, _]]](
service: smithy4s.Service[Alg]
): Either[Throwable, AWSInterpreterBuilder[Alg]] =
for {
awsService <- service.hints
.get(_root_.aws.api.Service)
Expand All @@ -50,36 +50,36 @@ object AwsClient {
)
} yield new AWSInterpreterBuilder(awsProtocol, service, endpointPrefix)

final class AWSInterpreterBuilder[Alg[_[_, _, _, _, _]], Op[_, _, _, _, _]](
final class AWSInterpreterBuilder[Alg[_[_, _, _, _, _]]](
awsProtocol: AwsProtocol,
service: smithy4s.Service[Alg, Op],
service: smithy4s.Service[Alg],
endpointPrefix: String
) {

def build[F[_]: MonadThrow](
private def interpreter[F[_]: MonadThrow](
awsEnv: AwsEnvironment[F]
): PolyFunction5[Op, AwsCall[F, *, *, *, *, *]] =
): service.Interpreter[AwsCall[F, *, *, *, *, *]] =
awsProtocol match {
case AwsProtocol.AWS_JSON_1_0(_) =>
new AwsJsonRPCInterpreter[Alg, Op, F](
new AwsJsonRPCInterpreter[Alg, service.Operation, F](
service,
endpointPrefix,
awsEnv,
"application/x-amz-json-1.0"
)

case AwsProtocol.AWS_JSON_1_1(_) =>
new AwsJsonRPCInterpreter[Alg, Op, F](
new AwsJsonRPCInterpreter[Alg, service.Operation, F](
service,
endpointPrefix,
awsEnv,
"application/x-amz-json-1.1"
)
}

def interpret[F[_]: MonadThrow](
def build[F[_]: MonadThrow](
awsEnv: AwsEnvironment[F]
): AwsClient[Alg, F] = service.fromPolyFunction(build(awsEnv))
): AwsClient[Alg, F] = service.fromPolyFunction(interpreter(awsEnv))
}
private def initError(msg: String): Throwable = InitialisationError(msg)
case class InitialisationError(msg: String) extends Throwable(msg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import smithy4s.kinds._
* An interpreter for unary operations in the AWS_JSON_1.0/AWS_JSON_1.1 protocol
*/
private[aws] class AwsJsonRPCInterpreter[Alg[_[_, _, _, _, _]], Op[_,_,_,_,_], F[_]](
service: smithy4s.Service[Alg, Op],
service: smithy4s.Service.Aux[Alg, Op],
endpointPrefix: String,
awsEnv: AwsEnvironment[F],
contentType: String
Expand Down
13 changes: 7 additions & 6 deletions modules/codegen/src/smithy4s/codegen/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ private[codegen] class Renderer(compilationUnit: CompilationUnit) { self =>
lines(
line"type ${NameDef(name)}[F[_]] = $FunctorAlgebra_[$nameGen, F]",
block(
line"object ${NameRef(name)} extends $Service_.Provider[$nameGen, ${name}Operation]"
line"object ${NameRef(name)} extends $Service_.Provider[$nameGen]"
)(
line"def apply[F[_]](implicit F: ${NameRef(name)}[F]): F.type = F",
line"def service: $Service_[$nameGen, ${name}Operation] = $nameGen",
line"def service: $Service_[$nameGen] = $nameGen",
line"val id: $ShapeId_ = service.id"
)
)
Expand Down Expand Up @@ -205,7 +205,7 @@ private[codegen] class Renderer(compilationUnit: CompilationUnit) { self =>
newline,
obj(
genNameRef,
ext = line"$Service_[$genNameRef, $opTraitNameRef]"
ext = line"$Service_.Mixin[$genNameRef, $opTraitNameRef]"
)(
newline,
line"def apply[F[_]](implicit F: $FunctorAlgebra_[$genNameRef, F]): F.type = F",
Expand All @@ -214,7 +214,7 @@ private[codegen] class Renderer(compilationUnit: CompilationUnit) { self =>
newline,
renderHintsVal(hints),
newline,
line"val endpoints: $list[$Endpoint_[$opTraitNameRef, _, _, _, _, _]] = $list"
line"val endpoints: $list[$genNameRef.Endpoint[_, _, _, _, _]] = $list"
.args(ops.map(_.name)),
newline,
line"""val version: String = "$version"""",
Expand Down Expand Up @@ -296,6 +296,7 @@ private[codegen] class Renderer(compilationUnit: CompilationUnit) { self =>
val params = if (op.input != Type.unit) {
line"input: ${op.input}"
} else Line.empty
val genServiceName = serviceName + "Gen"
val opName = op.name
val opNameRef = NameRef(opName)
val traitName = NameRef(s"${serviceName}Operation")
Expand Down Expand Up @@ -335,11 +336,11 @@ private[codegen] class Renderer(compilationUnit: CompilationUnit) { self =>

lines(
line"case class ${NameDef(opName)}($params) extends $traitName[${op
.renderAlgParams(serviceName + "Gen")}]",
.renderAlgParams(genServiceName)}]",
obj(
opNameRef,
ext =
line"$Endpoint_[${traitName}, ${op.renderAlgParams(serviceName + "Gen")}]$errorable"
line"$genServiceName.Endpoint[${op.renderAlgParams(genServiceName)}]$errorable"
)(
renderId(op.shapeId),
line"val input: $Schema_[${op.input}] = ${op.input.schemaRef}.addHints(smithy4s.internals.InputOutput.Input.widen)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import smithy.test._
import smithy4s.compliancetests.ComplianceTest.ComplianceResult
import smithy4s.http.CodecAPI
import smithy4s.Document
import smithy4s.Endpoint
import smithy4s.http.PayloadError
import smithy4s.Service
import smithy4s.ShapeTag
Expand All @@ -47,18 +46,18 @@ import org.http4s.Header

abstract class ClientHttpComplianceTestCase[
P,
Alg[_[_, _, _, _, _]],
Op[_, _, _, _, _]
Alg[_[_, _, _, _, _]]
](
protocol: P
protocol: P,
serviceProvider: Service.Provider[Alg]
)(implicit
service: Service[Alg, Op],
ce: CompatEffect,
protocolTag: ShapeTag[P]
) {
import ce._
import org.http4s.implicits._
private val baseUri = uri"http://localhost/"
private val service = serviceProvider.service

def getClient(app: HttpApp[IO]): Resource[IO, FunctorAlgebra[Alg, IO]]
def codecs: CodecAPI
Expand Down Expand Up @@ -111,7 +110,7 @@ abstract class ClientHttpComplianceTestCase[
}

private[compliancetests] def clientRequestTest[I, E, O, SE, SO](
endpoint: Endpoint[Op, I, E, O, SE, SO],
endpoint: service.Endpoint[I, E, O, SE, SO],
testCase: HttpRequestTestCase
): ComplianceTest[IO] = {
type R[I_, E_, O_, SE_, SO_] = IO[O_]
Expand Down Expand Up @@ -156,7 +155,7 @@ abstract class ClientHttpComplianceTestCase[
}

private[compliancetests] def clientResponseTest[I, E, O, SE, SO](
endpoint: Endpoint[Op, I, E, O, SE, SO],
endpoint: service.Endpoint[I, E, O, SE, SO],
testCase: HttpResponseTestCase,
errorSchema: Option[ErrorResponseTest[_, E]] = None
): ComplianceTest[IO] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import org.http4s._
import org.http4s.headers.`Content-Type`
import smithy.test._
import smithy4s.Document
import smithy4s.Endpoint
import smithy4s.http.CodecAPI
import smithy4s.Service
import smithy4s.ShapeTag
Expand All @@ -38,22 +37,22 @@ import smithy4s.Errorable

abstract class ServerHttpComplianceTestCase[
P,
Alg[_[_, _, _, _, _]],
Op[_, _, _, _, _]
Alg[_[_, _, _, _, _]]
](
protocol: P
protocol: P,
serviceProvider: Service.Provider[Alg]
)(implicit
originalService: Service[Alg, Op],
ce: CompatEffect,
protocolTag: ShapeTag[P]
) {
import ce._
import org.http4s.implicits._
private val originalService = serviceProvider.service
private val baseUri = uri"http://localhost/"

def getServer[Alg2[_[_, _, _, _, _]], Op2[_, _, _, _, _]](
def getServer[Alg2[_[_, _, _, _, _]]](
impl: FunctorAlgebra[Alg2, IO]
)(implicit s: Service[Alg2, Op2]): Resource[IO, HttpRoutes[IO]]
)(implicit s: Service[Alg2]): Resource[IO, HttpRoutes[IO]]
def codecs: CodecAPI

private def makeRequest(
Expand Down Expand Up @@ -108,21 +107,20 @@ abstract class ServerHttpComplianceTestCase[
}

private[compliancetests] def serverRequestTest[I, E, O, SE, SO](
endpoint: Endpoint[Op, I, E, O, SE, SO],
endpoint: originalService.Endpoint[I, E, O, SE, SO],
testCase: HttpRequestTestCase
): ComplianceTest[IO] = {
type R[I_, E_, O_, SE_, SO_] = IO[O_]

val inputFromDocument = Document.Decoder.fromSchema(endpoint.input)
ComplianceTest[IO](
name = endpoint.id.toString + "(server|request): " + testCase.id,
run = {
deferred[I].flatMap { inputDeferred =>
val fakeImpl: FunctorAlgebra[Alg, IO] =
originalService.fromPolyFunction[R](
new FunctorInterpreter[Op, IO] {
originalService.fromPolyFunction(
new originalService.FunctorInterpreter[IO] {
def apply[I_, E_, O_, SE_, SO_](
op: Op[I_, E_, O_, SE_, SO_]
op: originalService.Operation[I_, E_, O_, SE_, SO_]
): IO[O_] = {
val (in, endpointInternal) = originalService.endpoint(op)

Expand Down Expand Up @@ -154,7 +152,7 @@ abstract class ServerHttpComplianceTestCase[
}

private[compliancetests] def serverResponseTest[I, E, O, SE, SO](
endpoint: Endpoint[Op, I, E, O, SE, SO],
endpoint: originalService.Endpoint[I, E, O, SE, SO],
testCase: HttpResponseTestCase,
errorSchema: Option[ErrorResponseTest[_, E]] = None
): ComplianceTest[IO] = {
Expand Down Expand Up @@ -232,11 +230,11 @@ abstract class ServerHttpComplianceTestCase[

private case class NoInputOp[I_, E_, O_, SE_, SO_]()
private def prepareService[I, E, O, SE, SO](
endpoint: Endpoint[Op, I, E, O, SE, SO]
endpoint: originalService.Endpoint[I, E, O, SE, SO]
): (Service.Reflective[NoInputOp], Request[IO]) = {
val amendedEndpoint =
// format: off
new Endpoint[NoInputOp, Unit, E, O, Nothing, Nothing] {
new smithy4s.Endpoint[NoInputOp, Unit, E, O, Nothing, Nothing] {
def hints: smithy4s.Hints = {
val newHttp = smithy.api.Http(
method = smithy.api.NonEmptyString("GET"),
Expand All @@ -263,8 +261,8 @@ abstract class ServerHttpComplianceTestCase[
// format: off
new Service.Reflective[NoInputOp] {
override def id: ShapeId = ShapeId("custom", "service")
override def endpoints: List[Endpoint[NoInputOp, _, _, _, _, _]] = List(amendedEndpoint)
override def endpoint[I_, E_, O_, SI_, SO_](op: NoInputOp[I_, E_, O_, SI_, SO_]): (I_, Endpoint[NoInputOp, I_, E_, O_, SI_, SO_]) = ???
override def endpoints: List[Endpoint[_, _, _, _, _]] = List(amendedEndpoint)
override def endpoint[I_, E_, O_, SI_, SO_](op: NoInputOp[I_, E_, O_, SI_, SO_]): (I_, Endpoint[I_, E_, O_, SI_, SO_]) = ???
override def version: String = originalService.version
override def hints: Hints = originalService.hints
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@ import weaver._
import smithy4s.Service

object WeaverComplianceTest extends SimpleIOSuite {
val clientTestGenerator = new ClientHttpComplianceTestCase[
alloy.SimpleRestJson,
HelloServiceGen,
HelloServiceOperation
](
alloy.SimpleRestJson()
val clientTestGenerator = new ClientHttpComplianceTestCase(
alloy.SimpleRestJson(),
HelloService
) {
import org.http4s.implicits._
private val baseUri = uri"http://localhost/"
Expand All @@ -44,20 +41,15 @@ object WeaverComplianceTest extends SimpleIOSuite {
def codecs = SimpleRestJsonBuilder.codecs
}

val serverTestGenerator = new ServerHttpComplianceTestCase[
alloy.SimpleRestJson,
HelloServiceGen,
HelloServiceOperation
](
alloy.SimpleRestJson()
) {
def getServer[Alg2[_[_, _, _, _, _]], Op2[_, _, _, _, _]](
impl: smithy4s.kinds.FunctorAlgebra[Alg2, IO]
)(implicit s: Service[Alg2, Op2]): Resource[IO, HttpRoutes[IO]] =
SimpleRestJsonBuilder(s).routes(impl).resource
val serverTestGenerator =
new ServerHttpComplianceTestCase(alloy.SimpleRestJson(), HelloService) {
def getServer[Alg2[_[_, _, _, _, _]]](
impl: smithy4s.kinds.FunctorAlgebra[Alg2, IO]
)(implicit s: Service[Alg2]): Resource[IO, HttpRoutes[IO]] =
SimpleRestJsonBuilder(s).routes(impl).resource

def codecs = SimpleRestJsonBuilder.codecs
}
def codecs = SimpleRestJsonBuilder.codecs
}

val tests: List[ComplianceTest[IO]] =
clientTestGenerator.allClientTests() ++ serverTestGenerator.allServerTests()
Expand Down
Loading