Skip to content

Commit 25c8c6e

Browse files
committed
feat: enumeration support (complex type)
1 parent 16af15f commit 25c8c6e

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

src/QueryableValues.SqlServer/EntityPropertyMapping.cs

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal sealed class EntityPropertyMapping
1717
public PropertyInfo Target { get; }
1818
public Type NormalizedType { get; }
1919
public EntityPropertyTypeName TypeName { get; }
20+
public bool IsSourceEnum { get; }
2021

2122
static EntityPropertyMapping()
2223
{
@@ -38,12 +39,13 @@ static EntityPropertyMapping()
3839
};
3940
}
4041

41-
private EntityPropertyMapping(PropertyInfo source, PropertyInfo target, Type normalizedType)
42+
private EntityPropertyMapping(PropertyInfo source, PropertyInfo target, Type normalizedType, bool isSourceEnum)
4243
{
4344
Source = source;
4445
Target = target;
4546
NormalizedType = normalizedType;
4647
TypeName = GetTypeName(normalizedType);
48+
IsSourceEnum = isSourceEnum;
4749

4850
if (TypeName == EntityPropertyTypeName.Unknown)
4951
{
@@ -63,7 +65,21 @@ public static EntityPropertyTypeName GetTypeName(Type type)
6365
}
6466
}
6567

66-
public static Type GetNormalizedType(Type type) => Nullable.GetUnderlyingType(type) ?? type;
68+
public static Type GetNormalizedType(Type type, out bool isEnum)
69+
{
70+
type = Nullable.GetUnderlyingType(type) ?? type;
71+
72+
isEnum = type.IsEnum;
73+
74+
if (isEnum)
75+
{
76+
type = Enum.GetUnderlyingType(type);
77+
}
78+
79+
return type;
80+
}
81+
82+
public static Type GetNormalizedType(Type type) => GetNormalizedType(type, out _);
6783

6884
public static bool IsSimpleType(Type type)
6985
{
@@ -96,9 +112,9 @@ select g
96112

97113
foreach (var sourceProperty in sourceProperties)
98114
{
99-
var propertyType = GetNormalizedType(sourceProperty.PropertyType);
115+
var sourcePropertyNormalizedType = GetNormalizedType(sourceProperty.PropertyType, out var isSourceEnum);
100116

101-
if (targetPropertiesByType.TryGetValue(propertyType, out Queue<PropertyInfo>? targetProperties))
117+
if (targetPropertiesByType.TryGetValue(sourcePropertyNormalizedType, out Queue<PropertyInfo>? targetProperties))
102118
{
103119
if (targetProperties.Count == 0)
104120
{
@@ -108,7 +124,8 @@ select g
108124
var mapping = new EntityPropertyMapping(
109125
sourceProperty,
110126
targetProperties.Dequeue(),
111-
propertyType
127+
sourcePropertyNormalizedType,
128+
isSourceEnum
112129
);
113130

114131
mappings.Add(mapping);
@@ -128,5 +145,38 @@ public static IReadOnlyList<EntityPropertyMapping> GetMappings<T>()
128145
{
129146
return GetMappings(typeof(T));
130147
}
148+
149+
public object? GetSourceNormalizedValue(object objectInstance)
150+
{
151+
var value = Source.GetValue(objectInstance);
152+
153+
if (value is null)
154+
{
155+
return null;
156+
}
157+
158+
if (IsSourceEnum)
159+
{
160+
switch (TypeName)
161+
{
162+
case EntityPropertyTypeName.Int32:
163+
value = (int)value;
164+
break;
165+
case EntityPropertyTypeName.Byte:
166+
value = (byte)value;
167+
break;
168+
case EntityPropertyTypeName.Int16:
169+
value = (short)value;
170+
break;
171+
case EntityPropertyTypeName.Int64:
172+
value = (long)value;
173+
break;
174+
default:
175+
throw new NotSupportedException($"The underlying type of {NormalizedType.FullName} ({Enum.GetUnderlyingType(NormalizedType).FullName}) is not supported.");
176+
}
177+
}
178+
179+
return value;
180+
}
131181
}
132182
}

src/QueryableValues.SqlServer/Serializers/JsonSerializer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,13 @@ public void WriteValue(Utf8JsonWriter writer, object entity)
166166
{
167167
if (_writeValue != null)
168168
{
169-
var value = Mapping.Source.GetValue(entity);
169+
var value = Mapping.GetSourceNormalizedValue(entity);
170+
171+
if (value is null)
172+
{
173+
return;
174+
}
175+
170176
_writeValue.Invoke(writer, value);
171177
}
172178
}

src/QueryableValues.SqlServer/Serializers/XmlSerializer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,13 @@ public void WriteValue(XmlWriter writer, object entity)
242242
{
243243
if (_writeValue != null)
244244
{
245-
var value = Mapping.Source.GetValue(entity);
245+
var value = Mapping.GetSourceNormalizedValue(entity);
246+
247+
if (value is null)
248+
{
249+
return;
250+
}
251+
246252
_writeValue.Invoke(writer, value);
247253
}
248254
}

src/QueryableValues.SqlServer/SqlServer/QueryableFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ public IQueryable<TEnum> Create<TEnum>(DbContext dbContext, IEnumerable<TEnum> v
390390
where TEnum : struct, Enum
391391
{
392392
var enumType = typeof(TEnum);
393-
var normalizedType = EntityPropertyMapping.GetNormalizedType(Enum.GetUnderlyingType(enumType));
393+
var normalizedType = EntityPropertyMapping.GetNormalizedType(enumType);
394394

395395
return EntityPropertyMapping.GetTypeName(normalizedType) switch
396396
{

0 commit comments

Comments
 (0)