Giter Site home page Giter Site logo

Comments (10)

petrnymsa avatar petrnymsa commented on May 30, 2024 1

Agree, that we can extend "custom" behavior on Field level in way that

  • If provided function has any argument - it has to be "SOURCE" object argument - e.g for custom mapping from source (current behavior
  • If provided function has zero arguments - call it as is. new behavior. This would allow OP's request.

from auto_mappr.

petrnymsa avatar petrnymsa commented on May 30, 2024 1

@becjit Can you try out this solution from #169 ?

In pubspec.yaml you can change auto_mappr dependency from

auto_mappr: ^x.xx

to

auto_mappr:
    git:
      url: https://github.com/netglade/auto_mappr
      path: packages/auto_mappr
      ref: feat/168-extends-custom-mapping

Let us know! Thanks 🚀

from auto_mappr.

tenhobi avatar tenhobi commented on May 30, 2024

Hi @becjit, could you please elaborate more?

In this case static methods without any Source model argument is required, but looks like converters are not allowed without the source model

So you would want a type converter that does not have a source? How would that work?


The correct/current behavior is like this and I think it makes sense, no? For you to map/convert, you always need the source to produce the target. How would that work otherwise -- matching which converter to use, for example?

@AutoMappr(
  [
    MapType<UserDto, User>(
      // MapType specific
      converters: [
        TypeConverter<String, DateTime>()
      ],
    ),
  ],
  converters: [
    // or globally here
  ],
)
class Mappr extends $Mappr {
  static DateTime stringToDateTime(String source) {
    // ...
  }
}

// https://github.com/netglade/auto_mappr#type-converters

from auto_mappr.

becjit avatar becjit commented on May 30, 2024

Sure thing!
Let's say in the target I have a field with DateTime, and source does not have any corresponding column.
So I really dont need the source model to generate value for the target; a simple DateTime.now() would do.

Now let's say I have multiple models with similar requirements. Currently I am copy pasting the same code with separate SourceModel as across mappers

For example

@AutoMappr([
  MapType<UserDto, User>(
    fields: [

      Field('created', custom: Utility.mapDateTime), // Global method.

    ],
  ),
])
class Mappr extends $Mappr {

}




and

@AutoMappr([
  MapType<OtherDto, Other>(
    fields: [

      Field('created', custom: Utility.mapDateTime), // Global method.

    ],
  ),
])
class OtherMappr extends $Mappr {

}

class Utility {
 static DateTime mapDateTime(/* not needed */String source) {
    return DateTime.now();
  }
}

instead of that I could just have one global method mapDateTime and refer that in the converter. Do let me know if I am missing something obvious.
And thank you for the getting back quickly

from auto_mappr.

tenhobi avatar tenhobi commented on May 30, 2024

I see. Well, since TypeConverter converts from source and target, that's not the right thing to do anyway.

We could however potentially extend the behavior of custom in Field, since even now (I think :D) you can use either a function TARGET something(SOURCE) or a TARGET constant. And I think we could distinguish between 0 and 1 argument there based on the function you pass in. 🤔

Aka then you could do this

@AutoMappr([
  MapType<OtherDto, Other>(
    fields: [
      Field('something', custom: Utility.mapStringToInt), // internally will use Utility.mapStringToInt(model)
      Field('created', custom: Utility.mapDateTime), // internally will use Utility.mapDateTime()
    ],
  ),
])
class OtherMappr extends $Mappr {
  const OtherMappr();
}

class Utility {
  static DateTime mapDateTime() {
    return DateTime.now();
  }
  static String mapIntToString(String value) {
    return '$value';
  }
}

Thoughts, @petrnymsa ?


That being said, this is potentially an easy upgrade, do you want to take this and do a PR for that? Otherwise we don't probably have time any time soon for this -- maybe in January or February.

What is needed to be done:

from auto_mappr.

petrnymsa avatar petrnymsa commented on May 30, 2024

I am cooking some ad-hoc solution right now 🔥

from auto_mappr.

becjit avatar becjit commented on May 30, 2024

from auto_mappr.

tenhobi avatar tenhobi commented on May 30, 2024

Hey @becjit, did you have time to validate whether this change helps your case? :)

from auto_mappr.

becjit avatar becjit commented on May 30, 2024

from auto_mappr.

petrnymsa avatar petrnymsa commented on May 30, 2024

Fix available in 2.3.0 🎉

from auto_mappr.

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.