-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4393f63
commit 959cc45
Showing
14 changed files
with
212 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Tomlet.Tests.CommentProvider; | ||
|
||
public class TestInlineCommentProvider : ICommentProvider | ||
{ | ||
public static Dictionary<string, string> Comments = new Dictionary<string, string>(); | ||
|
||
private readonly string _name; | ||
|
||
public TestInlineCommentProvider(string name) | ||
{ | ||
_name = name; | ||
} | ||
|
||
public string GetComment() | ||
{ | ||
return Comments[_name]; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
Tomlet.Tests/CommentProvider/TestPrecedingCommentProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Tomlet.Tests.CommentProvider; | ||
|
||
public class TestPrecedingCommentProvider : ICommentProvider | ||
{ | ||
public static Dictionary<string, string> Comments = new Dictionary<string, string>(); | ||
|
||
private readonly string _name; | ||
|
||
public TestPrecedingCommentProvider(string name) | ||
{ | ||
_name = name; | ||
} | ||
|
||
public string GetComment() | ||
{ | ||
return Comments[_name]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Tomlet.Attributes; | ||
using Tomlet.Tests.CommentProvider; | ||
|
||
namespace Tomlet.Tests.TestModelClasses; | ||
|
||
public class CommentProviderTestModel | ||
{ | ||
[TomlPrecedingCommentProvider(typeof(TestPrecedingCommentProvider), new object[] { "PrecedingComment" })] | ||
[TomlInlineComment("PlainInlineComment")] | ||
public string PrecedingComment { get; set; } | ||
|
||
[TomlInlineCommentProvider(typeof(TestInlineCommentProvider), new object[] { "InlineComment" })] | ||
[TomlPrecedingComment("PlainPrecedingComment")] | ||
public string InlineComment { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace Tomlet.Attributes; | ||
|
||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] | ||
public class TomlCommentProviderAttribute : Attribute | ||
{ | ||
private readonly Type _provider; | ||
private readonly object[] _args; | ||
private readonly Type[] _constructorParamsType; | ||
|
||
public string GetComment() | ||
{ | ||
var constructor = _provider.GetConstructor(_constructorParamsType) ?? | ||
throw new ArgumentException("Fail to get a constructor matching the parameters"); | ||
var instance = constructor.Invoke(_args) as ICommentProvider ?? | ||
throw new Exception("Fail to create an instance of the provider"); | ||
return instance.GetComment(); | ||
} | ||
|
||
public TomlCommentProviderAttribute(Type provider, object[] args) | ||
{ | ||
if (!typeof(ICommentProvider).IsAssignableFrom(provider)) | ||
{ | ||
throw new ArgumentException("Provider must implement ICommentProvider"); | ||
} | ||
|
||
_provider = provider; | ||
_args = args ?? new object[] { }; | ||
_constructorParamsType = args?.Select(a => a.GetType()).ToArray() ?? new Type[] { }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
|
||
namespace Tomlet.Attributes; | ||
|
||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] | ||
public class TomlInlineCommentProviderAttribute : TomlCommentProviderAttribute | ||
{ | ||
public TomlInlineCommentProviderAttribute(Type provider) : base(provider, new object[] { }) | ||
{ | ||
} | ||
|
||
public TomlInlineCommentProviderAttribute(Type provider, object[] args) : base(provider, args) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
Tomlet/Attributes/TomlPrecedingCommentProviderAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
|
||
namespace Tomlet.Attributes; | ||
|
||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)] | ||
public class TomlPrecedingCommentProviderAttribute : TomlCommentProviderAttribute | ||
{ | ||
public TomlPrecedingCommentProviderAttribute(Type provider) : base(provider, new object[] { }) | ||
{ | ||
} | ||
|
||
public TomlPrecedingCommentProviderAttribute(Type provider, object[] args) : base(provider, args) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
|
||
namespace Tomlet; | ||
|
||
internal static class CommentProviderUtil | ||
{ | ||
public static string GetComment(Type provider) | ||
{ | ||
var constructor = provider.GetConstructor(Type.EmptyTypes); | ||
if (constructor == null) | ||
{ | ||
throw new ArgumentException("Provider must have a parameterless constructor"); | ||
} | ||
|
||
var instance = (ICommentProvider)constructor.Invoke(null); | ||
return instance.GetComment(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Tomlet; | ||
|
||
public interface ICommentProvider | ||
{ | ||
public string GetComment(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace Tomlet; | ||
|
||
internal class TomlSimpleCommentProvider : ICommentProvider | ||
{ | ||
private readonly string _comment; | ||
|
||
public TomlSimpleCommentProvider(string comment) | ||
{ | ||
_comment = comment; | ||
} | ||
|
||
public string GetComment() | ||
{ | ||
return _comment; | ||
} | ||
} |