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

Example showing right-biased validation #1

Open
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+30 −29
Diff settings

Always

Just for now

@@ -28,6 +28,14 @@ import annotation.tailrec
trait HostConfigProvider extends (String Validated[HostConfig])
object Implicits {
implicit class RightBiasedValidation[+A](val e: Validated[A]) extends AnyVal {
def foreach[U](f: A U): Unit = e.right.foreach(f)
def map[B](f: A B): Validated[B] = e.right.map(f)
def flatMap[B](f: A Validated[B]) = e.right.flatMap(f)
}
}
object HostConfigProvider {
implicit def login2HostConfigProvider(login: SshLogin) = new HostConfigProvider {
def apply(host: String) = Right(HostConfig(login = login, hostName = host))
@@ -58,36 +66,29 @@ object HostConfig {
abstract class FromStringsHostConfigProvider extends HostConfigProvider {
def rawLines(host: String): Validated[(String, TraversableOnce[String])]
import Implicits._
def apply(host: String) = {
rawLines(host).right.flatMap {
case (source, lines)
splitToMap(lines, source).right.flatMap { settings
login(settings, source).right.flatMap { login
optIntSetting("port", settings, source).right.flatMap { port
optIntSetting("connect-timeout", settings, source).right.flatMap { connectTimeout
optIntSetting("connection-timeout", settings, source).right.flatMap { connectionTimeout
optIntSetting("command-timeout", settings, source).right.flatMap { commandTimeout
optBoolSetting("enable-compression", settings, source).right.flatMap { enableCompression
setting("fingerprint", settings, source).right.map(forFingerprint).left.flatMap(_ KnownHosts).right.map { verifier
HostConfig(
login,
hostName = setting("host-name", settings, source).right.toOption.getOrElse(host),
port = port.getOrElse(22),
connectTimeout = connectTimeout,
connectionTimeout = connectionTimeout,
commandTimeout = commandTimeout,
enableCompression = enableCompression.getOrElse(false),
hostKeyVerifier = verifier
)
}
}
}
}
}
}
}
}
}
for {
inter rawLines(host).right
(source, lines) = inter
settings splitToMap(lines, source).right
login login(settings, source).right
port optIntSetting("port", settings, source).right
connectTimeout optIntSetting("connect-timeout", settings, source).right
connectionTimeout optIntSetting("connection-timeout", settings, source).right
commandTimeout optIntSetting("command-timeout", settings, source).right
enableCompression optBoolSetting("enable-compression", settings, source).right
verifier setting("fingerprint", settings, source).right.map(forFingerprint).left.flatMap(_ KnownHosts).right
} yield HostConfig(
login,
hostName = setting("host-name", settings, source).right.toOption.getOrElse(host),
port = port.getOrElse(22),
connectTimeout = connectTimeout,
connectionTimeout = connectionTimeout,
commandTimeout = commandTimeout,
enableCompression = enableCompression.getOrElse(false),
hostKeyVerifier = verifier
)
}
private def login(settings: Map[String, String], source: String) = {