-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathPropertyTracker.cs
More file actions
72 lines (63 loc) · 2.88 KB
/
PropertyTracker.cs
File metadata and controls
72 lines (63 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
namespace NamespacePrefixPlaceholder.PowerShell.Models
{
public class PropertyTracker
{
private readonly HashSet<string> _trackedProperties = new HashSet<string>();
public void TrackProperty(string propertyName)
{
_trackedProperties.Add(propertyName); // ✅ Track properties that are set
}
public bool IsPropertySet(string propertyName)
{
// Ensure that the first character of the property name is UpperCase
if (propertyName.Length > 0)
{
propertyName = char.ToUpper(propertyName[0]) + propertyName.Substring(1);
}
return _trackedProperties.Contains(propertyName);
}
public static T SanitizeValue<T>(object value)
{
if (typeof(T) == typeof(string))
{
return (T)(object)(string.IsNullOrEmpty(value as string) ? null : value);
}
return (T)value;
}
public static NamespacePrefixPlaceholder.PowerShell.Runtime.Json.JsonNode ConvertToJsonNode(Type propertyType, object value)
{
if (value == null)
{
return new NamespacePrefixPlaceholder.PowerShell.Runtime.Json.JsonString("null"); // Explicitly return null if the property is set to null
}
// Get the declared property type using reflection
// Handle different types based on the declared type
if (propertyType == typeof(string))
{
return new NamespacePrefixPlaceholder.PowerShell.Runtime.Json.JsonString(value.ToString());
}
else if (propertyType == typeof(int) || propertyType == typeof(int?) ||
propertyType == typeof(long) || propertyType == typeof(long?) ||
propertyType == typeof(short) || propertyType == typeof(short?))
{
return new NamespacePrefixPlaceholder.PowerShell.Runtime.Json.JsonNumber(Convert.ToDouble(value));
}
else if (propertyType == typeof(bool) || propertyType == typeof(bool?))
{
return new NamespacePrefixPlaceholder.PowerShell.Runtime.Json.JsonBoolean((bool)value);
}
else if (propertyType.IsEnum)
{
return new NamespacePrefixPlaceholder.PowerShell.Runtime.Json.JsonString(value.ToString());
}
else if (propertyType == typeof(DateTime) || propertyType == typeof(DateTime?))
{
return new NamespacePrefixPlaceholder.PowerShell.Runtime.Json.JsonString(((DateTime)value).ToString("o")); // ISO 8601 format
}
// Fallback to JSON object if the type is complex
return NamespacePrefixPlaceholder.PowerShell.Runtime.Json.JsonObject.FromObject(value);
}
}
}