Reputation: 99
I noticed that MapResult does not have an overload for ParserResult<T>
. Is there a way to perform the same function that MapResult provides, and return a ParserResult<T>
rather than just an object or would I have to clone the repo and add this feature myself to the extensions class?
Let me explain what I am trying to do. I need to map old options to new models. So rather than change the deployed signatures everywhere, I just want to map the old model to the new model and still use the ParserResult<T>
as per usual. The issue I see is that I can map the old model to the new model using MapResult, but then I need to return the ParserResult<T>
for the new model. All the constructors for the ParserResult<T>
are internal and include sealed parameters. So I can't just new up a ParserResult<T>
.
What options do you propose to perform something like this? It would seem like I would not be the only one requiring something like this. I would rather not have to clone the repo to add this feature.
Solutions?
Thanks
Upvotes: 0
Views: 651
Reputation: 99
Alright,
I was kinda in a hurry for this one, so I ended up cloning the repo and adding an extension method to the ParserResultExtensions file.
Since the original method was called MapResult<T>
, I simply added a new method called MapParserResult<T>
that returns T wrapped in a Parsed<T>
or NotParsed<T>
object. Since Parsed<T>
and NotParsed<T>
both derive from ParseResult<T>
, it just works.
Here is the code I added:
`
public static ParserResult<TResult> MapParserResult<TSource, TResult>(this ParserResult<TSource> result, Func<TSource, TResult> parsedFunc, Func<IEnumerable<Error>, TResult> notParsedFunc)
{
var parsed = result as Parsed<TSource>;
if (parsed != null)
{
return new Parsed<TResult>(parsedFunc(parsed.Value));
}
return new NotParsed<TResult>(TypeInfo.Create(typeof(TResult)),((NotParsed<TSource>)result).Errors);
}
`
I hope someone finds this useful or even better, CommandLineParser adds this extension method to their code base so that others may replicate this behavior without having to clone the repo.
Upvotes: 1