Refactor variable mapping and improve type alias handling

Updated `VariableMapper` for null assignment consistency and streamlined `VariableModel` instantiation. Improved `TypeJsonConverter` to handle list type aliases more explicitly, replacing ambiguous syntax with clearer format.
This commit is contained in:
Sipke Schoorstra 2025-01-15 10:50:34 +01:00
parent aaab19509e
commit 2c3843d5f5
2 changed files with 5 additions and 5 deletions

View file

@ -40,9 +40,9 @@ public class TypeJsonConverter : JsonConverter<Type>
}
// Handle collection types.
if (typeAlias.EndsWith("()"))
if (typeAlias.StartsWith("List<") && typeAlias.EndsWith(">"))
{
var elementTypeAlias = typeAlias[..^"()".Length];
var elementTypeAlias = typeAlias[5..^1];
var elementType = _wellKnownTypeRegistry.TryGetType(elementTypeAlias, out var t) ? t : Type.GetType(elementTypeAlias)!;
return typeof(List<>).MakeGenericType(elementType);
}
@ -70,7 +70,7 @@ public class TypeJsonConverter : JsonConverter<Type>
if (typedEnumerable.IsAssignableFrom(value) && _wellKnownTypeRegistry.TryGetAlias(elementType, out var elementTypeAlias))
{
writer.WriteStringValue($"{elementTypeAlias}()");
writer.WriteStringValue($"List<{elementTypeAlias}>");
return;
}
}

View file

@ -59,7 +59,7 @@ public class VariableMapper
.OnSuccess(value => variable.Value = value)
.OnFailure(e => _logger.LogWarning("Failed to convert {SourceValue} to {TargetType}", source.Value, type.Name));
variable.StorageDriverType = !string.IsNullOrEmpty(source.StorageDriverTypeName) ? Type.GetType(source.StorageDriverTypeName) : default;
variable.StorageDriverType = !string.IsNullOrEmpty(source.StorageDriverTypeName) ? Type.GetType(source.StorageDriverTypeName) : null;
return variable;
}
@ -76,6 +76,6 @@ public class VariableMapper
var storageDriverTypeName = source.StorageDriverType?.GetSimpleAssemblyQualifiedName();
var serializedValue = value.Format();
return new VariableModel(source.Id, source.Name, valueTypeAlias, serializedValue, storageDriverTypeName);
return new(source.Id, source.Name, valueTypeAlias, serializedValue, storageDriverTypeName);
}
}