Skip to content

Commit

Permalink
Merge pull request #1785 from zeal18/more-independency
Browse files Browse the repository at this point in the history
Degeneralize server and client generators
  • Loading branch information
blast-hardcheese committed Oct 10, 2023
2 parents 5209d35 + e69b7c9 commit 34957b4
Show file tree
Hide file tree
Showing 32 changed files with 5,961 additions and 2,424 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
- name: Set scala version for matrix
id: set-scala-versions # The greps on the following line are to ensure as much as possible that we've caught the right line
run: echo "::set-output name=scala_versions::$(sbt 'print githubMatrixSettings' | grep '^\[{' | grep 'bincompat' | tail -n 1)"
run: echo "scala_versions=$(sbt 'print githubMatrixSettings' | grep '^\[{' | grep 'bincompat' | tail -n 1)" >> $GITHUB_OUTPUT
java:
runs-on: ubuntu-20.04
needs: [core]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
run: |
module="$(echo "$GITHUB_REF" | sed 's~^refs/tags/\(.*\)-v[0-9.]\+$~\1~')"
echo "extract project: ${GITHUB_REF}, ${module}"
echo "::set-output name=module::$module"
echo "module=$module" >> $GITHUB_OUTPUT
- uses: actions/checkout@v2
with:
fetch-depth: 0
Expand Down
19 changes: 11 additions & 8 deletions modules/core/src/main/scala/dev/guardrail/Common.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import java.nio.file.Path
import java.net.URI

import dev.guardrail.core.{ SupportDefinition, Tracker }
import dev.guardrail.generators.{ ClientGenerator, Clients, ProtocolDefinitions, ProtocolGenerator, ServerGenerator, Servers }
import dev.guardrail.generators.{ Clients, Servers }
import dev.guardrail.generators.ProtocolDefinitions
import dev.guardrail.languages.LA
import dev.guardrail.terms.client.ClientTerms
import dev.guardrail.terms.framework.FrameworkTerms
Expand All @@ -35,12 +36,16 @@ object Common {
Se: ServerTerms[L, F],
Sw: SwaggerTerms[L, F]
): F[(ProtocolDefinitions[L], CodegenDefinitions[L])] = {
import Fw._
import Fw.{ getFrameworkImports, getFrameworkImplicits }
import Sw._

Sw.log.function("prepareDefinitions")(for {
proto @ ProtocolDefinitions(protocolElems, protocolImports, packageObjectImports, packageObjectContents, _) <- ProtocolGenerator
.fromSwagger[L, F](swagger, dtoPackage, supportPackage, context.propertyRequirement)
proto @ ProtocolDefinitions(protocolElems, protocolImports, packageObjectImports, packageObjectContents, _) <- P.fromSwagger(
swagger,
dtoPackage,
supportPackage,
context.propertyRequirement
)

serverUrls = NonEmptyList.fromList(
swagger
Expand Down Expand Up @@ -87,16 +92,14 @@ object Common {
codegen <- kind match {
case CodegenTarget.Client =>
for {
clientMeta <- ClientGenerator
.fromSwagger[L, F](context, frameworkImports)(serverUrls, basePath, groupedRoutes)(protocolElems, securitySchemes, components)
clientMeta <- C.fromSwagger(context, frameworkImports)(serverUrls, basePath, groupedRoutes)(protocolElems, securitySchemes, components)
Clients(clients, supportDefinitions) = clientMeta
frameworkImplicits <- getFrameworkImplicits()
} yield CodegenDefinitions[L](clients, List.empty, supportDefinitions, frameworkImplicits)

case CodegenTarget.Server =>
for {
serverMeta <- ServerGenerator
.fromSwagger[L, F](context, supportPackage, basePath, frameworkImports)(groupedRoutes)(protocolElems, securitySchemes, components)
serverMeta <- Se.fromSwagger(context, supportPackage, basePath, frameworkImports)(groupedRoutes)(protocolElems, securitySchemes, components)
Servers(servers, supportDefinitions) = serverMeta
frameworkImplicits <- getFrameworkImplicits()
} yield CodegenDefinitions[L](List.empty, servers, supportDefinitions, frameworkImplicits)
Expand Down
15 changes: 10 additions & 5 deletions modules/core/src/main/scala/dev/guardrail/core/Tracker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ import cats.Functor
* Tracker heavily utilizes syntax from IndexedFunctor and IndexedDistributive. These classes are similar to Functor and Traversable, except they expose the
* index into the structure they are walking over. This is used to automatically build the history while walking structures.
*
* val tracker: Tracker[ OpenAPI ] = Tracker(openAPI) val servers: Tracker[List[ Server ]] = tracker.downField("servers", _.getServers) val firstServer:
* Tracker[Option[ Server ]] = servers.map(_.headOption) val firstServerUrl: Tracker[Option[ String ]] = firstServer.flatDownField("url", _.getUrl)
* ```scala
* val tracker: Tracker[OpenAPI] = Tracker(openAPI)
* val servers: Tracker[List[Server]] = tracker.downField("servers", _.getServers)
* val firstServer: Tracker[Option[Server]] = servers.map(_.headOption)
* val firstServerUrl: Tracker[Option[String]] = firstServer.flatDownField("url", _.getUrl)
*
* val trackedUrl: Tracker[Option[ URL ]] = firstServerUrl.map(_.map(new URL(_)))
* val trackedUrl: Tracker[Option[URL]] = firstServerUrl.map(_.map(new URL(_)))
*
* // Examples of extracting: val firstServerUrl: Option[ URL ] = trackedUrl.unwrapTracker // Throw away history val firstServerUrl: Target[ URL ] =
* trackedUrl.raiseErrorIfEmpty("No Server URL found!") // Append history to the end of the error message
* // Examples of extracting:
* val firstServerUrl: Option[URL] = trackedUrl.unwrapTracker // Throw away history
* val firstServerUrl: Target[URL] = trackedUrl.raiseErrorIfEmpty("No Server URL found!") // Append history to the end of the error message
* ```
*/
class Tracker[+A] private[core] (private[core] val get: A, private[core] val history: Vector[String]) {
override def toString(): String = s"Tracker($get, $history)"
Expand Down

This file was deleted.

21 changes: 21 additions & 0 deletions modules/core/src/main/scala/dev/guardrail/generators/Clients.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.guardrail.generators

import cats.data.NonEmptyList

import dev.guardrail.core.SupportDefinition
import dev.guardrail.languages.LA
import dev.guardrail.terms.protocol.StaticDefns

case class Clients[L <: LA](clients: List[Client[L]], supportDefinitions: List[SupportDefinition[L]])
case class Client[L <: LA](
pkg: List[String],
clientName: String,
imports: List[L#Import],
staticDefns: StaticDefns[L],
client: NonEmptyList[Either[L#Trait, L#ClassDefinition]],
responseDefinitions: List[L#Definition]
)
case class RenderedClientOperation[L <: LA](
clientOperation: L#Definition,
supportDefinitions: List[L#Definition]
)
Loading

0 comments on commit 34957b4

Please sign in to comment.