Skip to content

Commit

Permalink
Merge pull request #648 from SteveDunn/interface-generation
Browse files Browse the repository at this point in the history
Add ability to switch off generating IVogen interface but still derive from it
  • Loading branch information
SteveDunn authored Jul 28, 2024
2 parents 9e5b8e0 + 067fc16 commit 7d0942c
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 15 deletions.
14 changes: 11 additions & 3 deletions src/Vogen.SharedTypes/StaticAbstractsGeneration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public enum StaticAbstractsGeneration
Unspecified = -1,

/// <summary>
/// Do not generate the factory.
/// Do not generate the interface definition, nor make the value objects derive implement the interface.
/// </summary>
Omit = 0,

Expand Down Expand Up @@ -53,9 +53,17 @@ public enum StaticAbstractsGeneration
InstanceMethodsAndProperties = 1 << 6,

/// <summary>
/// The most common usage; generates equals, explicit casts, and factory methods.
/// Value objects that are generated derive from `: IVogen`. Use just this flag if you have several projects and only wish to
/// generate the definition in one of them, or if you want to define it yourself.
/// </summary>
MostCommon = EqualsOperators |
ValueObjectsDeriveFromTheInterface = 1 << 7,

/// <summary>
/// The most common usage; generates the definition, containing equals, explicit casts, and factory methods, and
/// make value objects derive from it.
/// </summary>
MostCommon = ValueObjectsDeriveFromTheInterface |
EqualsOperators |
ExplicitCastFromPrimitive |
ExplicitCastToPrimitive |
FactoryMethods
Expand Down
11 changes: 6 additions & 5 deletions src/Vogen/WriteStaticAbstracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public static void WriteInterfacesAndMethodsIfNeeded(VogenConfiguration? globalC
StaticAbstractsGeneration generation = globalConfig?.StaticAbstractsGeneration ??
VogenConfiguration.DefaultInstance.StaticAbstractsGeneration;

if (generation == StaticAbstractsGeneration.Omit)
// we don't want to generate if omitting or if we're only _using_ the interface (it could be declared somewhere else)
if (generation is StaticAbstractsGeneration.Omit or StaticAbstractsGeneration.ValueObjectsDeriveFromTheInterface)
{
return;
}
Expand Down Expand Up @@ -127,16 +128,16 @@ string GenerateEqualsOperatorsIfNeeded()

public static string WriteHeaderIfNeeded(string precedingText, VoWorkItem item, TypeDeclarationSyntax tds)
{
if (item.Config.StaticAbstractsGeneration == StaticAbstractsGeneration.Omit)
if (item.LanguageVersion < LanguageVersion.CSharp11)
{
return string.Empty;
}

if (item.LanguageVersion >= LanguageVersion.CSharp11)
if (!item.Config.StaticAbstractsGeneration.HasFlag(StaticAbstractsGeneration.ValueObjectsDeriveFromTheInterface))
{
return precedingText + $" IVogen<{tds.Identifier}, {item.UnderlyingTypeFullName}>";
return string.Empty;
}

return string.Empty;
return precedingText + $" IVogen<{tds.Identifier}, {item.UnderlyingTypeFullName}>";
}
}
12 changes: 6 additions & 6 deletions tests/SnapshotTests/StaticAbstracts/StaticAbstractTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ public partial {{type}} MyVo { }
}

[Theory]
[InlineData("EqualsOperators")]
[InlineData("FactoryMethods")]
[InlineData("EqualsOperators", "FactoryMethods")]
[InlineData("InstanceMethodsAndProperties")]
[InlineData("InstanceMethodsAndProperties", "EqualsOperators", "FactoryMethods")]
[InlineData("InstanceMethodsAndProperties", "FactoryMethods", "EqualsOperators")]
[InlineData("ValueObjectsDeriveFromTheInterface", "EqualsOperators")]
[InlineData("ValueObjectsDeriveFromTheInterface", "FactoryMethods")]
[InlineData("ValueObjectsDeriveFromTheInterface", "EqualsOperators", "FactoryMethods")]
[InlineData("ValueObjectsDeriveFromTheInterface", "InstanceMethodsAndProperties")]
[InlineData("ValueObjectsDeriveFromTheInterface", "InstanceMethodsAndProperties", "EqualsOperators", "FactoryMethods")]
[InlineData("ValueObjectsDeriveFromTheInterface", "InstanceMethodsAndProperties", "FactoryMethods", "EqualsOperators")]
public async Task Generates_code_for_different_variations(params string[] flags)
{
string attrs = string.Join(" | ", flags.Select(f => $"StaticAbstractsGeneration.{f}"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ using Vogen;

[global::System.Diagnostics.DebuggerTypeProxyAttribute(typeof(MyVoDebugView))]
[global::System.Diagnostics.DebuggerDisplayAttribute("Underlying type: System.Guid, Value = { _value }")]
public partial class MyVo : global::System.IEquatable<MyVo>, global::System.IEquatable<System.Guid> , global::System.IComparable<MyVo>, global::System.IComparable, global::System.IParsable<MyVo>, global::System.ISpanParsable<MyVo>, IVogen<MyVo, System.Guid>
public partial class MyVo : global::System.IEquatable<MyVo>, global::System.IEquatable<System.Guid> , global::System.IComparable<MyVo>, global::System.IComparable, global::System.IParsable<MyVo>, global::System.ISpanParsable<MyVo>
{
#if DEBUG
private readonly global::System.Diagnostics.StackTrace _stackTrace = null;
Expand Down

0 comments on commit 7d0942c

Please sign in to comment.