Przeglądaj źródła

Added partial copy support in LuObjectConverter and tests

develop
Robin Thoni 6 lat temu
rodzic
commit
e8366860ff

+ 67
- 6
Luticate2.Auth.Tests/Business/ObjectConverter/ObjectConverterTests.cs Wyświetl plik

@@ -8,6 +8,7 @@ using Luticate2.Auth.Utils.Business.Fields;
8 8
 using Luticate2.Auth.Utils.Business.ObjectConverter;
9 9
 using Luticate2.Auth.Utils.Business.ObjectConverterDescriptor;
10 10
 using Luticate2.Auth.Utils.Dbo;
11
+using Luticate2.Auth.Utils.Dbo.Fields;
11 12
 using Luticate2.Auth.Utils.Dbo.Result;
12 13
 using Luticate2.Auth.Utils.Interfaces;
13 14
 using Microsoft.Extensions.DependencyInjection;
@@ -79,12 +80,9 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
79 80
             return options;
80 81
         }
81 82
 
82
-        [Fact]
83
-        void Test1()
83
+        protected TestModel1 GetBasicModel()
84 84
         {
85
-            var serviceProvider = GetServiceProvider();
86
-            var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
87
-            var model = new TestModel1
85
+            return new TestModel1
88 86
             {
89 87
                 id = Guid.NewGuid(),
90 88
                 name = "Test.",
@@ -100,12 +98,21 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
100 98
                     }
101 99
                 }
102 100
             };
103
-            var result = converter.Convert(model, LuPartialFieldsParser.Parse("*").Data, GetConverterOptions());
101
+        }
102
+
103
+        [Fact]
104
+        public void TestBasic1()
105
+        {
106
+            var serviceProvider = GetServiceProvider();
107
+            var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
108
+            var model = GetBasicModel();
109
+            var result = converter.Convert(model, new LuFieldDbo(), LuPartialFieldsParser.Parse("*").Data, GetConverterOptions());
104 110
             Assert.Equal(LuStatus.Success.ToInt(), result.Status);
105 111
             var dbo = result.Data as TestDbo1;
106 112
             Assert.NotNull(dbo);
107 113
             Assert.Equal(model.id, dbo.Id);
108 114
             Assert.Equal(model.name, dbo.Name);
115
+            Assert.NotNull(dbo.TestDbo2s);
109 116
             Assert.Equal(model.test_model2.Count, dbo.TestDbo2s.Count);
110 117
             foreach (var e in model.test_model2.Zip(dbo.TestDbo2s, (model2, dbo2) => new {Model = model2, Dbo = dbo2}))
111 118
             {
@@ -116,5 +123,59 @@ namespace Luticate2.Auth.Tests.Business.ObjectConverter
116 123
                 Assert.Null(e.Dbo.TestDbo1);
117 124
             }
118 125
         }
