小编Dan*_* Li的帖子

使用akka-http执行OAuth2身份验证的最佳方法是什么?

Akka HTTP和Spray提供了一个authenticateOAuth2指令,但他们的文档说明了这一点

该指令不实现完整的OAuth2协议,而是通过从HTTP头中提取所需的令牌来实现它.

我也找不到任何为Akka HTTP或Spray实现OAuth2的库.有什么我想念的,或者这只是现在这些库的状态?

scala oauth-2.0 spray akka-http

16
推荐指数
1
解决办法
2821
查看次数

我如何等待Scala future的onSuccess回调完成?

在Scala中,我可以Await用来等待未来完成.但是,如果我已经注册了一个回调,以便在完成该未来的情况下运行,那么我怎样才能等待未来完成以及回调完成?

这是一个最小但完整的程序来说明问题:

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.concurrent.{ Await, Future }

object Main {
  def main(args: Array[String]): Unit = {
    val f: Future[Int] = Future(0)
    f.onSuccess { case _ =>
      Thread.sleep(10000)
      println("The program waited patiently for this callback to finish.")
    }

    // This waits for `f` to complete but doesn't wait for the callback
    // to finish running.
    Await.ready(f, Duration.Inf)
  }
}
Run Code Online (Sandbox Code Playgroud)

我希望输出为:

The program waited patiently for this callback to finish.
Run Code Online (Sandbox Code Playgroud)

相反,没有输出; 程序在回调结束前退出.

请注意,这与等待未来完成的问题不同,这已在此问题中得到解答.

concurrency scala future

14
推荐指数
2
解决办法
1万
查看次数

scala.util.Try比try..catch有什么优势?

在线搜索答案给出了两个突出的帖子(CodacyDaniel Westheide),两者都给出了与Scala官方文档Try相同的答案:

上面示例中显示的Try的一个重要属性是它能够管理或链接操作,沿途捕获异常.

上面引用的示例是:

import scala.io.StdIn
import scala.util.{Try, Success, Failure}

def divide: Try[Int] = {
  val dividend = Try(StdIn.readLine("Enter an Int that you'd like to divide:\n").toInt)
  val divisor = Try(StdIn.readLine("Enter an Int that you'd like to divide by:\n").toInt)
  val problem = dividend.flatMap(x => divisor.map(y => x/y))
  problem match {
    case Success(v) =>
      println("Result of " + dividend.get + "/"+ divisor.get +" is: " + v)
      Success(v)
    case Failure(e) =>
      println("You must've divided by zero …
Run Code Online (Sandbox Code Playgroud)

monads scala try-catch codacy

7
推荐指数
1
解决办法
1800
查看次数

如何获取传递给 Scala 宏的参数的运行时值?

我有一个表面上很简单的宏观问题,我已经用头撞了几个小时,但没有运气。也许有更多经验的人可以提供帮助。

我有以下宏:

import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context

object MacroObject {
  def run(s: String): Unit =
    macro runImpl

  def runImpl(c: Context)(s: c.Tree): c.Tree = {
    import c.universe._
    println(s)    // <-- I need the macro to know the value of s at compile time
    q"()"
  }
}
Run Code Online (Sandbox Code Playgroud)

问题是:我希望宏知道s传递给它的值——不是 AST s,而是s它本身的值。具体来说,我希望它具有这种行为:

def runTheMacro(str: String): Unit = MacroObject.run(str)

final val HardCodedString1 = "Hello, world!"
runTheMacro(HardCodedString1)    // the macro should print "Hello, world!"
                                 // to the console during macro expansion …
Run Code Online (Sandbox Code Playgroud)

macros scala scala-macros scala-quasiquotes scala-reflect

6
推荐指数
1
解决办法
528
查看次数

`git reflog` 默认输出的格式字符串是什么?

我想调整 的默认输出git reflog,但我首先需要格式字符串来重现默认输出。具体来说,我在为不同的引用名称着色时遇到了问题:

git-reflog

有更懂 Git 的人可以帮我吗?

git

5
推荐指数
1
解决办法
749
查看次数

使用Slick重构期间键入不匹配

我有以下一段代码我想制作DRYer:

def createAdmin(/* ... */): Future[Int] =
  db.run {
    {
      (users returning users.map(_.id)) += Account(0, /* ... */)
    } flatMap { id =>
      admins += Admin(userId = id, /* ... */)
    }
  }

def createStandardUser(/* ... */): Future[Int] =
  db.run {
    {
      (users returning users.map(_.id)) += Account(0, /* ... */)
    } flatMap { id =>
      standardUsers += StandardUser(userId = id, /* ... */)
    }
  }
Run Code Online (Sandbox Code Playgroud)

编译好.但如果我将两者合并到以下内容中:

def createUser(role: String)(/* ... */): Future[Int] =
  db.run {
    {
      (users returning users.map(_.id)) += …
Run Code Online (Sandbox Code Playgroud)

scala slick

4
推荐指数
1
解决办法
1061
查看次数