private static bool IsEmpty(this object value, Specification specification, bool skipDefault) { if (value == null) { return(true); } if (skipDefault && value.Equals(specification.DefaultValue.FromJust())) { return(true); } if (Nullable.GetUnderlyingType(specification.ConversionType) != null) { return(false); //nullable } #if !SKIP_FSHARP if (ReflectionHelper.IsFSharpOptionType(value.GetType()) && !FSharpOptionHelper.IsSome(value)) { return(true); } #endif if (value is ValueType && value.Equals(value.GetType().GetDefaultValue())) { return(true); } if (value is string && ((string)value).Length == 0) { return(true); } if (value is IEnumerable && !((IEnumerable)value).GetEnumerator().MoveNext()) { return(true); } return(false); }
public void Create_some() { var expected = FSharpOptionHelper.Some(FSharpOptionHelper.GetUnderlyingType(TestData.PropertyType), "with data"); expected.Should().BeOfType <FSharpOption <string> >(); FSharpOption <string> .get_IsSome((FSharpOption <string>) expected).Should().BeTrue(); }
private static bool IsEmpty(this object value) { if (value == null) { return(true); } #if !SKIP_FSHARP if (ReflectionHelper.IsFSharpOptionType(value.GetType()) && !FSharpOptionHelper.IsSome(value)) { return(true); } #endif if (value is ValueType && value.Equals(value.GetType().GetDefaultValue())) { return(true); } if (value is string && ((string)value).Length == 0) { return(true); } if (value is IEnumerable && !((IEnumerable)value).GetEnumerator().MoveNext()) { return(true); } return(false); }
private static Either <object, Exception> ChangeTypeScalarImpl(string value, Type conversionType, CultureInfo conversionCulture) { Func <string, object> changeType = input => { Func <object> safeChangeType = () => { var isFsOption = ReflectionHelper.IsFSharpOptionType(conversionType); Func <Type> getUnderlyingType = () => isFsOption ? FSharpOptionHelper.GetUnderlyingType(conversionType) : Nullable.GetUnderlyingType(conversionType); var type = getUnderlyingType() ?? conversionType; Func <object> withValue = () => isFsOption ? FSharpOptionHelper.Some(type, Convert.ChangeType(input, type, conversionCulture)) : Convert.ChangeType(input, type, conversionCulture); Func <object> empty = () => isFsOption?FSharpOptionHelper.None(type) : null; return((input == null) ? empty() : withValue()); }; return(input.IsBooleanString() ? input.ToBoolean() : conversionType.IsEnum ? input.ToEnum(conversionType) : safeChangeType()); }; return(Either.Protect(changeType, value)); }
private static Result <object, Exception> ChangeTypeScalarImpl(string value, Type conversionType, CultureInfo conversionCulture, bool ignoreValueCase) { Func <object> changeType = () => { Func <object> safeChangeType = () => { var isFsOption = ReflectionHelper.IsFSharpOptionType(conversionType); Func <Type> getUnderlyingType = () => #if !SKIP_FSHARP isFsOption ? FSharpOptionHelper.GetUnderlyingType(conversionType) : #endif Nullable.GetUnderlyingType(conversionType); var type = getUnderlyingType() ?? conversionType; Func <object> withValue = () => #if !SKIP_FSHARP isFsOption ? FSharpOptionHelper.Some(type, ConvertString(value, type, conversionCulture)) : #endif ConvertString(value, type, conversionCulture); #if !SKIP_FSHARP Func <object> empty = () => isFsOption?FSharpOptionHelper.None(type) : null; #else Func <object> empty = () => null; #endif return((value == null) ? empty() : withValue()); }; return(value.IsBooleanString() && conversionType == typeof(bool) ? value.ToBoolean() : conversionType.GetTypeInfo().IsEnum ? value.ToEnum(conversionType, ignoreValueCase) : safeChangeType()); }; Func <object> makeType = () => { try { var ctor = conversionType.GetTypeInfo().GetConstructor(new[] { typeof(string) }); return(ctor.Invoke(new object[] { value })); } catch (Exception) { throw new FormatException("Destination conversion type must have a constructor that accepts a string."); } }; return(Result.Try( conversionType.IsPrimitiveEx() || ReflectionHelper.IsFSharpOptionType(conversionType) ? changeType : makeType)); }
private static object NormalizeValue(this object value) { if (value != null && ReflectionHelper.IsFSharpOptionType(value.GetType()) && FSharpOptionHelper.IsSome(value)) { return(FSharpOptionHelper.ValueOf(value)); } return(value); }
private static Maybe <object> ChangeType(string value, Type conversionType, CultureInfo conversionCulture) { try { Func <object> safeChangeType = () => { var isFsOption = ReflectionHelper.IsFSharpOptionType(conversionType); Func <Type> getUnderlyingType = () => isFsOption ? FSharpOptionHelper.GetUnderlyingType(conversionType) : Nullable.GetUnderlyingType(conversionType); var type = getUnderlyingType() ?? conversionType; Func <object> withValue = () => isFsOption ? FSharpOptionHelper.Some(type, Convert.ChangeType(value, type, conversionCulture)) : Convert.ChangeType(value, type, conversionCulture); Func <object> empty = () => isFsOption ? FSharpOptionHelper.None(type) : null; return((value == null) ? empty() : withValue()); }; return(Maybe.Just( MatchBoolString(value) ? ConvertBoolString(value) : conversionType.IsEnum ? ConvertEnumString(value, conversionType) : safeChangeType())); } catch (InvalidCastException) { return(Maybe.Nothing <object>()); } catch (FormatException) { return(Maybe.Nothing <object>()); } catch (OverflowException) { return(Maybe.Nothing <object>()); } }
public void Create_none() { var expected = FSharpOptionHelper.None(FSharpOptionHelper.GetUnderlyingType(TestData.PropertyType)); FSharpOption <string> .get_IsNone((FSharpOption <string>) expected).Should().BeTrue(); }
public void Get_underlying_type() { FSharpOptionHelper.GetUnderlyingType(TestData.PropertyType).FullName .Should().BeEquivalentTo("System.String"); }