mirror of https://github.com/abpframework/abp.git
7 changed files with 117 additions and 11 deletions
@ -0,0 +1,56 @@ |
|||||
|
using System.Linq; |
||||
|
using System.Text.Json.Nodes; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.SimpleStateChecking; |
||||
|
|
||||
|
namespace Volo.Abp.Features; |
||||
|
|
||||
|
public class FeaturesSimpleStateCheckerSerializerContributor : |
||||
|
ISimpleStateCheckerSerializerContributor, |
||||
|
ISingletonDependency |
||||
|
{ |
||||
|
public const string CheckerShortName = "F"; |
||||
|
|
||||
|
public string SerializeToJson<TState>(ISimpleStateChecker<TState> checker) |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
if (checker is not RequireFeaturesSimpleStateChecker<TState> featuresSimpleStateChecker) |
||||
|
{ |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
var jsonObject = new JsonObject { |
||||
|
["T"] = CheckerShortName, |
||||
|
["A"] = featuresSimpleStateChecker.RequiresAll |
||||
|
}; |
||||
|
|
||||
|
var nameArray = new JsonArray(); |
||||
|
foreach (var featureName in featuresSimpleStateChecker.FeatureNames) |
||||
|
{ |
||||
|
nameArray.Add(featureName); |
||||
|
} |
||||
|
|
||||
|
jsonObject["N"] = nameArray; |
||||
|
return jsonObject.ToJsonString(); |
||||
|
} |
||||
|
|
||||
|
public ISimpleStateChecker<TState> Deserialize<TState>(JsonObject jsonObject) |
||||
|
where TState : IHasSimpleStateCheckers<TState> |
||||
|
{ |
||||
|
if (jsonObject["T"]?.ToString() != CheckerShortName) |
||||
|
{ |
||||
|
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 RequireFeaturesSimpleStateChecker<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.Features; |
||||
|
|
||||
|
public class FeaturesSimpleStateCheckerSerializerContributor_Tests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_Serialize_RequireGlobalFeaturesSimpleStateChecker() |
||||
|
{ |
||||
|
var serializer = new FeaturesSimpleStateCheckerSerializerContributor(); |
||||
|
|
||||
|
var result = serializer.SerializeToJson( |
||||
|
new RequireFeaturesSimpleStateChecker<MyState>( |
||||
|
"FeatureA", |
||||
|
"FeatureB" |
||||
|
) |
||||
|
); |
||||
|
|
||||
|
result.ShouldBe("{\"T\":\"F\",\"A\":true,\"N\":[\"FeatureA\",\"FeatureB\"]}"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Deserialize_RequireGlobalFeaturesSimpleStateChecker() |
||||
|
{ |
||||
|
var serializer = new FeaturesSimpleStateCheckerSerializerContributor(); |
||||
|
|
||||
|
var jsonObject = (JsonObject)JsonNode.Parse("{\"T\":\"F\",\"A\":true,\"N\":[\"FeatureA\",\"FeatureB\"]}"); |
||||
|
var checker = serializer.Deserialize<MyState>(jsonObject); |
||||
|
|
||||
|
checker.ShouldBeOfType<RequireFeaturesSimpleStateChecker<MyState>>(); |
||||
|
var globalFeaturesSimpleStateChecker = checker as RequireFeaturesSimpleStateChecker<MyState>; |
||||
|
globalFeaturesSimpleStateChecker.ShouldNotBeNull(); |
||||
|
globalFeaturesSimpleStateChecker.RequiresAll.ShouldBeTrue(); |
||||
|
globalFeaturesSimpleStateChecker.FeatureNames[0].ShouldBe("FeatureA"); |
||||
|
globalFeaturesSimpleStateChecker.FeatureNames[1].ShouldBe("FeatureB"); |
||||
|
} |
||||
|
|
||||
|
private class MyState : IHasSimpleStateCheckers<MyState> |
||||
|
{ |
||||
|
public List<ISimpleStateChecker<MyState>> StateCheckers { get; } = new(); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue