Comments (12)
Something like this #52 ?
from sheettoobjects.
Alrighty then! New version 2.4.0 is published to NuGet
from sheettoobjects.
Looks like a great addition. Not so sure what the added value of a stored mapper is in comparison to simply passing a func, can you elaborate?
from sheettoobjects.
I have been trying some different approaches but the issue is that I can not have a property
private Func<string, T> _valueMapper;
in the ColumnMappingBuilder.cs because the _valueMapper has to be passed on to different methods where T is unknown. So I am trying to use a property Func<in string, out object> where the out parameter has to be parsed back to the required type.
It would look like this:
var result = new SheetMapper()
.AddConfigFor<ModelThree>(cfg => cfg
.HasHeaders()
.MapColumn(column => column
.WithColumnIndex(0)
.WithDefaultValue(false)
.WithValueMapper(x => x == "1" ? true : false)
.MapTo(t => t.IsMedical))
.MapColumn(column => column
.WithColumnIndex(1)
.MapTo(t => t.Code))
.MapColumn(column => column
.WithColumnIndex(2)
.WithDefaultValue(ProductType.Unknown)
.WithValueMapper(x =>
{
switch(x.ToLower())
{
case "cheap": return ProductType.Cheap;
case "expensive": return ProductType.Expensive;
default: return ProductType.Unknown;
}
})
.MapTo(t => t.ProductType)))
.Map<ModelThree>(sheetData);
from sheettoobjects.
In the RowMapper something like this has to be done to parse back to required value (still WIP/POC though):
if(columnMapping.ValueMapper != null)
{
if (property.PropertyType == typeof(string))
{
string typedValue = (string)columnMapping.ValueMapper(value.ToString());
property.SetValue(obj, typedValue);
}
else
{
var typedValue = Activator.CreateInstance(property.PropertyType);
typedValue = Convert.ChangeType(columnMapping.ValueMapper(value.ToString()), value.GetType());
property.SetValue(obj, typedValue);
}
}
else
{
property.SetValue(obj, value);
}
from sheettoobjects.
I think you might have to indeed. Instead of writing your own parser to parse it back, you might be able to reuse the ParsingStrategyProvider
?
from sheettoobjects.
I am not sure this will work but I am going to have a look at it. ParsingStrategyProvider expects a string while I already have an object of parsed type. Parsing it back to string and than to an object again does not solve my issue. Anyway, I will try and find a working solution first (it will be after the holidays btw).
from sheettoobjects.
I did a quick scan and it looks good but I am currently unable to do a full review (on a vacation without laptop). I hope to review it on Friday. One thing: the naming of some parameters are confusing: parsingrules, customparser, rules.
I would suggest using customValueParser for the custom parser.
Nice work though, looks like we came up with the same solution.
from sheettoobjects.
I have reviewed the PR, looks good! I will try and implement the new feature on Monday. When satisfied I will close this issue too.
from sheettoobjects.
When using a custom value parser it is required to add a default value:
.MapColumn(column =>
column.WithHeader("IngredientRatioKgPerTon")
.ParseValueUsing(x => !string.IsNullOrEmpty(x) ? double.Parse(x) : 0)
.WithDefaultValue(0)
.MapTo(m => m.IngredientRatioKgPerTon))
I'm not sure why the default value is needed when using the ParseValueUsing() but not when I do not use any parser. This will just work fine:
.MapColumn(column => column.WithHeader("IngredientCode").MapTo(m => m.IngredientCode))
Any reason for this behaviour?
from sheettoobjects.
If you map to a Non Nullable value type, you have to specify the default value (otherwise what should the mapper map to?). Maybe that's the validation you hit?
This is the check:
if (propertyType.IsNotNullable()
&& propertyType.IsValueType
&& !hasRequiredRule
&& _defaultValue.IsNull())
{
throw new MappingConfigurationException(
$"Non-nullable property '{propertyInfo.Name}' is not required and therefor needs a default value.");
}
from sheettoobjects.
Makes sense. Closing this issue, nice feature!
from sheettoobjects.
Related Issues (20)
- Multiple configurations by Type
- Add nested objects support HOT 1
- Add comments to MappingConfigBuilder and underlying methods for better Intellisense experience
- Add base64 encoded string as input for CSV and Excel adapter
- Create Excel adapter
- Configuring default values for non-nullables
- Facilitating custom validation rules
- Regex which allows empty strings fails while it shouldn't HOT 1
- Documentation for the adapters HOT 1
- Make internals in Core and Lib public HOT 3
- Protected Sheets as separate nuget package HOT 2
- Stop parsing when cell x has value y HOT 3
- Add as dotnet package
- Make Mapping Sheet extension for nicer chaining HOT 1
- Easier registering of configurations HOT 1
- DateTime parsing with format
- Add min/max validation HOT 1
- Add 'IsUniqueInColumn' validation HOT 1
- Add 'IsUniqueRow' validation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sheettoobjects.