Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit f92fb84

Browse files
committed
Move ConvertTo tests to SS.Text + handle empty string to ValueType
1 parent d51e76e commit f92fb84

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

src/ServiceStack.Text/AutoMappingUtils.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,38 +160,46 @@ public static object ConvertTo(this object from, Type toType, bool skipConverter
160160

161161
public static object ChangeValueType(object from, Type toType)
162162
{
163+
var s = from as string;
164+
163165
var fromType = from.GetType();
164166
if (!fromType.IsEnum && !toType.IsEnum)
165167
{
166-
if (toType == typeof(char) && from is string s)
168+
var toString = toType == typeof(string);
169+
if (toType == typeof(char) && s != null)
167170
return s.Length > 0 ? (object) s[0] : null;
168-
if (toType == typeof(string) && from is char c)
171+
if (toString && from is char c)
169172
return c.ToString();
170173
if (toType == typeof(TimeSpan) && from is long ticks)
171174
return new TimeSpan(ticks);
172175
if (toType == typeof(long) && from is TimeSpan time)
173176
return time.Ticks;
174177

175178
var destNumberType = DynamicNumber.GetNumber(toType);
176-
var value = destNumberType?.ConvertFrom(from);
177-
if (value != null)
179+
if (destNumberType != null)
178180
{
179-
if (toType == typeof(char))
180-
return value.ToString()[0];
181-
182-
return value;
181+
if (s != null && s == string.Empty)
182+
return destNumberType.DefaultValue;
183+
184+
var value = destNumberType.ConvertFrom(from);
185+
if (value != null)
186+
{
187+
return toType == typeof(char)
188+
? value.ToString()[0]
189+
: value;
190+
}
183191
}
184192

185-
if (toType == typeof(string))
193+
if (toString)
186194
{
187195
var srcNumberType = DynamicNumber.GetNumber(from.GetType());
188196
if (srcNumberType != null)
189-
return srcNumberType.ToString(@from);
197+
return srcNumberType.ToString(from);
190198
}
191199
}
192200

193-
if (from is string strValue)
194-
return TypeSerializer.DeserializeFromString(strValue, toType);
201+
if (s != null)
202+
return TypeSerializer.DeserializeFromString(s, toType);
195203

196204
if (toType == typeof(string))
197205
return from.ToJsv();

src/ServiceStack.Text/DynamicNumber.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public interface IDynamicNumber
1313
object ConvertFrom(object value);
1414
bool TryParse(string str, out object result);
1515
string ToString(object value);
16+
object DefaultValue { get; }
1617

1718
object add(object lhs, object rhs);
1819
object sub(object lhs, object rhs);
@@ -53,6 +54,7 @@ public bool TryParse(string str, out object result)
5354
}
5455

5556
public string ToString(object value) => Convert(value).ToString();
57+
public object DefaultValue => default(sbyte);
5658

5759
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
5860
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
@@ -93,6 +95,7 @@ public bool TryParse(string str, out object result)
9395
}
9496

9597
public string ToString(object value) => Convert(value).ToString();
98+
public object DefaultValue => default(byte);
9699

97100
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
98101
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
@@ -133,6 +136,7 @@ public bool TryParse(string str, out object result)
133136
}
134137

135138
public string ToString(object value) => Convert(value).ToString();
139+
public object DefaultValue => default(short);
136140

137141
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
138142
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
@@ -173,6 +177,7 @@ public bool TryParse(string str, out object result)
173177
}
174178

175179
public string ToString(object value) => Convert(value).ToString();
180+
public object DefaultValue => default(ushort);
176181

177182
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
178183
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
@@ -213,6 +218,7 @@ public bool TryParse(string str, out object result)
213218
}
214219

215220
public string ToString(object value) => Convert(value).ToString();
221+
public object DefaultValue => default(int);
216222

217223
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
218224
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
@@ -253,6 +259,7 @@ public bool TryParse(string str, out object result)
253259
}
254260

255261
public string ToString(object value) => Convert(value).ToString();
262+
public object DefaultValue => default(uint);
256263

257264
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
258265
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
@@ -293,6 +300,7 @@ public bool TryParse(string str, out object result)
293300
}
294301

295302
public string ToString(object value) => Convert(value).ToString();
303+
public object DefaultValue => default(long);
296304

297305
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
298306
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
@@ -333,6 +341,7 @@ public bool TryParse(string str, out object result)
333341
}
334342

335343
public string ToString(object value) => Convert(value).ToString();
344+
public object DefaultValue => default(ulong);
336345

337346
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
338347
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
@@ -373,6 +382,7 @@ public bool TryParse(string str, out object result)
373382
}
374383

375384
public string ToString(object value) => Convert(value).ToString("r", CultureInfo.InvariantCulture);
385+
public object DefaultValue => default(float);
376386

377387
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
378388
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
@@ -413,6 +423,7 @@ public bool TryParse(string str, out object result)
413423
}
414424

415425
public string ToString(object value) => Convert(value).ToString("r", CultureInfo.InvariantCulture);
426+
public object DefaultValue => default(double);
416427

417428
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
418429
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);
@@ -453,6 +464,7 @@ public bool TryParse(string str, out object result)
453464
}
454465

455466
public string ToString(object value) => Convert(value).ToString(CultureInfo.InvariantCulture);
467+
public object DefaultValue => default(decimal);
456468

457469
public object add(object lhs, object rhs) => Convert(lhs) + Convert(rhs);
458470
public object sub(object lhs, object rhs) => Convert(lhs) - Convert(rhs);

tests/ServiceStack.Text.Tests/AutoMappingTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,5 +1076,27 @@ public void Can_convert_to_array_to_string()
10761076
Assert.That(new []{ new ViewModel { Public = "A"} }.ConvertTo<string>(), Is.Not.Empty);
10771077
Assert.That(new []{ DayOfWeek.Monday }.ConvertTo<string>(), Is.Not.Empty);
10781078
}
1079+
1080+
[Test]
1081+
public void To_works_with_ValueTypes()
1082+
{
1083+
Assert.That(1.ToString().ConvertTo<int>(), Is.EqualTo(1));
1084+
}
1085+
1086+
[Test]
1087+
public void To_on_null_or_empty_string_returns_default_value_supplied()
1088+
{
1089+
const string nullString = null;
1090+
Assert.That("".ConvertTo(1), Is.EqualTo(1));
1091+
Assert.That("".ConvertTo(default(int)), Is.EqualTo(default(int)));
1092+
Assert.That(nullString.ConvertTo(1), Is.EqualTo(1));
1093+
}
1094+
1095+
[Test]
1096+
public void To_ValueType_on_null_or_empty_string_returns_default_value()
1097+
{
1098+
Assert.That("".ConvertTo<int>(), Is.EqualTo(default(int)));
1099+
}
1100+
10791101
}
10801102
}

0 commit comments

Comments
 (0)