Skip to content

Commit 0d8bd6d

Browse files
author
patryk.bujalla
committed
extend enum and enum? maping methods, use linq in ConvertToDotNetEnum method
1 parent b7fd68a commit 0d8bd6d

1 file changed

Lines changed: 35 additions & 46 deletions

File tree

PortaCapena.OdooJsonRpcClient/Converters/OdooModelMapper.cs

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -47,51 +47,39 @@ public static bool ConverOdooPropertyToDotNet(Type dotnetType, JToken value, out
4747
return true;
4848

4949
case JTokenType.String when dotnetType == typeof(DateTime) || dotnetType == typeof(DateTime?):
50-
{
51-
var stringTime = value.ToObject(typeof(string)) as string;
52-
result = DateTime.Parse(stringTime);
53-
return true;
54-
}
50+
var stringTime = value.ToObject(typeof(string)) as string;
51+
result = DateTime.Parse(stringTime);
52+
return true;
5553

56-
case JTokenType.String when dotnetType.IsEnum ||
57-
(dotnetType.IsGenericType &&
58-
dotnetType.GetGenericTypeDefinition() == typeof(Nullable<>) &&
59-
dotnetType.GenericTypeArguments.Length == 1 &&
60-
dotnetType.GenericTypeArguments[0].IsEnum):
61-
{
62-
if (dotnetType.IsEnum)
63-
{
64-
result = ConvertToDotNetEnum(dotnetType, value.ToString());
65-
return true;
66-
}
67-
var nullableType = Nullable.GetUnderlyingType(dotnetType);
68-
result = ConvertToDotNetEnum(nullableType, value);
69-
return true;
70-
}
54+
case JTokenType.String when dotnetType.IsEnum:
55+
result = ConvertToDotNetEnum(dotnetType, value.ToString());
56+
return true;
57+
58+
case JTokenType.String when dotnetType.IsGenericType && dotnetType.GetGenericTypeDefinition() == typeof(Nullable<>) &&
59+
dotnetType.GenericTypeArguments.Length == 1 && dotnetType.GenericTypeArguments[0].IsEnum:
60+
var nullableType = Nullable.GetUnderlyingType(dotnetType);
61+
result = ConvertToDotNetEnum(nullableType, value);
62+
return true;
7163

7264
case JTokenType.Array when dotnetType.IsArray:
65+
if (!value.HasValues)
7366
{
74-
if (!value.HasValues)
75-
{
76-
result = Activator.CreateInstance(dotnetType, 0);
77-
return true;
78-
}
79-
80-
result = value.ToObject(dotnetType);
67+
result = Activator.CreateInstance(dotnetType, 0);
8168
return true;
8269
}
8370

71+
result = value.ToObject(dotnetType);
72+
return true;
73+
8474
case JTokenType.Array when !dotnetType.IsArray:
85-
{
86-
if (!value.HasValues)
87-
return false;
75+
if (!value.HasValues)
76+
return false;
8877

89-
if (value.Count() > 2 || dotnetType != typeof(long) && dotnetType != typeof(long?) && dotnetType != typeof(int) && dotnetType != typeof(int?) || value.First.Type != JTokenType.Integer)
90-
throw new Exception($"Not implemented json mapping '${value.Parent}'");
78+
if (value.Count() > 2 || dotnetType != typeof(long) && dotnetType != typeof(long?) && dotnetType != typeof(int) && dotnetType != typeof(int?) || value.First.Type != JTokenType.Integer)
79+
throw new Exception($"Not implemented json mapping '${value.Parent}'");
9180

92-
result = value.First.ToObject(dotnetType);
93-
return true;
94-
}
81+
result = value.First.ToObject(dotnetType);
82+
return true;
9583

9684
default:
9785
throw new Exception($"Not implemented json mapping value: '${value.Parent}' to {dotnetType.Name}");
@@ -206,23 +194,24 @@ public static string ConvertOdooNameToDotNet(string odooName)
206194

207195
public static object ConvertToDotNetEnum(Type type, JToken value)
208196
{
209-
foreach (var name in Enum.GetNames(type))
210-
{
211-
var odooValue = GetOdooEnumName(type.GetField(name));
212-
if (value.ToString() == odooValue)
213-
return Enum.Parse(type, name);
214-
}
215-
throw new ArgumentException();
197+
var field = type.GetFields()
198+
.Where(x => x.IsLiteral && GetOdooEnumName(x) == value.ToString())
199+
.FirstOrDefault();
200+
201+
if (field != null)
202+
return Enum.Parse(type, field.Name);
203+
204+
throw new ArgumentException($"Value: '{value}' not found in enum : '{type.FullName}'");
216205
}
217206

218207
public static string GetOdooEnumName(FieldInfo fieldInfo)
219208
{
220-
var jsonEnumAttribute = Attribute.GetCustomAttributes(fieldInfo)
209+
var enumAttribute = Attribute.GetCustomAttributes(fieldInfo)
221210
.FirstOrDefault(x => x is EnumMemberAttribute) as EnumMemberAttribute;
222-
if (jsonEnumAttribute != null)
223-
return jsonEnumAttribute.Value;
211+
if (enumAttribute != null)
212+
return enumAttribute.Value;
224213

225-
throw new ArgumentException($"Missing atrribute: '{nameof(EnumMemberAttribute)}' in enum");
214+
throw new ArgumentException($"Missing atrribute: '{nameof(EnumMemberAttribute)}' for enum '{fieldInfo.FieldType.Name}' - '{fieldInfo.Name}'");
226215
}
227216
}
228217
}

0 commit comments

Comments
 (0)