LangageExt:使用 Bind() 链接两个 Either,但无法弄清楚如何使用它们创建另一个 Either
LangageExt: Chaining of two Eithers with Bind(), but cannot figure out how to use them to create another Either
我正在使用 LanguageExt 来获得 C# 中的函数式编程功能。我有一个方法,我想在其中构建一个 VaultSharp 实例来访问我们的 HashiCorp Vault 服务。我的目标是通过两个 Eithers 链创建一个 VaultClientSettings 实例(参见下面的方法)。最后,return 链中任何 Either 的异常或
VaultClient 设置。我认为我很接近但无法完成最后一步。我会很感激你的建议。
这里是 C# 的 FP 库和 VaultSharp 库的链接;
- https://github.com/louthy/language-ext/tree/main/LanguageExt.Core
- https://github.com/rajanadar/VaultSharp
这是一张显示我看到的错误的图片:
Either<Exception, Uri> GetVaultUri() =>
EnvironmentVariable.GetEnvironmentVariable(KVaultAddressEnvironmentVariableName)
.Map(uri => new Uri(uri));
Either<Exception, TokenAuthMethodInfo> GetAuthInfo() =>
EnvironmentVariable.GetEnvironmentVariable(KVaultTokenEnvironmentVariableName)
.Map(token => new TokenAuthMethodInfo(token));
Either<Exception, VaultClientSettings> GetVaultClientSettings(
Either<Exception, Uri> vaultUri,
Either<Exception, TokenAuthMethodInfo> authInfo
)
{
/////////////////////////////////////////////////////////////////////////
// I have access to the uri as u and the authmethod as a, but I cannot //
// figure out how to create the instance of VaultClientSettings. //
Either<Exception, VaultClientSettings> settings =
vaultUri.Bind<Uri>(u =>
authInfo.Bind<TokenAuthMethodInfo>(a =>
{
Either<Exception, VaultClientSettings> vaultClientSettings =
new VaultClientSettings(u.AbsoluteUri, a);
return vaultClientSettings;
}));
}
没有使用过任何一个库,但正在查看 Bind
的签名:
Either<L, B> Bind<B>(Func<R, Either<L, B>> f)
根据签名判断,以下内容应该是有效的:
Either<Exception, VaultClientSettings> settings =
vaultUri.Bind<VaultClientSettings>(u =>
authInfo.Bind<VaultClientSettings>(a =>
{
Either<Exception, VaultClientSettings> vaultClientSettings = new VaultClientSettings(u.AbsoluteUri, a);
return vaultClientSettings;
}));
正如@hayden 已经指出的那样:绑定类型参数是错误的(需要是“正确”类型的结果)。
对于 LanguageExt:如果 return 正确的类型,您甚至可以省略类型参数:
Either<Exception, VaultClientSettings> settings =
vaultUri.Bind(u =>
authInfo.Bind(a =>
{
Either<Exception, VaultClientSettings> vaultClientSettings =
new VaultClientSettings(u.AbsoluteUri, a);
return vaultClientSettings;
}));
此代码 (LINQ) 有另一种形式,您可能更易读:
var settings = from u in vaultUri
from a in authInfo
select new VaultClientSettings(u.AbsoluteUri, a);
本质上 Bind
是 SelectMany
(来自...)
我正在使用 LanguageExt 来获得 C# 中的函数式编程功能。我有一个方法,我想在其中构建一个 VaultSharp 实例来访问我们的 HashiCorp Vault 服务。我的目标是通过两个 Eithers 链创建一个 VaultClientSettings 实例(参见下面的方法)。最后,return 链中任何 Either 的异常或 VaultClient 设置。我认为我很接近但无法完成最后一步。我会很感激你的建议。
这里是 C# 的 FP 库和 VaultSharp 库的链接;
- https://github.com/louthy/language-ext/tree/main/LanguageExt.Core
- https://github.com/rajanadar/VaultSharp
这是一张显示我看到的错误的图片:
Either<Exception, Uri> GetVaultUri() =>
EnvironmentVariable.GetEnvironmentVariable(KVaultAddressEnvironmentVariableName)
.Map(uri => new Uri(uri));
Either<Exception, TokenAuthMethodInfo> GetAuthInfo() =>
EnvironmentVariable.GetEnvironmentVariable(KVaultTokenEnvironmentVariableName)
.Map(token => new TokenAuthMethodInfo(token));
Either<Exception, VaultClientSettings> GetVaultClientSettings(
Either<Exception, Uri> vaultUri,
Either<Exception, TokenAuthMethodInfo> authInfo
)
{
/////////////////////////////////////////////////////////////////////////
// I have access to the uri as u and the authmethod as a, but I cannot //
// figure out how to create the instance of VaultClientSettings. //
Either<Exception, VaultClientSettings> settings =
vaultUri.Bind<Uri>(u =>
authInfo.Bind<TokenAuthMethodInfo>(a =>
{
Either<Exception, VaultClientSettings> vaultClientSettings =
new VaultClientSettings(u.AbsoluteUri, a);
return vaultClientSettings;
}));
}
没有使用过任何一个库,但正在查看 Bind
的签名:
Either<L, B> Bind<B>(Func<R, Either<L, B>> f)
根据签名判断,以下内容应该是有效的:
Either<Exception, VaultClientSettings> settings =
vaultUri.Bind<VaultClientSettings>(u =>
authInfo.Bind<VaultClientSettings>(a =>
{
Either<Exception, VaultClientSettings> vaultClientSettings = new VaultClientSettings(u.AbsoluteUri, a);
return vaultClientSettings;
}));
正如@hayden 已经指出的那样:绑定类型参数是错误的(需要是“正确”类型的结果)。
对于 LanguageExt:如果 return 正确的类型,您甚至可以省略类型参数:
Either<Exception, VaultClientSettings> settings =
vaultUri.Bind(u =>
authInfo.Bind(a =>
{
Either<Exception, VaultClientSettings> vaultClientSettings =
new VaultClientSettings(u.AbsoluteUri, a);
return vaultClientSettings;
}));
此代码 (LINQ) 有另一种形式,您可能更易读:
var settings = from u in vaultUri
from a in authInfo
select new VaultClientSettings(u.AbsoluteUri, a);
本质上 Bind
是 SelectMany
(来自...)