-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Value converters for StringTemplate.Format() #12
Value converters for StringTemplate.Format() #12
Conversation
Salut Melvyn ! I like the idea, but I think it would be better to implement converters as classes. Each converter would implement a public interface IStringTemplateValueConverter
{
bool CanConvert(object value);
object Convert(object value);
} And the attribute would refer to the convert type: class Request
{
[StringTemplateValueConverter(typeof(CustomDayOfWeekConverter))]
public DayOfWeek Day { get; set; }
[StringTemplateValueConverter(typeof(CommaSeparatedValuesConverter))]
public IReadOnlyCollection<int> Collection { get; set; }
}
public class CommaSeparatedValueConverter : IStringTemplateValueConverter
{
public bool CanConvert(object value) => value is IEnumerable<object>);
public object Convert(object value) => string.Join(",", ((IEnumerable)value).Cast<object>());
}
public class CustomDayOfWeekConverter : IStringTemplateValueConverter
{
public bool CanConvert(object value) => value is DayOfWeek);
public object Convert(object value) => ((DayOfWeek)value).ToString().ToLowerInvariant();
} Pros:
|
Hey! Thanks for your comment. I agree converter classes feel cleaner, but I wasn't sure it would be worth the effort. I liked the simpleness of NUnit test case sources... I'm a bit sad to lose the convenience and freedom allowed by Anyway, I'll see what I can do. I'm not sure about the way to go for caching though. Should I create another cache instance just for the converters? |
Yes, I think it would be best Thanks ! |
With the StringTemplateConverterAttribute, a property or field from an object input into StringTemplate.Format() can be converted by a user-defined converter prior to being formatted. Converters are implementations of IStringTemplateValueConverter. StringTemplateValueConverter<T> provides a typed generic implementation of IStringTemplateValueConverter.
I finally got the time to do a proper implementation with class converters! :) Things to note:
There is a slight issue (that might just not be one...) with the current implementation and caching: This worries me a bit, BUT it's probably not that important, because all the caches are currently cleared together. This might be a problem if this changes though... EDIT: I added a unit test for that. It's not foolproof but it's a start (it can only check What do you think? |
Fine by me!
Oh, I didn't think of that.
I don't think it's a problem. I don't intend to expose a way to clear caches selectively. In fact, I'd rather not have the Now I'll do a proper review ;) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pretty good! I have a few comments, but they're quite minor.
Thanks !
Just checking in: is something blocking this PR, or did you simply not have the time to merge it yet? |
Ah, I didn't get a notification for your last push... I'll merge it now. Thanks @mlaily! |
Thanks a lot! :) Any estimated time frame for a new release? (Not that it's pressing or anything) |
This PR introduces a
StringTemplateValueConverterAttribute
that can be applied on a property or field that is going to be formatted by one of theStringTemplate.Format()
overloads taking an object as argument.This attribute allows the value of the property or field it is applied on to be converted by a user-defined method prior to being formatted by
StringTemplate.Format()
.Why use this, and not a custom format provider?
Example
The following class:
Can be used like that: