Giter VIP home page Giter VIP logo

Comments (12)

josdeweger-zz avatar josdeweger-zz commented on June 9, 2024 1

Something like this #52 ?

from sheettoobjects.

josdeweger-zz avatar josdeweger-zz commented on June 9, 2024 1

Alrighty then! New version 2.4.0 is published to NuGet

from sheettoobjects.

josdeweger-zz avatar josdeweger-zz commented on June 9, 2024

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.

jacobduijzer avatar jacobduijzer commented on June 9, 2024

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.

jacobduijzer avatar jacobduijzer commented on June 9, 2024

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.

josdeweger-zz avatar josdeweger-zz commented on June 9, 2024

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.

jacobduijzer avatar jacobduijzer commented on June 9, 2024

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.

jacobduijzer avatar jacobduijzer commented on June 9, 2024

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.

jacobduijzer avatar jacobduijzer commented on June 9, 2024

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.

jacobduijzer avatar jacobduijzer commented on June 9, 2024

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.

josdeweger-zz avatar josdeweger-zz commented on June 9, 2024

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.

jacobduijzer avatar jacobduijzer commented on June 9, 2024

Makes sense. Closing this issue, nice feature!

from sheettoobjects.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.