Skip to content

Commit 99f54ad

Browse files
committed
refactor: streamline property conversion logic in PropertyBag
1 parent 4cb5493 commit 99f54ad

File tree

2 files changed

+10
-65
lines changed

2 files changed

+10
-65
lines changed

src/Weaviate.Client/ObjectHelper.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -165,31 +165,6 @@ public static IDictionary<string, string>[] MakeBeacons(IEnumerable<Guid> guids)
165165
return ConvertValue(value, underlyingType);
166166
}
167167

168-
if (targetType == typeof(GeoCoordinate) && value is Rest.Dto.GeoCoordinates geo)
169-
{
170-
return geo.ToModel();
171-
}
172-
173-
if (targetType == typeof(Models.PhoneNumber) && value is Rest.Dto.PhoneNumber phoneNumber)
174-
{
175-
return phoneNumber.ToModel();
176-
}
177-
178-
if (targetType == typeof(byte[]) && value is string base64String)
179-
{
180-
return Convert.FromBase64String(base64String);
181-
}
182-
183-
if (targetType == typeof(DateTime) && value is string dateString)
184-
{
185-
return DateTime.SpecifyKind(DateTime.Parse(dateString), DateTimeKind.Utc);
186-
}
187-
188-
if (targetType == typeof(DateTime) && value is DateTime dateTime)
189-
{
190-
return DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
191-
}
192-
193168
// Handle nested objects (dictionaries -> custom types)
194169
if (
195170
value is IDictionary<string, object?> nestedDict

src/Weaviate.Client/Serialization/PropertyBag.cs

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,8 @@ public PropertyBag(IDictionary<string, object?> properties)
9898
if (!TryGetValue(name, out var value) || value is null)
9999
return null;
100100

101-
return value switch
102-
{
103-
DateTime dt => dt,
104-
string s => DateTime.Parse(s),
105-
_ => null,
106-
};
101+
var converter = PropertyConverterRegistry.Default.GetConverterForType(typeof(DateTime));
102+
return (DateTime?)converter?.FromRest(value, typeof(DateTime));
107103
}
108104

109105
/// <summary>
@@ -130,17 +126,10 @@ public PropertyBag(IDictionary<string, object?> properties)
130126
if (!TryGetValue(name, out var value) || value is null)
131127
return null;
132128

133-
if (value is GeoCoordinate geo)
134-
return geo;
135-
136-
if (value is IDictionary<string, object?> dict)
137-
{
138-
var lat = dict.TryGetValue("latitude", out var latVal) ? Convert.ToSingle(latVal) : 0f;
139-
var lon = dict.TryGetValue("longitude", out var lonVal) ? Convert.ToSingle(lonVal) : 0f;
140-
return new GeoCoordinate(lat, lon);
141-
}
142-
143-
return null;
129+
var converter = PropertyConverterRegistry.Default.GetConverterForType(
130+
typeof(GeoCoordinate)
131+
);
132+
return (GeoCoordinate?)converter?.FromRest(value, typeof(GeoCoordinate));
144133
}
145134

146135
/// <summary>
@@ -151,23 +140,8 @@ public PropertyBag(IDictionary<string, object?> properties)
151140
if (!TryGetValue(name, out var value) || value is null)
152141
return null;
153142

154-
if (value is PhoneNumber phone)
155-
return phone;
156-
157-
if (value is IDictionary<string, object?> dict)
158-
{
159-
var input = dict.TryGetValue("input", out var inputVal)
160-
? inputVal?.ToString() ?? ""
161-
: "";
162-
return new PhoneNumber(input)
163-
{
164-
DefaultCountry = dict.TryGetValue("defaultCountry", out var dc)
165-
? dc?.ToString()
166-
: null,
167-
};
168-
}
169-
170-
return null;
143+
var converter = PropertyConverterRegistry.Default.GetConverterForType(typeof(PhoneNumber));
144+
return (PhoneNumber?)converter?.FromRest(value, typeof(PhoneNumber));
171145
}
172146

173147
/// <summary>
@@ -178,12 +152,8 @@ public PropertyBag(IDictionary<string, object?> properties)
178152
if (!TryGetValue(name, out var value) || value is null)
179153
return null;
180154

181-
return value switch
182-
{
183-
byte[] bytes => bytes,
184-
string s => Convert.FromBase64String(s),
185-
_ => null,
186-
};
155+
var converter = PropertyConverterRegistry.Default.GetConverterForType(typeof(byte[]));
156+
return (byte[]?)converter?.FromRest(value, typeof(byte[]));
187157
}
188158

189159
/// <summary>

0 commit comments

Comments
 (0)