Skip to content

Commit

Permalink
Added explicit conversion from key to type
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelGerr committed Jan 11, 2021
1 parent 2f9931b commit 18c1dd2
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ private static void DemoForNonValidatableEnum(ILogger logger)
var productType = ProductType.Get("Groceries");
logger.Information("Product type: {type}", productType);

productType = (ProductType)"Groceries";
logger.Information("Explicitly casted product type: {type}", productType);

if (ProductType.TryGet("Housewares", out var housewares))
logger.Information("Product type {type} with TryGet found", housewares);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ internal static void ModuleInit()

GeneratedTryGet();
GenerateImplicitConversion();
GenerateExplicitConversion();
GenerateEqualityOperators();
GenerateTypedEquals();

Expand Down Expand Up @@ -216,7 +217,7 @@ private void GenerateImplicitConversion()
_sb.Append($@"
/// <summary>
/// Implicit conversion to the type of <see cref=""{_state.KeyType}""/>.
/// Implicit conversion to the type <see cref=""{_state.KeyType}""/>.
/// </summary>
/// <param name=""item"">Item to covert.</param>
/// <returns>The <see cref=""{_state.KeyPropertyName}""/> of provided <paramref name=""item""/> or <c>default</c> if <paramref name=""item""/> is <c>null</c>.</returns>
Expand All @@ -239,6 +240,22 @@ private void GenerateImplicitConversion()
}}");
}

