Skip to content

Commit af07f59

Browse files
committed
Less restrictive contract.Read types, support for properties + fields
1 parent 30661bb commit af07f59

File tree

1 file changed

+55
-10
lines changed

1 file changed

+55
-10
lines changed

Assets/Thirdweb/Core/Scripts/Contract.cs

+55-10
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,18 @@ public async Task<T> Read<T>(string functionName, params object[] args)
246246
// Single
247247
if (rawResults.Count == 1)
248248
{
249-
return ConvertValue<T>(rawResults[0]);
249+
try
250+
{
251+
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(rawResults[0]));
252+
}
253+
catch
254+
{
255+
return ConvertValue<T>(rawResults[0]);
256+
}
250257
}
251258

252259
// List or array
253-
if ((typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(List<>)) || (typeof(T).IsArray))
260+
if ((typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(List<>)) || typeof(T).IsArray)
254261
{
255262
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(rawResults));
256263
}
@@ -261,11 +268,13 @@ public async Task<T> Read<T>(string functionName, params object[] args)
261268
var targetType = typeof(T);
262269
var properties = targetType.GetProperties();
263270
var fields = targetType.GetFields();
271+
var combinedCount = properties.Length + fields.Length;
264272

265-
if (rawResults.Count == properties.Length)
266-
{
267-
var instance = Activator.CreateInstance<T>();
273+
var instance = Activator.CreateInstance<T>();
268274

275+
if (rawResults.Count == combinedCount)
276+
{
277+
// Assign values to properties
269278
for (int i = 0; i < properties.Length; i++)
270279
{
271280
try
@@ -275,18 +284,51 @@ public async Task<T> Read<T>(string functionName, params object[] args)
275284
catch (ArgumentException ex)
276285
{
277286
throw new UnityException(
278-
$"Type mismatch assigning value to property {properties[i].Name}: expected {rawResults[i].GetType().Name}, got {properties[i].PropertyType.Name}",
287+
$"Type mismatch assigning value to property {properties[i].Name} of type {typeof(T).Name}: expected {rawResults[i].GetType().Name}, got {properties[i].PropertyType.Name}",
288+
ex
289+
);
290+
}
291+
}
292+
293+
// Assign values to fields
294+
for (int i = 0; i < fields.Length; i++)
295+
{
296+
try
297+
{
298+
fields[i].SetValue(instance, rawResults[properties.Length + i]);
299+
}
300+
catch (ArgumentException ex)
301+
{
302+
throw new UnityException(
303+
$"Type mismatch assigning value to field {fields[i].Name} of type {typeof(T).Name}: expected {rawResults[properties.Length + i].GetType().Name}, got {fields[i].FieldType.Name}",
279304
ex
280305
);
281306
}
282307
}
283308

284309
return instance;
285310
}
286-
else if (rawResults.Count == fields.Length)
311+
else if (rawResults.Count == properties.Length) // Just Properties
287312
{
288-
var instance = Activator.CreateInstance<T>();
313+
for (int i = 0; i < properties.Length; i++)
314+
{
315+
try
316+
{
317+
properties[i].SetValue(instance, rawResults[i]);
318+
}
319+
catch (ArgumentException ex)
320+
{
321+
throw new UnityException(
322+
$"Type mismatch assigning value to property {properties[i].Name} of type {typeof(T).Name}: expected {rawResults[i].GetType().Name}, got {properties[i].PropertyType.Name}",
323+
ex
324+
);
325+
}
326+
}
289327

328+
return instance;
329+
}
330+
else if (rawResults.Count == fields.Length) // Just Fields
331+
{
290332
for (int i = 0; i < fields.Length; i++)
291333
{
292334
try
@@ -295,7 +337,10 @@ public async Task<T> Read<T>(string functionName, params object[] args)
295337
}
296338
catch (ArgumentException ex)
297339
{
298-
throw new UnityException($"Type mismatch assigning value to field {fields[i].Name}: expected {rawResults[i].GetType().Name}, got {fields[i].FieldType.Name}", ex);
340+
throw new UnityException(
341+
$"Type mismatch assigning value to field {fields[i].Name} of type {typeof(T).Name}: expected {rawResults[i].GetType().Name}, got {fields[i].FieldType.Name}",
342+
ex
343+
);
299344
}
300345
}
301346

@@ -304,7 +349,7 @@ public async Task<T> Read<T>(string functionName, params object[] args)
304349
else
305350
{
306351
throw new UnityException(
307-
$"The number of properties or fields in the target type do not match the number of results: expected {rawResults.Count}, got {properties.Length} properties and {fields.Length} fields"
352+
$"The number of combined properties and fields in type {typeof(T).Name} does not match the number of results: expected {combinedCount}, got {properties.Length} properties and {fields.Length} fields with {rawResults.Count} results."
308353
);
309354
}
310355
}

0 commit comments

Comments
 (0)