Browse Source

Added partial copy support in LuObjectConverter and tests

develop
Robin Thoni 6 years ago
parent
commit
e8366860ff

+ 67
- 6
Luticate2.Auth.Tests/Business/ObjectConverter/ObjectConverterTests.cs View File

8
 using Luticate2.Auth.Utils.Business.ObjectConverter;
8
 using Luticate2.Auth.Utils.Business.ObjectConverter;
9
 using Luticate2.Auth.Utils.Business.ObjectConverterDescriptor;
9
 using Luticate2.Auth.Utils.Business.ObjectConverterDescriptor;
10
 using Luticate2.Auth.Utils.Dbo;
10
 using Luticate2.Auth.Utils.Dbo;
11
+using Luticate2.Auth.Utils.Dbo.Fields;
11
 using Luticate2.Auth.Utils.Dbo.Result;
12
 using Luticate2.Auth.Utils.Dbo.Result;
12
 using Luticate2.Auth.Utils.Interfaces;
13
 using Luticate2.Auth.Utils.Interfaces;
13
 using Microsoft.Extensions.DependencyInjection;
14
 using Microsoft.Extensions.DependencyInjection;
79
             return options;
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
                 id = Guid.NewGuid(),
87
                 id = Guid.NewGuid(),
90
                 name = "Test.",
88
                 name = "Test.",
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
             Assert.Equal(LuStatus.Success.ToInt(), result.Status);
110
             Assert.Equal(LuStatus.Success.ToInt(), result.Status);
105
             var dbo = result.Data as TestDbo1;
111
             var dbo = result.Data as TestDbo1;
106
             Assert.NotNull(dbo);
112
             Assert.NotNull(dbo);
107
             Assert.Equal(model.id, dbo.Id);
113
             Assert.Equal(model.id, dbo.Id);
108
             Assert.Equal(model.name, dbo.Name);
114
             Assert.Equal(model.name, dbo.Name);
115
+            Assert.NotNull(dbo.TestDbo2s);
109
             Assert.Equal(model.test_model2.Count, dbo.TestDbo2s.Count);
116
             Assert.Equal(model.test_model2.Count, dbo.TestDbo2s.Count);
110
             foreach (var e in model.test_model2.Zip(dbo.TestDbo2s, (model2, dbo2) => new {Model = model2, Dbo = dbo2}))
117
             foreach (var e in model.test_model2.Zip(dbo.TestDbo2s, (model2, dbo2) => new {Model = model2, Dbo = dbo2}))
111
             {
118
             {
116
                 Assert.Null(e.Dbo.TestDbo1);
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 View File

39
                 return LuResult<TModel>.Error(LuStatus.InternalError.ToInt(),
39
                 return LuResult<TModel>.Error(LuStatus.InternalError.ToInt(),
40
                     $"Could not get service: {nameof(ILuObjectConverter)}<{typeof(TDbo).Name}, {typeof(TModel).Name}>");
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
             return result.Select(o => o as TModel);
43
             return result.Select(o => o as TModel);
44
         }
44
         }
45
 
45
 
51
                 return LuResult<TDbo>.Error(LuStatus.InternalError.ToInt(),
51
                 return LuResult<TDbo>.Error(LuStatus.InternalError.ToInt(),
52
                     $"Could not get service: {nameof(ILuObjectConverter)}<{typeof(TModel).Name}, {typeof(TDbo).Name}>");
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
             return result.Select(o => o as TDbo);
55
             return result.Select(o => o as TDbo);
56
         }
56
         }
57
 
57
 

+ 2
- 1
Luticate2.Auth/Utils/Business/ObjectConverter/LuObjectConverterIdentity.cs View File

6
 {
6
 {
7
     public class LuObjectConverterIdentity : ILuObjectConverterIdentity
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
             return LuResult<object>.Ok(srcObj);
12
             return LuResult<object>.Ok(srcObj);
12
         }
13
         }
13
     }
14
     }

+ 2
- 2
Luticate2.Auth/Utils/Business/ObjectConverter/LuObjectConverterLists.cs View File

20
 
20
 
21
         protected abstract Type GetListType();
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
             if (srcObj is IEnumerable enumerable)
25
             if (srcObj is IEnumerable enumerable)
26
             {
26
             {
37
                         var typeTo = options.TypeConverter.ConvertType(typeFrom);
37
                         var typeTo = options.TypeConverter.ConvertType(typeFrom);
38
                         // TODO Handle converter == null
38
                         // TODO Handle converter == null
39
                         var converter = LuObjectConverterUtils.GetObjectConverter(ServiceProvider, typeFrom, typeTo);
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
                         if (!convertResult)
41
                         if (!convertResult)
42
                         {
42
                         {
43
                             return convertResult;
43
                             return convertResult;

+ 8
- 3
Luticate2.Auth/Utils/Business/ObjectConverter/LuObjectConverterPoco.cs View File

1
 using System;
1
 using System;
2
+using Luticate2.Auth.Utils.Business.Fields;
2
 using Luticate2.Auth.Utils.Business.Utils;
3
 using Luticate2.Auth.Utils.Business.Utils;
3
 using Luticate2.Auth.Utils.Dbo.Fields;
4
 using Luticate2.Auth.Utils.Dbo.Fields;
4
 using Luticate2.Auth.Utils.Dbo.Result;
5
 using Luticate2.Auth.Utils.Dbo.Result;
16
             ServiceProvider = serviceProvider;
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
             if (srcObj == null)
22
             if (srcObj == null)
23
             {
23
             {
24
                 return LuResult<object>.Ok(default(TTypeTo));
24
                 return LuResult<object>.Ok(default(TTypeTo));
31
             foreach (var memberInfo in typeTo.GetProperties())
31
             foreach (var memberInfo in typeTo.GetProperties())
32
             {
32
             {
33
                 // TODO check if field is included in fields
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
                 // TODO Handle srcPropExpression == null
39
                 // TODO Handle srcPropExpression == null
35
                 var srcPropExpression = descriptor.GetMemberValueExpression(memberInfo, options.DescriptorOptions);
40
                 var srcPropExpression = descriptor.GetMemberValueExpression(memberInfo, options.DescriptorOptions);
36
                 var srcPropDelegate = srcPropExpression.Compile();
41
                 var srcPropDelegate = srcPropExpression.Compile();
40
 
45
 
41
                 // TODO Handle objectConverter == null
46
                 // TODO Handle objectConverter == null
42
                 var objectConverter = LuObjectConverterUtils.GetObjectConverter(ServiceProvider, srcType, dstType);
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
                 if (!dstPropResult)
49
                 if (!dstPropResult)
45
                 {
50
                 {
46
                     return dstPropResult;
51
                     return dstPropResult;

+ 2
- 1
Luticate2.Auth/Utils/Interfaces/ILuObjectConverter.cs View File

14
 
14
 
15
     public interface ILuObjectConverter
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
     public interface ILuObjectConverter<TTypeFrom, TTypeTo> : ILuObjectConverter
21
     public interface ILuObjectConverter<TTypeFrom, TTypeTo> : ILuObjectConverter

Loading…
Cancel
Save