Skip to content

Commit

Permalink
StoreToDisk :IsDefaultValue emits wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
apobekiaris committed Nov 1, 2022
1 parent cf86e4c commit 03d6737
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ public static IObservable<TSource> WhenDefault<TSource>(this IObservable<TSource

public static IObservable<TSource> WhenDefault<TSource,TValue>(this IObservable<TSource> source,Func<TSource, TValue> valueSelector)
=>source.Where(source1 => valueSelector(source1).IsDefaultValue());

public static IObservable<TSource> WhenDefault<TSource>(this IObservable<TSource> source,Func<TSource, object> valueSelector,Func<TSource,Type> valueType)
=>source.Where(source1 => valueSelector(source1).IsDefaultValue());
}
}
13 changes: 13 additions & 0 deletions src/Extensions/Xpand.Extensions/LinqExtensions/Slice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace Xpand.Extensions.LinqExtensions {
public static partial class LinqExtensions {
public static T[] Slice<T>(this T[] source, int index, int length) {
var slice = new T[length];
Array.Copy(source, index, slice, 0, length);
return slice;
}
public static T[] Slice<T>(this T[] source, int length)
=> source.Slice(0, length);
}
}
2 changes: 1 addition & 1 deletion src/Extensions/Xpand.Extensions/LinqExtensions/Split.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Xpand.Extensions.LinqExtensions{
public static partial class LinqExtensions{
public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> list, int parts){
var i = 0;
return list.GroupBy(item => i++ % parts).Select(part => part.AsEnumerable());
return list.GroupBy(_ => i++ % parts).Select(part => part.AsEnumerable());
}
}
}
15 changes: 10 additions & 5 deletions src/Extensions/Xpand.Extensions/ObjectExtensions/IsDefaultValue.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using Xpand.Extensions.TypeExtensions;
using System;
using Xpand.Extensions.TypeExtensions;

namespace Xpand.Extensions.ObjectExtensions{
public static partial class ObjectExtensions{
public static bool IsDefaultValue<TSource>(this TSource source){
var def = default(TSource);
return def != null ? def.Equals(source) : source == null;
}
public static bool IsDefaultValue<TSource>(this TSource source) {
var def = default(TSource);
return def != null ? def.Equals(source) : typeof(TSource) == typeof(object)
? source == null || source.Equals(source.GetType().DefaultValue()) : source == null;
}

public static bool IsDefaultValue(this object source)
=> source == null || source.Equals(source.GetType().DefaultValue());

public static bool IsDefaultValue(this object source,Type objectType)
=> objectType.DefaultValue()==source;
}
}
16 changes: 13 additions & 3 deletions src/Modules/StoreToDisk/StoreToDiskService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ private static IObservable<Unit> DailyBackup(this XafApplication application)
.SelectMany(o1 => jArray.Where(jToken => keyMember.Match(jToken, o1)).Take(1).ToNowObservable()
.SelectMany(jToken => memberInfos.ToNowObservable()
.WhenDefault(info => info.GetValue(o1))
.Do(info => info.SetValue(o1, ((string)jToken[info.Name]).Change(info.MemberType)))
.Do(info => {
var value = ((JValue)jToken[info.Name])?.Value;
var change = !info.MemberTypeInfo.IsPersistent ? value.Change(info.MemberType) :
value == null ? null : space.GetObjectByKey(info.MemberType, value);
info.SetValue(o1, change);
})
.Select(_ => o1)))
)
.BufferUntilCompleted()
Expand Down Expand Up @@ -106,8 +111,13 @@ private static object[] ObjectsToStore(this (IMemberInfo keyMember, IMemberInfo[
private static IObservable<JObject[]> StoreToDisk(this object[] objects ,JArray jArray,IMemberInfo keyMember, IMemberInfo[] memberInfos, string filePath, StoreToDiskAttribute attribute)
=> objects.ToNowObservable().SelectMany(instance => {
var jtoken = jArray.GetToken(keyMember, memberInfos, instance);
return memberInfos.ToNowObservable().Do(memberInfo =>
jtoken[memberInfo.Name] = memberInfo.GetValue(instance).ToJToken())
return memberInfos.ToNowObservable().Do(memberInfo => {
var value = memberInfo.GetValue(instance);
if (memberInfo.MemberTypeInfo.IsDomainComponent) {
value = memberInfo.MemberTypeInfo.KeyMember.GetValue(value);
}
jtoken[memberInfo.Name] = value.ToJToken();
})
.ConcatIgnoredValue(jtoken);
}).BufferUntilCompleted(true)
.Do(jObjects => {
Expand Down
9 changes: 9 additions & 0 deletions src/Tests/Extensions/ProcessTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Shouldly;
using Xpand.Extensions.ObjectExtensions;
using Xpand.Extensions.Reactive.Transform.System.Diagnostics;
using Xpand.TestsLib.Common.Attributes;

namespace Xpand.Extensions.Tests{
public class ObjectExtensionsTests {
[TestCase(0,true)]
public void DefaultValue(object value,bool isDefault) {
value.IsDefaultValue().ShouldBe(isDefault);
}

}
public class ProcessTests{
private static string CreateScriptFile(){
var path = $"{Path.GetTempPath()}\\Output.ps1";
Expand Down
9 changes: 8 additions & 1 deletion src/Tests/StoreToDisk/BOModel/STD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Xpand.XAF.Persistent.BaseImpl;

namespace Xpand.XAF.Modules.StoreToDisk.Tests.BOModel {
[StoreToDisk(nameof(Name),DataProtectionScope.LocalMachine,nameof(Secret))]
[StoreToDisk(nameof(Name),DataProtectionScope.LocalMachine,nameof(Secret),nameof(Number))]
public class STD:CustomBaseObject {
public STD(Session session) : base(session) { }

Expand All @@ -14,6 +14,13 @@ public string Secret {
set => SetPropertyValue(nameof(Secret), ref _secret, value);
}

int _number;

public int Number {
get => _number;
set => SetPropertyValue(nameof(Number), ref _number, value);
}

string _name;

public string Name {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private async Task WhenObjectSpaceCommitsNewObjects(string name,IObjectSpace obj

std = testObserver.AwaitDone(Timeout).Items.Single();
std.Secret.ShouldBe($"{name}secret");

std.Number.ShouldBe(2);
await Application.UseObjectSpace(space => {
std = space.GetObject(std);
space.Delete(std);
Expand All @@ -57,7 +57,7 @@ private void CreateStorage(string name, bool protect=false) {
if (Directory.Exists(folder)) {
Directory.Delete(folder, true);
Directory.CreateDirectory(folder);
var data = new[] { new { Secret = $"{name}secret", Name = name } }.Serialize();
var data = new[] { new { Secret = $"{name}secret", Name = name,Number=2 } }.Serialize();
var bytes = data.Bytes();
if (protect) {
bytes = data.Protect();
Expand Down
2 changes: 1 addition & 1 deletion tools/Xpand.VersionConverter/Xpand.VersionConverter.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Xpand.VersionConverter</id>
<version>4.221.10.1</version>
<version>4.221.10.2</version>
<title>This package modifies the DevExpress references in all Xpand.XAF.* assemblies to match the target project DevExpress version</title>
<authors>eXpandFramework</authors>
<owners>eXpandFramework</owners>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>Xpand.XAF.ModelEditor</id>
<version>4.221.10.1</version>
<version>4.221.10.2</version>
<title>DevExpress eXpandFramework Standalone Model Editor VS/Rider/Explorer integration.</title>
<authors>eXpandFramework</authors>
<owners>eXpandFramework</owners>
Expand Down

0 comments on commit 03d6737

Please sign in to comment.