126
+
127
+        [Fact]
128
+        void TestPath1()
129
+        {
130
+            var serviceProvider = GetServiceProvider();
131
+            var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
132
+            var model = GetBasicModel();
133
+            var result = converter.Convert(model, new LuFieldDbo(), LuPartialFieldsParser.Parse("Id").Data, GetConverterOptions());
134
+            Assert.Equal(LuStatus.Success.ToInt(), result.Status);
135
+            var dbo = result.Data as TestDbo1;
136
+            Assert.NotNull(dbo);
137
+            Assert.Equal(model.id, dbo.Id);
138
+            Assert.Null(dbo.Name);
139
+            Assert.Null(dbo.TestDbo2s);
140
+        }
141
+
142
+        [Fact]
143
+        void TestPath2()
144
+        {
145
+            var serviceProvider = GetServiceProvider();
146
+            var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
147
+            var model = GetBasicModel();
148
+            var result = converter.Convert(model, new LuFieldDbo(), LuPartialFieldsParser.Parse("id").Data, GetConverterOptions());
149
+            Assert.Equal(LuStatus.Success.ToInt(), result.Status);
150
+            var dbo = result.Data as TestDbo1;
151
+            Assert.NotNull(dbo);
152
+            Assert.Equal(model.id, dbo.Id);
153
+            Assert.Null(dbo.Name);
154
+            Assert.Null(dbo.TestDbo2s);
155
+        }
156
+
157
+        [Fact]
158
+        public void TestPath3()
159
+        {
160
+            var serviceProvider = GetServiceProvider();
161
+            var converter = serviceProvider.GetService<ILuObjectConverter<TestModel1, TestDbo1>>();
162
+            var model = GetBasicModel();
163
+            var result = converter.Convert(model, new LuFieldDbo(), LuPartialFieldsParser.Parse("Id, TestDbo2s(Id, Name)").Data, GetConverterOptions());
164
+            Assert.Equal(LuStatus.Success.ToInt(), result.Status);
165
+            var dbo = result.Data as TestDbo1;
166
+            Assert.NotNull(dbo);
167
+            Assert.Equal(model.id, dbo.Id);
168
+            Assert.Null(dbo.Name);
169
+            Assert.NotNull(dbo.TestDbo2s);
170
+            Assert.Equal(model.test_model2.Count, dbo.TestDbo2s.Count);
171
+            foreach (var e in model.test_model2.Zip(dbo.TestDbo2s, (model2, dbo2) => new {Model = model2, Dbo = dbo2}))
172
+            {
173
+                Assert.Equal(e.Model.id, e.Dbo.Id);
174
+                Assert.Equal(e.Model.name, e.Dbo.Name);
175
+                Assert.Null(e.Dbo.NameVirtual);
176
+                Assert.Null(e.Dbo.Parent);
177
+                Assert.Null(e.Dbo.TestDbo1);
178
+            }
179
+        }
119 180
     }
120 181
 }

+ 2
- 2
Luticate2.Auth/Utils/Business/Crud/LuEfCrudBusiness.cs Wyświetl plik

@@ -39,7 +39,7 @@ namespace Luticate2.Auth.Utils.Business.Crud
39 39
                 return LuResult<TModel>.Error(LuStatus.InternalError.ToInt(),
40 40
                     $"Could not get service: {nameof(ILuObjectConverter)}<{typeof(TDbo).Name}, {typeof(TModel).Name}>");
41 41
             }
42
-            var result = copier.Convert(dbo, partialInput, GetOptions());
42
+            var result = copier.Convert(dbo, new LuFieldDbo(), partialInput, GetOptions());
43 43
             return result.Select(o => o as TModel);
44 44
         }
45 45
 
@@ -51,7 +51,7 @@ namespace Luticate2.Auth.Utils.Business.Crud
51 51
                 return LuResult<TDbo>.Error(LuStatus.InternalError.ToInt(),
52 52
                     $"Could not get service: {nameof(ILuObjectConverter)}<{typeof(TModel).Name}, {typeof(TDbo).Name}>");
53 53
             }
54
-            var result = copier.Convert(model, partialResponse, GetOptions());
54
+            var result = copier.Convert(model, new LuFieldDbo(), partialResponse, GetOptions());
55 55
             return result.Select(o => o as TDbo);
56 56
         }
57 57
 

+ 2
- 1
Luticate2.Auth/Utils/Business/ObjectConverter/LuObjectConverterIdentity.cs Wyświetl plik

