mirror of https://github.com/abpframework/abp.git
12 changed files with 251 additions and 23 deletions
@ -0,0 +1,10 @@ |
|||||
|
namespace Volo.Abp.SimpleStateChecking; |
||||
|
|
||||
|
public interface ISimpleStateCheckerSerializer |
||||
|
{ |
||||
|
public string Serialize<TState>(ISimpleStateChecker<TState> checker) |
||||
|
where TState : IHasSimpleStateCheckers<TState>; |
||||
|
|
||||
|
public ISimpleStateChecker<TState> Deserialize<TState>(string value) |
||||
|
where TState : IHasSimpleStateCheckers<TState>; |
||||
|
} |
||||
@ -0,0 +1,15 @@ |
|||||
|
using System.Text.Json.Nodes; |
||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking; |
||||
|
|
||||
|
public interface ISimpleStateCheckerSerializerContributor |
||||
|
{ |
||||
|
[CanBeNull] |
||||
|
public string SerializeToJson<TState>(ISimpleStateChecker<TState> checker) |
||||
|
where TState : IHasSimpleStateCheckers<TState>; |
||||
|
|
||||
|
[CanBeNull] |
||||
|
public ISimpleStateChecker<TState> Deserialize<TState>(JsonObject jsonObject) |
||||
|
where TState : IHasSimpleStateCheckers<TState>; |
||||
|
} |
||||
@ -0,0 +1,56 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Text.Json.Nodes; |
||||
|
using JetBrains.Annotations; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking; |
||||
|
|
||||
|
public class SimpleStateCheckerSerializer : |
||||
|
ISimpleStateCheckerSerializer, |
||||
|
ITransientDependency |
||||
|
{ |
||||
|
private readonly IEnumerable<ISimpleStateCheckerSerializerContributor> _contributors; |
||||
|
|
||||
|
public SimpleStateCheckerSerializer(IEnumerable<ISimpleStateCheckerSerializerContributor> contributors) |
||||
|
{ |
||||
|
_contributors = contributors; |
||||
|
} |
||||
|
|
||||
|
[CanBeNull] |
||||
|
public string Serialize<TState>(ISimpleStateChecker<TState> checker) |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
foreach (var contributor in _contributors) |
||||
|
{ |
||||
|
var result = contributor.SerializeToJson(checker); |
||||
|
if (result != null) |
||||
|
{ |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
[CanBeNull] |
||||
|
public ISimpleStateChecker<TState> Deserialize<TState>(string value) |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
var jsonObject = JsonNode.Parse(value) as JsonObject; |
||||
|
if (jsonObject == null) |
||||
|
{ |
||||
|
throw new AbpException("The value is not a JSON object: " + value); |
||||
|
} |
||||
|
|
||||
|
foreach (var contributor in _contributors) |
||||
|
{ |
||||
|
var result = contributor.Deserialize<TState>(jsonObject); |
||||
|
if (result != null) |
||||
|
{ |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,38 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace Volo.Abp.SimpleStateChecking; |
||||
|
|
||||
|
public static class SimpleStateCheckerSerializerExtensions |
||||
|
{ |
||||
|
public static string Serialize<TState>( |
||||
|
this ISimpleStateCheckerSerializer serializer, |
||||
|
IList<ISimpleStateChecker<TState>> stateCheckers) |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
switch (stateCheckers.Count) |
||||
|
{ |
||||
|
case 0: |
||||
|
return null; |
||||
|
case 1: |
||||
|
return $"[{serializer.Serialize(stateCheckers.Single())}]"; |
||||
|
default: |
||||
|
var stringBuilder = new StringBuilder("["); |
||||
|
|
||||
|
for (var i = 0; i < stateCheckers.Count; i++) |
||||
|
{ |
||||
|
if (i > 0) |
||||
|
{ |
||||
|
stringBuilder.Append(","); |
||||
|
} |
||||
|
|
||||
|
stringBuilder.Append(serializer.Serialize(stateCheckers[i])); |
||||
|
} |
||||
|
|
||||
|
stringBuilder.Append("]"); |
||||
|
|
||||
|
return stringBuilder.ToString(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,54 @@ |
|||||
|
using System.Linq; |
||||
|
using System.Text.Json.Nodes; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.SimpleStateChecking; |
||||
|
|
||||
|
namespace Volo.Abp.GlobalFeatures; |
||||
|
|
||||
|
public class GlobalFeaturesSimpleStateCheckerSerializerContributor : |
||||
|
ISimpleStateCheckerSerializerContributor, |
||||
|
ITransientDependency |
||||
|
{ |
||||
|
public string SerializeToJson<TState>(ISimpleStateChecker<TState> checker) |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
if (checker is not RequireGlobalFeaturesSimpleStateChecker<TState> globalFeaturesSimpleStateChecker) |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
var jsonObject = new JsonObject { |
||||
|
["T"] = "GF", |
||||
|
["A"] = globalFeaturesSimpleStateChecker.RequiresAll |
||||
|
}; |
||||
|
|
||||
|
var nameArray = new JsonArray(); |
||||
|
foreach (var globalFeatureName in globalFeaturesSimpleStateChecker.GlobalFeatureNames) |
||||
|
{ |
||||
|
nameArray.Add(globalFeatureName); |
||||
|
} |
||||
|
|
||||
|
jsonObject["N"] = nameArray; |
||||
|
return jsonObject.ToJsonString(); |
||||
|
} |
||||
|
|
||||
|
public ISimpleStateChecker<TState> Deserialize<TState>(JsonObject jsonObject) |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
if (jsonObject["T"]?.ToString() != "GF") |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
var nameArray = jsonObject["N"] as JsonArray; |
||||
|
if (nameArray == null) |
||||
|
{ |
||||
|
throw new AbpException("'N' is not an array in the serialized state checker! JsonObject: " + jsonObject.ToJsonString()); |
||||
|
} |
||||
|
|
||||
|
return new RequireGlobalFeaturesSimpleStateChecker<TState>( |
||||
|
(bool?)jsonObject["A"] ?? false, |
||||
|
nameArray.Select(x => x.ToString()).ToArray() |
||||
|
); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,46 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Text.Json.Nodes; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.SimpleStateChecking; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.GlobalFeatures; |
||||
|
|
||||
|
public class GlobalFeaturesSimpleStateCheckerSerializerContributor_Tests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_Serialize_RequireGlobalFeaturesSimpleStateChecker() |
||||
|
{ |
||||
|
var serializer = new GlobalFeaturesSimpleStateCheckerSerializerContributor(); |
||||
|
|
||||
|
var result = serializer.SerializeToJson( |
||||
|
new RequireGlobalFeaturesSimpleStateChecker<MyState>( |
||||
|
"FeatureA", |
||||
|
"FeatureB" |
||||
|
) |
||||
|
); |
||||
|
|
||||
|
result.ShouldBe("{\"T\":\"GF\",\"A\":true,\"N\":[\"FeatureA\",\"FeatureB\"]}"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Deserialize_RequireGlobalFeaturesSimpleStateChecker() |
||||
|
{ |
||||
|
var serializer = new GlobalFeaturesSimpleStateCheckerSerializerContributor(); |
||||
|
|
||||
|
var jsonObject = (JsonObject)JsonNode.Parse("{\"T\":\"GF\",\"A\":true,\"N\":[\"FeatureA\",\"FeatureB\"]}"); |
||||
|
var checker = serializer.Deserialize<MyState>(jsonObject); |
||||
|
|
||||
|
checker.ShouldBeOfType<RequireGlobalFeaturesSimpleStateChecker<MyState>>(); |
||||
|
var globalFeaturesSimpleStateChecker = checker as RequireGlobalFeaturesSimpleStateChecker<MyState>; |
||||
|
globalFeaturesSimpleStateChecker.ShouldNotBeNull(); |
||||
|
globalFeaturesSimpleStateChecker.RequiresAll.ShouldBeTrue(); |
||||
|
globalFeaturesSimpleStateChecker.GlobalFeatureNames[0].ShouldBe("FeatureA"); |
||||
|
globalFeaturesSimpleStateChecker.GlobalFeatureNames[1].ShouldBe("FeatureB"); |
||||
|
} |
||||
|
|
||||
|
private class MyState : IHasSimpleStateCheckers<MyState> |
||||
|
{ |
||||
|
public List<ISimpleStateChecker<MyState>> StateCheckers { get; } = new(); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue