Skip to content

Commit

Permalink
Added JsonVaultExtensionMethodAttribute (#89)
Browse files Browse the repository at this point in the history
Added support for JsonVaultExtensionMethodAttribute.
  • Loading branch information
CraigHawker committed Jan 17, 2023
1 parent f0f3937 commit ea60f34
Show file tree
Hide file tree
Showing 5 changed files with 486 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using MFiles.VAF.Configuration.JsonAdaptor;
using MFiles.VAF.Configuration.AdminConfigurations;
using MFiles.VAF.Configuration.JsonEditor;
using MFiles.VAF.Configuration.JsonAdaptor;
using MFiles.VAF.Extensions.Configuration;
using MFiles.VAF.Extensions.Configuration.Upgrading;
using MFiles.VAF.Extensions.ExtensionMethods;
Expand Down Expand Up @@ -752,5 +753,59 @@ public void StableValueOptionsEnumValuesAreStoredAsIntegers()

#endregion


[DataContract]
private class ConfigurationWithSearchConditionsJA
{
[DataMember]
public SearchConditionsJA SearchConditions { get; set; }
= new SearchConditionsJA();
}

[TestMethod]
public void LoggingUpgrade_ConfigurationWithSearchConditionsJA()
{
var vault = Mock.Of<Vault>();
var rule = new EnsureLatestSerializationSettingsUpgradeRuleProxy<ConfigurationWithSearchConditionsJA>();
rule.SetReadWriteLocation(MFNamedValueType.MFConfigurationValue, "sampleNamespace", "config");
rule.SetReadWriteLocationValue(vault, @"{
""SearchConditions"": [
{
""conditionType"": ""equal"",
""expression"": {
""type"": ""propertyValue"",
""propertyDef"": ""{82490C2F-8FB2-423B-85B5-F4ADB214C0FD}"",
""indirectionLevels"": []
},
""typedValue"": {
""dataType"": ""lookup"",
""value"": {
""isNull"": true
}
}
}
]
}");

Assert.IsTrue(rule.Execute(vault));
Assert.That.AreEqualJson(@"{
""SearchConditions"": [
{
""conditionType"": ""equal"",
""expression"": {
""type"": ""propertyValue"",
""propertyDef"": ""{82490C2F-8FB2-423B-85B5-F4ADB214C0FD}""
},
""typedValue"": {
""dataType"": ""lookup"",
""value"": {
""isNull"": true
}
}
}
]
}", rule.GetReadWriteLocationValue(vault));

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;

namespace MFiles.VAF.Extensions
{
/// <summary>
/// Defines that the method is a vault extension method that sends/receives data in JSON format.
/// The method should declare one (single vault) or two (vault plus format of the data in the VEM input) parameters.
/// The method can optionally have a return type.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class JsonVaultExtensionMethodAttribute
: TypedVaultExtensionMethodAttribute
{
public JsonVaultExtensionMethodAttribute(string vaultExtensionMethodName)
: base(vaultExtensionMethodName)
{
}

/// <summary>
/// Converts <paramref name="e"/> to a <see cref="JObject"/>
/// that can be included in the failure details.
/// </summary>
/// <param name="e">The exception</param>
/// <returns>null if <paramref name="e"/> is null, the <see cref="JObject"/> if not.</returns>
protected virtual JObject GetExceptionJObject(Exception e)
{
// Sanity.
if (e == null)
return null;
var exceptionJObject = new JObject()
{
{"type", e.GetType().ToString()},
{"message", e.Message},
{"stackTrace", e.StackTrace}
};

// Anything inside it?
if (e.InnerException != null)
exceptionJObject.Add("innerException", this.GetExceptionJObject(e.InnerException));
return exceptionJObject;
}

/// <inheritdoc />
public override string GetFailedOutput(Exception e)
{
// Create the basic data.
var jObject = new JObject
{
new JProperty("successful", false)
};

// Include the exception details if we are told to.
if (this.IncludeExceptionDetailsInResponse)
{
var exceptionJObject = this.GetExceptionJObject(e);
if(null != exceptionJObject)
jObject.Add(new JProperty("exception", exceptionJObject));
}

// Return the data to the caller.
return jObject.ToString();
}


/// <inheritdoc />
public override string GetSuccessfulOutput()
=> @"{ ""successful"": true }";

/// <inheritdoc />
protected override object Deserialize(string input, Type t)
=> Newtonsoft.Json.JsonConvert.DeserializeObject(input, t);

/// <inheritdoc />
protected override string Serialize(object input)
=> Newtonsoft.Json.JsonConvert.SerializeObject(input);
}
}

0 comments on commit ea60f34

Please sign in to comment.