Skip to content
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

feat: lower target framework to netstandard2.0 #425

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Momento.Sdk/Auth/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Momento.Sdk.Auth;

internal class TokenAndEndpoints
{
public string AuthToken { get; }
public string AuthToken { get; }
public string ControlEndpoint { get; }
public string CacheEndpoint { get; }

Expand All @@ -29,8 +29,15 @@ internal static class AuthUtils
{
public static bool IsBase64String(string base64)
{
Span<byte> buffer = new Span<byte>(new byte[base64.Length]);
return Convert.TryFromBase64String(base64, buffer, out int bytesParsed);
try
{
Convert.FromBase64String(base64);
return true;
}
catch (Exception)
{
return false;
}
}

public static TokenAndEndpoints TryDecodeAuthToken(string authToken)
Expand All @@ -47,7 +54,7 @@ public static TokenAndEndpoints TryDecodeAuthToken(string authToken)
throw new InvalidArgumentException("");
}
return new TokenAndEndpoints(
decodedToken.api_key,
decodedToken.api_key!,
"control." + decodedToken.endpoint,
"cache." + decodedToken.endpoint
);
Expand Down
27 changes: 27 additions & 0 deletions src/Momento.Sdk/Internal/ExtensionMethods/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,30 @@ public static IDictionary<string, string> Clone(this IDictionary<string, string>
return new Dictionary<string, string>(dictionary);
}
}


/// <summary>
/// Defines extension methods to operate on dictionaries with
/// generic keys and values.
/// </summary>
public static class DictionaryExtensions
{
/// <summary>
/// Add all items in <paramref name="items"/> to <paramref name="dictionary"/>.
/// </summary>
/// <remark>
/// We add this since the .NET Framework does not have a constructor that takes an
/// <see cref="IEnumerable{T}"/>.
/// </remark>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="dictionary">The dictionary to add the items to.</param>
/// <param name="items">The key-value pairs to add.</param>
public static void AddRange<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IEnumerable<KeyValuePair<TKey, TValue>> items)
{
foreach (var item in items)
{
dictionary.Add(item);
}
}
}
2 changes: 1 addition & 1 deletion src/Momento.Sdk/Momento.Sdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Build Configuration -->
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<!-- Include documentation in build -->
Expand Down
17 changes: 10 additions & 7 deletions src/Momento.Sdk/Responses/CacheDictionaryFetchResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,23 @@ public Hit(_DictionaryFetchResponse response)
items = response.Found.Items;
_dictionaryByteArrayByteArray = new(() =>
{
return new Dictionary<byte[], byte[]>(
items.Select(kv => new KeyValuePair<byte[], byte[]>(kv.Field.ToByteArray(), kv.Value.ToByteArray())),
Utils.ByteArrayComparer);
var dictionary = new Dictionary<byte[], byte[]>(Utils.ByteArrayComparer);
dictionary.AddRange(items.Select(kv => new KeyValuePair<byte[], byte[]>(kv.Field.ToByteArray(), kv.Value.ToByteArray())));
return dictionary;

});

_dictionaryStringString = new(() =>
{
return new Dictionary<string, string>(
items.Select(kv => new KeyValuePair<string, string>(kv.Field.ToStringUtf8(), kv.Value.ToStringUtf8())));
var dictionary = new Dictionary<string, string>();
dictionary.AddRange(items.Select(kv => new KeyValuePair<string, string>(kv.Field.ToStringUtf8(), kv.Value.ToStringUtf8())));
return dictionary;
});
_dictionaryStringByteArray = new(() =>
{
return new Dictionary<string, byte[]>(
items.Select(kv => new KeyValuePair<string, byte[]>(kv.Field.ToStringUtf8(), kv.Value.ToByteArray())));
var dictionary = new Dictionary<string, byte[]>();
dictionary.AddRange(items.Select(kv => new KeyValuePair<string, byte[]>(kv.Field.ToStringUtf8(), kv.Value.ToByteArray())));
return dictionary;
});
}

Expand Down
15 changes: 10 additions & 5 deletions src/Momento.Sdk/Responses/CacheDictionaryGetFieldsResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,31 @@ public Hit(IEnumerable<ByteString> fields, _DictionaryGetResponse responses)

_dictionaryByteArrayByteArray = new(() =>
{
return new Dictionary<byte[], byte[]>(
var dictionary = new Dictionary<byte[], byte[]>(Utils.ByteArrayComparer);
dictionary.AddRange(
fields.Zip(responses.Found.Items, (f, r) => new ValueTuple<ByteString, _DictionaryGetResponsePart>(f, r))
.Where(pair => pair.Item2.Result == ECacheResult.Hit)
.Select(pair => new KeyValuePair<byte[], byte[]>(pair.Item1.ToByteArray(), pair.Item2.CacheBody.ToByteArray())),
Utils.ByteArrayComparer);
.Select(pair => new KeyValuePair<byte[], byte[]>(pair.Item1.ToByteArray(), pair.Item2.CacheBody.ToByteArray())));
return dictionary;
});

_dictionaryStringString = new(() =>
{
return new Dictionary<string, string>(
var dictionary = new Dictionary<string, string>();
dictionary.AddRange(
fields.Zip(responses.Found.Items, (f, r) => new ValueTuple<ByteString, _DictionaryGetResponsePart>(f, r))
.Where(pair => pair.Item2.Result == ECacheResult.Hit)
.Select(pair => new KeyValuePair<string, string>(pair.Item1.ToStringUtf8(), pair.Item2.CacheBody.ToStringUtf8())));
return dictionary;
});
_dictionaryStringByteArray = new(() =>
{
return new Dictionary<string, byte[]>(
var dictionary = new Dictionary<string, byte[]>();
dictionary.AddRange(
fields.Zip(responses.Found.Items, (f, r) => new ValueTuple<ByteString, _DictionaryGetResponsePart>(f, r))
.Where(pair => pair.Item2.Result == ECacheResult.Hit)
.Select(pair => new KeyValuePair<string, byte[]>(pair.Item1.ToStringUtf8(), pair.Item2.CacheBody.ToByteArray())));
return dictionary;
});
}

Expand Down