-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a descriptor for the runtime type parser (#113)
* feat: add a descriptor for the runtime type parser * docs: review for all fluent APIs --------- Co-authored-by: codefactor-io <[email protected]>
- Loading branch information
1 parent
59697bc
commit f2fcf59
Showing
21 changed files
with
446 additions
and
68 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
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
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
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
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
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,9 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace PocketCsvReader.Configuration; | ||
public delegate object ParseFunction(string input); | ||
public delegate T ParseFunction<T>(string input); |
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,31 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace PocketCsvReader.Configuration; | ||
public class RuntimeParsersDescriptor : IEnumerable<KeyValuePair<Type, ParseFunction>> | ||
{ | ||
private Dictionary<Type, ParseFunction> Parsers { get; init; } = []; | ||
|
||
public void AddParser<T>(ParseFunction<T> parse) | ||
=> AddParser(typeof(T), (string str) => parse.Invoke(str)!); | ||
|
||
public void AddParser(Type type, ParseFunction parse) | ||
{ | ||
var returnType = parse.Method.ReturnType; | ||
if (!type.IsAssignableTo(returnType)) | ||
throw new ArgumentException($"The provided parser returns {returnType}, which is not assignable from {type}."); | ||
|
||
Parsers.Add(type, parse); | ||
} | ||
|
||
public int Count => Parsers.Count; | ||
|
||
public IEnumerator<KeyValuePair<Type, ParseFunction>> GetEnumerator() | ||
=> Parsers.GetEnumerator(); | ||
IEnumerator IEnumerable.GetEnumerator() | ||
=> GetEnumerator(); | ||
} |
37 changes: 37 additions & 0 deletions
37
PocketCsvReader/Configuration/RuntimeParsersDescriptorBuilder.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,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.Design; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Xml.Linq; | ||
|
||
namespace PocketCsvReader.Configuration; | ||
|
||
public class RuntimeParsersDescriptorBuilder | ||
{ | ||
private Dictionary<Type, ParseFunction> _types = new(); | ||
private RuntimeParsersDescriptor? Descriptor { get; set; } | ||
|
||
public RuntimeParsersDescriptorBuilder WithParser<T>(ParseFunction<T> parse) | ||
=> WithParser(typeof(T), (string str) => parse.Invoke(str)!); | ||
|
||
public RuntimeParsersDescriptorBuilder WithParser(Type type, ParseFunction parse) | ||
{ | ||
var returnType = parse.Method.ReturnType; | ||
if (!type.IsAssignableTo(returnType)) | ||
throw new ArgumentException($"The provided parser returns {returnType}, which is not assignable from {type}."); | ||
|
||
if (!_types.TryAdd(type, parse)) | ||
_types[type] = parse; | ||
return this; | ||
} | ||
|
||
public RuntimeParsersDescriptor? Build() | ||
{ | ||
Descriptor = new RuntimeParsersDescriptor(); | ||
foreach (var type in _types) | ||
Descriptor.AddParser(type.Key, type.Value); | ||
return Descriptor.Count > 0 ? Descriptor : null; | ||
} | ||
} |
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
Oops, something went wrong.