Skip to content

Commit

Permalink
Merge pull request #16 from takapi327/refactor/2022-05-Pre-release-ad…
Browse files Browse the repository at this point in the history
…justments

Refactor/2022 05 pre release adjustments
  • Loading branch information
takapi327 authored May 25, 2022
2 parents a08e7a6 + 53f2b98 commit 1b6cf38
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 29 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

<div align="center">
<img src="./documentation/images/lepus_logo.png" style="width:180px">
<img src="images/lepus_logo.png" style="width:180px">
<h1>Lepus Framework</h1>
<img src="https://img.shields.io/badge/lepus-v0.0.0-blue">
<img src="https://img.shields.io/badge/lepus-v0.1.0-blue">
<a href="https://en.wikipedia.org/wiki/MIT_License">
<img src="https://img.shields.io/badge/license-MIT-green">
</a>
Expand Down Expand Up @@ -131,13 +131,13 @@ import io.circe.generic.semiauto._

import lepus.router._
import lepus.router.http._
import lepus.router.syntax._
import lepus.router.model.ServerResponse
import lepus.router.generic.SchemaDerivation

case class Sample(info: String)
object Sample extends SchemaDerivation {
object Sample {
implicit lazy val encoder: Encoder[Sample] = deriveEncoder
implicit val schema = gen[Sample]
implicit val schema = schemaGen[Sample]
}

object HelloRoute extends RouterConstructor[IO, (String, Long)] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* file that was distributed with this source code.
*/

package lepus.router.mvc
package lepus.router

import java.nio.charset.StandardCharsets._

Expand All @@ -14,7 +14,7 @@ import io.circe.{ CursorOp, Decoder, DecodingFailure, Encoder, Errors, ParsingFa

import fs2._

import lepus.router.mvc.ConvertResult._
import ConvertResult._

trait BodyConverter[T] {
def decode(s: String): ConvertResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
* file that was distributed with this source code.
*/

package lepus.router.mvc
package lepus.router

import java.time._
import java.util.UUID

import scala.annotation._
import scala.reflect.ClassTag
import scala.util._

import lepus.router.model._
import Schema._

import scala.reflect.ClassTag

@implicitNotFound("Could not find an implicit EndpointConverter[${S}, ${T}]")
trait EndpointConverter[S, T] {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package lepus.router

import lepus.router.http._
import lepus.router.mvc.EndpointConverter

trait LepusRouter {
implicit def stringToPath(str: String): RequestEndpoint.FixedPath[String] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.http4s.{ Request => Http4sRequest, HttpRoutes => Http4sRoutes }

import lepus.router.http._
import lepus.router.model.{ ServerRequest, ServerResponse }
import lepus.router.mvc.ConvertResult._
import ConvertResult._

/** Compare and verify Http requests and endpoints, and combine them with logic.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* file that was distributed with this source code.
*/

package lepus.router.mvc
package lepus.router

import lepus.router.model.DecodeResult

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ trait SchemaDerivation {
)
}

implicit def gen[T]: Schema[T] = macro Magnolia.gen[T]

private def typeNameToSchemaName(typeName: TypeName): Schema.Name =
Schema.Name(
fullName = typeName.full,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package lepus.router.http

import lepus.router.mvc._
import lepus.router.{ EndpointConverter, Validator }

object RequestEndpoint {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import fs2.Stream
import org.http4s.{ Response, Uri, Headers => Http4sHeaders }
import org.http4s.headers.Location

import lepus.router.http.ResponseStatus
import lepus.router.http.Header
import lepus.router.http.Header.ResponseHeader
import lepus.router.mvc.ConvertResult
import lepus.router.http.{ ResponseStatus, Header }
import lepus.router.ConvertResult
import Header.ResponseHeader

/** A model of the response to be returned in response to a received request.
*
Expand Down
10 changes: 9 additions & 1 deletion core/lepus-router/src/main/scala/lepus/router/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

package lepus

import language.experimental.macros

import magnolia1._

import cats.Semigroup
import cats.syntax.semigroupk._

Expand All @@ -12,7 +16,7 @@ import cats.effect.IO
import org.http4s.{ HttpRoutes => Http4sRoutes }

import lepus.router.http.RequestMethod
import lepus.router.model.{ ServerRequest, ServerResponse }
import lepus.router.model.{ Schema, ServerRequest, ServerResponse }

package object router extends LepusRouter with ExtensionMethods {

Expand All @@ -23,4 +27,8 @@ package object router extends LepusRouter with ExtensionMethods {
type HttpRoutes[F[_], T] = Http[ServerRequest[F, T] => F[ServerResponse]]

implicit val routesSemigroup: Semigroup[Http4sRoutes[IO]] = _ combineK _

object syntax extends generic.SchemaDerivation {
implicit def schemaGen[T]: Schema[T] = macro Magnolia.gen[T]
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package lepus.router.mvc
/** This file is part of the Lepus Framework. For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

package lepus.router

import io.circe.generic.semiauto.{ deriveDecoder, deriveEncoder }
import io.circe.{ Decoder, Encoder }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
* file that was distributed with this source code.
*/

package lepus.router.mvc
package lepus.router

import java.time._
import java.util.UUID

import scala.reflect.ClassTag

import org.scalacheck.{ Arbitrary, Gen }
import org.scalacheck.Prop._
import org.scalacheck.{ Arbitrary, Gen }

import org.scalatest.Assertion
import org.scalatest.flatspec.AnyFlatSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* file that was distributed with this source code.
*/

package lepus.router.mvc
package lepus.router

import org.specs2.mutable.Specification

Expand Down
58 changes: 54 additions & 4 deletions core/lepus/src/main/scala/lepus/core/util/Configuration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

package lepus.core.util

import java.time.{ Duration => JavaDuration }

import scala.jdk.CollectionConverters._
import scala.concurrent.duration.{ Duration, FiniteDuration, _ }

import com.typesafe.config._

Expand All @@ -31,8 +34,13 @@ object Configuration {
def load(): Configuration = Configuration(ConfigFactory.load())
}

trait ConfigLoader[A] {
trait ConfigLoader[A] { self =>
def load(config: Config, path: String): A
def map[B](f: A => B): ConfigLoader[B] = new ConfigLoader[B] {
def load(config: Config, path: String): B = {
f(self.load(config, path))
}
}
}

object ConfigLoader {
Expand All @@ -42,9 +50,51 @@ object ConfigLoader {
f(config)(path)
}

implicit val string: ConfigLoader[String] = ConfigLoader(_.getString)
implicit val int: ConfigLoader[Int] = ConfigLoader(_.getInt)
implicit val long: ConfigLoader[Long] = ConfigLoader(_.getLong)
implicit val string: ConfigLoader[String] = ConfigLoader(_.getString)
implicit val int: ConfigLoader[Int] = ConfigLoader(_.getInt)
implicit val long: ConfigLoader[Long] = ConfigLoader(_.getLong)
implicit val number: ConfigLoader[Number] = ConfigLoader(_.getNumber)
implicit val double: ConfigLoader[Double] = ConfigLoader(_.getDouble)
implicit val bytes: ConfigLoader[ConfigMemorySize] = ConfigLoader(_.getMemorySize)
implicit val finiteDuration: ConfigLoader[FiniteDuration] = ConfigLoader(_.getDuration).map(_.toNanos.nanos)
implicit val javaDuration: ConfigLoader[JavaDuration] = ConfigLoader(_.getDuration)
implicit val scalaDuration: ConfigLoader[Duration] = ConfigLoader(config =>
path =>
if (config.getIsNull(path)) {
Duration.Inf
} else {
config.getDuration(path).toNanos.nanos
}
)

implicit val seqBoolean: ConfigLoader[Seq[Boolean]] =
ConfigLoader(_.getBooleanList).map(_.asScala.map(_.booleanValue).asInstanceOf[Seq[Boolean]])
implicit val seqInt: ConfigLoader[Seq[Int]] =
ConfigLoader(_.getIntList).map(_.asScala.map(_.toInt).asInstanceOf[Seq[Int]])
implicit val seqLong: ConfigLoader[Seq[Long]] =
ConfigLoader(_.getDoubleList).map(_.asScala.map(_.longValue).asInstanceOf[Seq[Long]])
implicit val seqNumber: ConfigLoader[Seq[Number]] =
ConfigLoader(_.getNumberList).map(_.asScala.asInstanceOf[Seq[Number]])
implicit val seqDouble: ConfigLoader[Seq[Double]] =
ConfigLoader(_.getDoubleList).map(_.asScala.map(_.doubleValue).asInstanceOf[Seq[Double]])
implicit val seqString: ConfigLoader[Seq[String]] =
ConfigLoader(_.getStringList).map(_.asScala.asInstanceOf[Seq[String]])
implicit val seqBytes: ConfigLoader[Seq[ConfigMemorySize]] =
ConfigLoader(_.getMemorySizeList).map(_.asScala.asInstanceOf[Seq[ConfigMemorySize]])
implicit val seqFinite: ConfigLoader[Seq[FiniteDuration]] =
ConfigLoader(_.getDurationList).map(_.asScala.map(_.toNanos.nanos).asInstanceOf[Seq[FiniteDuration]])
implicit val seqJavaDuration: ConfigLoader[Seq[JavaDuration]] =
ConfigLoader(_.getDurationList).map(_.asScala.asInstanceOf[Seq[JavaDuration]])
implicit val seqScalaDuration: ConfigLoader[Seq[Duration]] =
ConfigLoader(_.getDurationList).map(_.asScala.map(_.toNanos.nanos).asInstanceOf[Seq[Duration]])

implicit val config: ConfigLoader[Config] = ConfigLoader(_.getConfig)
implicit val configObject: ConfigLoader[ConfigObject] = ConfigLoader(_.getObject)
implicit val configList: ConfigLoader[ConfigList] = ConfigLoader(_.getList)
implicit val seqConfig: ConfigLoader[Seq[Config]] =
ConfigLoader(_.getConfigList).map(_.asScala.asInstanceOf[Seq[Config]])
implicit val configuration: ConfigLoader[Configuration] = config.map(Configuration(_))
implicit val seqConfiguration: ConfigLoader[Seq[Configuration]] = seqConfig.map(_.map(Configuration(_)))

implicit def optionA[A](implicit loader: ConfigLoader[A]): ConfigLoader[Option[A]] =
new ConfigLoader[Option[A]] {
Expand Down
Binary file removed documentation/images/lepus_logo.png
Binary file not shown.
Binary file added images/lepus_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1b6cf38

Please sign in to comment.