private void GenerateExplicitConversion()
{
_sb.Append($@"
/// <summary>
/// Explicit conversion from the type <see cref=""{_state.KeyType}""/>.
/// </summary>
/// <param name=""{_state.KeyArgumentName}"">Value to covert.</param>
/// <returns>An instance of <see cref=""{_state.EnumIdentifier}""/> if the <paramref name=""{_state.KeyArgumentName}""/> is a known item or implements <see cref=""IValidatableEnum{{TKey}}""/>.</returns>
[return: NotNullIfNotNull(""{_state.KeyArgumentName}"")]
public static explicit operator {_state.EnumIdentifier}{_state.NullableQuestionMarkEnum}({_state.KeyType}{_state.NullableQuestionMarkKey} {_state.KeyArgumentName})
{{
return {_state.EnumIdentifier}.Get({_state.KeyArgumentName});
}}");
}

private void GenerateTypedEquals()
{
_sb.Append($@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ internal static void ModuleInit()
{
GenerateFactoryMethod(_state.KeyMember);
GenerateImplicitConversion(_state.KeyMember);
GenerateExplicitConversion(_state.KeyMember);
}

GenerateConstructor();
Expand All @@ -140,7 +141,7 @@ private void GenerateImplicitConversion(InstanceMemberInfo keyMember)
_sb.Append($@"
/// <summary>
/// Implicit conversion to the type of <see cref=""{keyMember.Type}""/>.
/// Implicit conversion to the type <see cref=""{keyMember.Type}""/>.
/// </summary>
/// <param name=""obj"">Object to covert.</param>
/// <returns>The <see cref=""{keyMember.Identifier}""/> of provided <paramref name=""obj""/> or <c>default</c> if <paramref name=""obj""/> is <c>null</c>.</returns>
Expand All @@ -163,6 +164,21 @@ private void GenerateImplicitConversion(InstanceMemberInfo keyMember)
}}");
}

private void GenerateExplicitConversion(InstanceMemberInfo keyMember)
{
_sb.Append($@"
/// <summary>
/// Explicit conversion from the type <see cref=""{keyMember.Type}""/>.
/// </summary>
/// <param name=""{keyMember.ArgumentName}"">Value to covert.</param>
/// <returns>An instance of <see cref=""{_state.TypeIdentifier}""/>.</returns>
public static explicit operator {_state.TypeIdentifier}({keyMember.Type} {keyMember.ArgumentName})
{{
return {_state.TypeIdentifier}.Create({keyMember.ArgumentName});
}}");
}

private void GenerateFactoryMethod(InstanceMemberInfo keyMember)
{
_sb.Append($@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal static void ModuleInit()
Expression<Func<TestEnum, string?>> convertToKeyExpression = item => item.Key;
var enumType = typeof(TestEnum);
var metadata = new ValueTypeMetadata(enumType, typeof(string), convertFromKey, convertFromKeyExpression, convertToKey, convertToKeyExpression);
var metadata = new ValueTypeMetadata(enumType, typeof(string), false, convertFromKey, convertFromKeyExpression, convertToKey, convertToKeyExpression);
ValueTypeMetadataLookup.AddMetadata(enumType, metadata);
}
Expand Down Expand Up @@ -144,7 +144,7 @@ public static bool TryGet([AllowNull] string key, [MaybeNullWhen(false)] out Tes
}
/// <summary>
/// Implicit conversion to the type of <see cref=""string""/>.
/// Implicit conversion to the type <see cref=""string""/>.
/// </summary>
/// <param name=""item"">Item to covert.</param>
/// <returns>The <see cref=""Key""/> of provided <paramref name=""item""/> or <c>default</c> if <paramref name=""item""/> is <c>null</c>.</returns>
Expand All @@ -154,6 +154,17 @@ public static bool TryGet([AllowNull] string key, [MaybeNullWhen(false)] out Tes
return item is null ? default : item.Key;
}
/// <summary>
/// Explicit conversion from the type <see cref=""string""/>.
/// </summary>
/// <param name=""key"">Value to covert.</param>
/// <returns>An instance of <see cref=""TestEnum""/> if the <paramref name=""key""/> is a known item or implements <see cref=""IValidatableEnum{TKey}""/>.</returns>
[return: NotNullIfNotNull(""key"")]
public static explicit operator TestEnum?(string? key)
{
return TestEnum.Get(key);
}
/// <summary>
/// Compares to instances of <see cref=""TestEnum""/>.
/// </summary>
Expand Down Expand Up @@ -336,7 +347,7 @@ internal static void ModuleInit()
Expression<Func<TestEnum, string?>> convertToKeyExpression = item => item.Key;
var enumType = typeof(TestEnum);
var metadata = new ValueTypeMetadata(enumType, typeof(string), convertFromKey, convertFromKeyExpression, convertToKey, convertToKeyExpression);
var metadata = new ValueTypeMetadata(enumType, typeof(string), true, convertFromKey, convertFromKeyExpression, convertToKey, convertToKeyExpression);
ValueTypeMetadataLookup.AddMetadata(enumType, metadata);
}
Expand Down Expand Up @@ -447,7 +458,7 @@ public static bool TryGet([AllowNull] string key, [MaybeNullWhen(false)] out Tes
}
/// <summary>
/// Implicit conversion to the type of <see cref=""string""/>.
/// Implicit conversion to the type <see cref=""string""/>.
/// </summary>
/// <param name=""item"">Item to covert.</param>
/// <returns>The <see cref=""Key""/> of provided <paramref name=""item""/> or <c>default</c> if <paramref name=""item""/> is <c>null</c>.</returns>
Expand All @@ -457,6 +468,17 @@ public static bool TryGet([AllowNull] string key, [MaybeNullWhen(false)] out Tes
return item is null ? default : item.Key;
}
/// <summary>
/// Explicit conversion from the type <see cref=""string""/>.
/// </summary>
/// <param name=""key"">Value to covert.</param>
/// <returns>An instance of <see cref=""TestEnum""/> if the <paramref name=""key""/> is a known item or implements <see cref=""IValidatableEnum{TKey}""/>.</returns>
[return: NotNullIfNotNull(""key"")]
public static explicit operator TestEnum?(string? key)
{
return TestEnum.Get(key);
}
/// <summary>
/// Compares to instances of <see cref=""TestEnum""/>.
/// </summary>
Expand Down Expand Up @@ -609,7 +631,7 @@ internal static void ModuleInit()
Expression<Func<TestEnum, string?>> convertToKeyExpression = item => item.Key;
var enumType = typeof(TestEnum);
var metadata = new ValueTypeMetadata(enumType, typeof(string), convertFromKey, convertFromKeyExpression, convertToKey, convertToKeyExpression);
var metadata = new ValueTypeMetadata(enumType, typeof(string), true, convertFromKey, convertFromKeyExpression, convertToKey, convertToKeyExpression);
ValueTypeMetadataLookup.AddMetadata(enumType, metadata);
}
Expand Down Expand Up @@ -717,7 +739,7 @@ public static bool TryGet([AllowNull] string key, [MaybeNullWhen(false)] out Tes
}
/// <summary>
/// Implicit conversion to the type of <see cref=""string""/>.
/// Implicit conversion to the type <see cref=""string""/>.
/// </summary>
/// <param name=""item"">Item to covert.</param>
/// <returns>The <see cref=""Key""/> of provided <paramref name=""item""/> or <c>default</c> if <paramref name=""item""/> is <c>null</c>.</returns>
Expand All @@ -727,6 +749,17 @@ public static bool TryGet([AllowNull] string key, [MaybeNullWhen(false)] out Tes
return item.Key;
}
/// <summary>
/// Explicit conversion from the type <see cref=""string""/>.
/// </summary>
/// <param name=""key"">Value to covert.</param>
/// <returns>An instance of <see cref=""TestEnum""/> if the <paramref name=""key""/> is a known item or implements <see cref=""IValidatableEnum{TKey}""/>.</returns>
[return: NotNullIfNotNull(""key"")]
public static explicit operator TestEnum(string? key)
{
return TestEnum.Get(key);
}
/// <summary>
/// Compares to instances of <see cref=""TestEnum""/>.
/// </summary>
Expand Down Expand Up @@ -898,7 +931,7 @@ internal static void ModuleInit()
Expression<Func<TestEnum, string?>> convertToKeyExpression = item => item.Name;
var enumType = typeof(TestEnum);
var metadata = new ValueTypeMetadata(enumType, typeof(string), convertFromKey, convertFromKeyExpression, convertToKey, convertToKeyExpression);
var metadata = new ValueTypeMetadata(enumType, typeof(string), true, convertFromKey, convertFromKeyExpression, convertToKey, convertToKeyExpression);
ValueTypeMetadataLookup.AddMetadata(enumType, metadata);
}
Expand Down Expand Up @@ -1009,7 +1042,7 @@ public static bool TryGet([AllowNull] string name, [MaybeNullWhen(false)] out Te
}
/// <summary>
/// Implicit conversion to the type of <see cref=""string""/>.
/// Implicit conversion to the type <see cref=""string""/>.
/// </summary>
/// <param name=""item"">Item to covert.</param>
/// <returns>The <see cref=""Name""/> of provided <paramref name=""item""/> or <c>default</c> if <paramref name=""item""/> is <c>null</c>.</returns>
Expand All @@ -1019,6 +1052,17 @@ public static bool TryGet([AllowNull] string name, [MaybeNullWhen(false)] out Te
return item is null ? default : item.Name;
}
/// <summary>
/// Explicit conversion from the type <see cref=""string""/>.
/// </summary>
/// <param name=""name"">Value to covert.</param>
/// <returns>An instance of <see cref=""TestEnum""/> if the <paramref name=""name""/> is a known item or implements <see cref=""IValidatableEnum{TKey}""/>.</returns>
[return: NotNullIfNotNull(""name"")]
public static explicit operator TestEnum?(string? name)
{
return TestEnum.Get(name);
}
/// <summary>
/// Compares to instances of <see cref=""TestEnum""/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ internal static void ModuleInit()
Expression<Func<TestValueType, string>> convertToKeyExpression = obj => obj.ReferenceField;
var type = typeof(TestValueType);
var metadata = new ValueTypeMetadata(type, typeof(string), convertFromKey, convertFromKeyExpression, convertToKey, convertToKeyExpression);
var metadata = new ValueTypeMetadata(type, typeof(string), false, convertFromKey, convertFromKeyExpression, convertToKey, convertToKeyExpression);
ValueTypeMetadataLookup.AddMetadata(type, metadata);
}
Expand Down Expand Up @@ -330,7 +330,7 @@ public static bool TryCreate(
static partial void ValidateFactoryArguments(ref string referenceField);
/// <summary>
/// Implicit conversion to the type of <see cref=""string""/>.
/// Implicit conversion to the type <see cref=""string""/>.
/// </summary>
/// <param name=""obj"">Object to covert.</param>
/// <returns>The <see cref=""ReferenceField""/> of provided <paramref name=""obj""/> or <c>default</c> if <paramref name=""obj""/> is <c>null</c>.</returns>
Expand All @@ -340,6 +340,16 @@ public static bool TryCreate(
return obj is null ? default : obj.ReferenceField;
}
/// <summary>
/// Explicit conversion from the type <see cref=""string""/>.
/// </summary>
/// <param name=""referenceField"">Value to covert.</param>
/// <returns>An instance of <see cref=""TestValueType""/>.</returns>
public static explicit operator TestValueType(string referenceField)
{
return TestValueType.Create(referenceField);
}
private TestValueType(string referenceField)
{
ValidateConstructorArguments(ref referenceField);
Expand Down

0 comments on commit 18c1dd2

Please sign in to comment.