Skip to content

Instantly share code, notes, and snippets.

@pvillega
Created June 19, 2017 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pvillega/dfde4c8c5a7d966acaa0b22d869fc249 to your computer and use it in GitHub Desktop.
Save pvillega/dfde4c8c5a7d966acaa0b22d869fc249 to your computer and use it in GitHub Desktop.
Extract of classes from FreeStyle that give me the error
import freestyle._
import freestyle.implicits._
import freestyle.logging._
import freestyle.cache._
import freestyle.loggingJVM.implicits._
object all {
// Some model classes for tthe example
type Key = String
type AnswerMap = Map[Key, Answer[_]]
case class Answer[T](k: Key, v: T)
final case class UserAnswers(id: Long, answerMap: AnswerMap)
// I define a single Algebra and 2 Modules
@free
trait Storage {
def get(id: Long): FS[Option[UserAnswers]]
def delete(id: Long): FS[Unit]
def save(id: Long, answerMap: AnswerMap): FS[Unit]
def all: FS[List[UserAnswers]]
}
val cacheP = new KeyValueProvider[Long, UserAnswers]
@module
trait Persistence {
val storage: Storage
}
@module
trait App {
val persistence: Persistence
val cacheM: cacheP.CacheM
val log: LoggingM
}
// a simple program that will load the data from storage
def getAllUserAnswers[F[_]](implicit app: App[F]): FreeS[F, List[UserAnswers]] = {
import app._, app.persistence._
for {
answersList <- storage.all
_ <- log.info(s"Loaded all ${answersList.size} user answers from db.")
} yield answersList
}
// define interpreters for Storage algebra and Cache using a TestStack type
type TestStack[A] = Kleisli[Id, Config, A]
implicit val cacheHandler: cacheP.CacheM.Handler[TestStack] = {
val rawMap: KeyValueMap[Id, Long, UserAnswers] =
new ConcurrentHashMapWrapper[Id, Long, UserAnswers]
val cacheIdToStack: Id ~> TestStack =
new (Id ~> TestStack) {
def apply[A](a: Id[A]): TestStack[A] = Kleisli.pure(a)
}
cacheP.implicits.cacheHandler(rawMap, cacheIdToStack)
}
implicit val inMemoryStorageHandler: Storage.Handler[TestStack] = new Storage.Handler[TestStack] {
val rawMap: KeyValueMap[Id, Long, UserAnswers] =
new ConcurrentHashMapWrapper[Id, Long, UserAnswers]
def get(id: Long): TestStack[Option[UserAnswers]] =
Kleisli.lift(rawMap.get(id))
def delete(id: Long): TestStack[Unit] =
Kleisli.pure(rawMap.delete(id))
def save(id: Long, answerMap: AnswerMap): TestStack[Unit] =
Kleisli.pure(rawMap.put(id, UserAnswers(id, answerMap)))
def all: TestStack[List[UserAnswers]] =
Kleisli.pure(rawMap.keys.flatMap(k => rawMap.get(k).toList))
}
// complains some handler is missing. If I remove 'log' from module App it works. I expected 'freestyle.loggingJVM.implicits._'
// would make it work, but...no
getAllUserAnswers[App.Op].interpret[TestStack]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment