-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
497 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<Company>Gigya Inc.</Company> | ||
<Copyright>© 2018 Gigya Inc.</Copyright> | ||
<Copyright>© 2020 Gigya Inc.</Copyright> | ||
<Product>Microdot Framework</Product> | ||
<Description>Microdot Framework</Description> | ||
<AssemblyVersion></AssemblyVersion> | ||
<Version>3.0.8</Version> | ||
<Version>3.1.0</Version> | ||
<RunSettingsFilePath>$(SolutionDir)\test.runsettings</RunSettingsFilePath> | ||
</PropertyGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using Gigya.Common.Contracts.Exceptions; | ||
using Gigya.Microdot.SharedLogic.Utils; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Gigya.Microdot.SharedLogic.Events | ||
{ | ||
public struct ContextTag | ||
{ | ||
public object Value; | ||
public bool IsEncrypted; | ||
} | ||
|
||
public class ContextTags | ||
{ | ||
internal Dictionary<string, ContextTag> Tags; | ||
public ContextTags() { Tags = new Dictionary<string, ContextTag>(); } | ||
public ContextTags(Dictionary<string, ContextTag> tags) { Tags = tags; } | ||
|
||
public IEnumerable<KeyValuePair<string, object>> GetUnencryptedTags() => Tags.GetUnencryptedTags(); | ||
|
||
public IEnumerable<KeyValuePair<string, object>> GetEncryptedTags() => Tags.GetEncryptedTags(); | ||
|
||
public bool TryGet(string key, out object value) | ||
{ | ||
if (Tags.TryGetValue(key, out var item)) | ||
{ | ||
value = item.Value; | ||
return true; | ||
} | ||
else | ||
{ | ||
value = null; | ||
return false; | ||
} | ||
} | ||
|
||
public bool TryGet<T>(string key, out T value) | ||
{ | ||
if (Tags.TryGetValue(key, out var item)) | ||
{ | ||
try | ||
{ | ||
value = (T)Convert.ChangeType(item.Value, typeof(T)); | ||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new ProgrammaticException("Cannot obtain tag with desired type", unencrypted: new Tags() { | ||
["error"] = ex.Message, | ||
["actualType"] = item.Value?.GetType().ToString(), | ||
["desiredType"] = typeof(T).Name, | ||
["tagName"] = key, | ||
}, | ||
encrypted: new Tags() { ["tagValue"] = item.Value?.ToString() }); | ||
} | ||
} | ||
else | ||
{ | ||
value = default(T); | ||
return false; | ||
} | ||
} | ||
|
||
public IDisposable SetEncryptedTag<T>(string key, T value) | ||
{ | ||
return Tag(key, value, IsEncrypted: true); | ||
} | ||
|
||
public IDisposable SetUnencryptedTag<T>(string key, T value) | ||
{ | ||
return Tag(key, value, IsEncrypted: false); | ||
} | ||
|
||
IDisposable Tag<T>(string key, T value, bool IsEncrypted) | ||
{ | ||
if (!typeof(T).IsValueType && !(value is string)) | ||
throw new ProgrammaticException("Tags can only be pritmive values or strings"); | ||
|
||
if (Tags.TryGetValue(key, out var currentTag)) | ||
{ | ||
Tags[key] = new ContextTag { Value = value, IsEncrypted = IsEncrypted }; | ||
|
||
// On dispose, we restore the tag to its previous value | ||
return new DisposableAction<bool>( | ||
state: true, | ||
dispose: s => Tags[key] = new ContextTag { Value = currentTag.Value, IsEncrypted = currentTag.IsEncrypted }); | ||
} | ||
else | ||
{ | ||
Tags[key] = new ContextTag { Value = value, IsEncrypted = IsEncrypted }; | ||
|
||
// On dispose, we remove the value | ||
return new DisposableAction<bool>( | ||
state: true, | ||
dispose: s => Tags.Remove(key)); | ||
} | ||
|
||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
|
||
namespace Gigya.Microdot.SharedLogic.Events | ||
{ | ||
/// <summary> | ||
/// A disposable action allows the developer to use it as part of a using statement to call a function on creation and another on disposal. | ||
/// </summary> | ||
class DisposableAction<T> : IDisposable | ||
{ | ||
private readonly T _state; | ||
private Action<T> _dispose; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DisposableAction"/> class. | ||
/// </summary> | ||
/// <param name="dispose"> | ||
/// The dispose. | ||
/// </param> | ||
public DisposableAction(T state, Action<T> dispose) | ||
{ | ||
if (dispose == null) throw new ArgumentNullException("dispose"); | ||
_state = state; | ||
_dispose = dispose; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. | ||
/// </summary> | ||
/// <filterpriority>2</filterpriority> | ||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
/// <summary> | ||
/// The dispose. | ||
/// </summary> | ||
/// <param name="disposing"> | ||
/// The disposing. | ||
/// </param> | ||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (disposing) | ||
{ | ||
_dispose?.Invoke(_state); | ||
_dispose = null; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.