@@ -6,8 +6,9 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverter
6 6
 {
7 7
     public class LuObjectConverterIdentity : ILuObjectConverterIdentity
8 8
     {
9
-        public LuResult<object> Convert(object srcObj, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
9
+        public LuResult<object> Convert(object srcObj, LuFieldDbo path, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
10 10
         {
11
+            // TODO Handle path
11 12
             return LuResult<object>.Ok(srcObj);
12 13
         }
13 14
     }

+ 2
- 2
Luticate2.Auth/Utils/Business/ObjectConverter/LuObjectConverterLists.cs Wyświetl plik

@@ -20,7 +20,7 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverter
20 20
 
21 21
         protected abstract Type GetListType();
22 22
 
23
-        public LuResult<object> Convert(object srcObj, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
23
+        public LuResult<object> Convert(object srcObj, LuFieldDbo path, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
24 24
         {
25 25
             if (srcObj is IEnumerable enumerable)
26 26
             {
@@ -37,7 +37,7 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverter
37 37
                         var typeTo = options.TypeConverter.ConvertType(typeFrom);
38 38
                         // TODO Handle converter == null
39 39
                         var converter = LuObjectConverterUtils.GetObjectConverter(ServiceProvider, typeFrom, typeTo);
40
-                        var convertResult = converter.Convert(obj, fields, options);
40
+                        var convertResult = converter.Convert(obj, path, fields, options);
41 41
                         if (!convertResult)
42 42
                         {
43 43
                             return convertResult;

+ 8
- 3
Luticate2.Auth/Utils/Business/ObjectConverter/LuObjectConverterPoco.cs Wyświetl plik

@@ -1,4 +1,5 @@
1 1
 using System;
2
+using Luticate2.Auth.Utils.Business.Fields;
2 3
 using Luticate2.Auth.Utils.Business.Utils;
3 4
 using Luticate2.Auth.Utils.Dbo.Fields;
4 5
 using Luticate2.Auth.Utils.Dbo.Result;
@@ -16,9 +17,8 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverter
16 17
             ServiceProvider = serviceProvider;
17 18
         }
18 19
 
19
-        public LuResult<object> Convert(object srcObj, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
20
+        public LuResult<object> Convert(object srcObj, LuFieldDbo path, LuPartialFieldsDbo fields, ILuObjectConverterOptions options)
20 21
         {
21
-            // TODO Check if this is correct
22 22
             if (srcObj == null)
23 23
             {
24 24
                 return LuResult<object>.Ok(default(TTypeTo));
@@ -31,6 +31,11 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverter
31 31
             foreach (var memberInfo in typeTo.GetProperties())
32 32
             {
33 33
                 // TODO check if field is included in fields
34
+                var memberPath = LuFieldDbo.Make(path).Add(memberInfo.Name);
35
+                if (!fields.Fields.IsIncluded(memberPath))
36
+                {
37
+                    continue;
38
+                }
34 39
                 // TODO Handle srcPropExpression == null
35 40
                 var srcPropExpression = descriptor.GetMemberValueExpression(memberInfo, options.DescriptorOptions);
36 41
                 var srcPropDelegate = srcPropExpression.Compile();
@@ -40,7 +45,7 @@ namespace Luticate2.Auth.Utils.Business.ObjectConverter
40 45
 
41 46
                 // TODO Handle objectConverter == null
42 47
                 var objectConverter = LuObjectConverterUtils.GetObjectConverter(ServiceProvider, srcType, dstType);
43
-                var dstPropResult = objectConverter.Convert(srcProp, fields, options);
48
+                var dstPropResult = objectConverter.Convert(srcProp, memberPath, fields, options);
44 49
                 if (!dstPropResult)
45 50
                 {
46 51
                     return dstPropResult;

+ 2
- 1
Luticate2.Auth/Utils/Interfaces/ILuObjectConverter.cs Wyświetl plik

@@ -14,7 +14,8 @@ namespace Luticate2.Auth.Utils.Interfaces
14 14
 
15 15
     public interface ILuObjectConverter
16 16
     {
17
-        LuResult<object> Convert(object srcObj, LuPartialFieldsDbo fields, ILuObjectConverterOptions options);
17
+        // TODO add dstObject
18
+        LuResult<object> Convert(object srcObj, LuFieldDbo path, LuPartialFieldsDbo fields, ILuObjectConverterOptions options);
18 19
     }
19 20
 
20 21
     public interface ILuObjectConverter<TTypeFrom, TTypeTo> : ILuObjectConverter

Ładowanie…
Anuluj
Zapisz