Reputation: 1766
I am using LanguageExt to have functional programming features in C#. I have a method in which I want to build an instance of VaultSharp to access our HashiCorp Vault service. My goal is to create an instance of VaultClientSettings (see methods below) through a chain of two Eithers. In the end, either return an exception from any Either in the chain or the instance of VaultClientSettings. I think that I am close but cannot make the last step work. I'd appreciate your suggestions.
Here are links to the FP library for C# and the VaultSharp library;
Here is an image showing the error that I am seeing:
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;
}));
}
Upvotes: 0
Views: 745
Reputation: 862
As @hayden already noted: bind type argument was wrong (needs to be "right" type of result either type).
For LanguageExt: You can even omit the type argument if you return the correct type:
Either<Exception, VaultClientSettings> settings =
vaultUri.Bind(u =>
authInfo.Bind(a =>
{
Either<Exception, VaultClientSettings> vaultClientSettings =
new VaultClientSettings(u.AbsoluteUri, a);
return vaultClientSettings;
}));
There is another form for this code (LINQ) which may be more readable to you:
var settings = from u in vaultUri
from a in authInfo
select new VaultClientSettings(u.AbsoluteUri, a);
Essentially Bind
is SelectMany
(from ...)
Upvotes: 5
Reputation: 2988
Haven't used either library, but looking at the signature for Bind
:
Either<L, B> Bind<B>(Func<R, Either<L, B>> f)
Judging by the signature, the following should be valid:
Either<Exception, VaultClientSettings> settings =
vaultUri.Bind<VaultClientSettings>(u =>
authInfo.Bind<VaultClientSettings>(a =>
{
Either<Exception, VaultClientSettings> vaultClientSettings = new VaultClientSettings(u.AbsoluteUri, a);
return vaultClientSettings;
}));
Upvotes: 1