Comments (10)
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.
@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.
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.
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.
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:
- https://github.com/netglade/auto_mappr/blob/main/packages/auto_mappr/lib/src/extensions/dart_object_extension.dart#L19C52-L25 modify this (detect 0 vs 1 arguments)
- add some tests
- update docs
from auto_mappr.
I am cooking some ad-hoc solution right now 🔥
from auto_mappr.
from auto_mappr.
Hey @becjit, did you have time to validate whether this change helps your case? :)
from auto_mappr.
from auto_mappr.
Fix available in 2.3.0 🎉
from auto_mappr.
Related Issues (20)
- Support reverse flag in build.yaml config HOT 2
- Global converter not found when used with reverse flag HOT 2
- Nullable input/output with type converters HOT 6
- Strict mode HOT 6
- Build Options as Config object HOT 1
- Switch to DCM OSS
- Custom MapType HOT 5
- Generation fails when required TARGET field doesn't exist in SOURCE even with whenNull HOT 2
- Combining multiple SOURCEs into one single TARGET HOT 2
- Custom Mapping Futures HOT 3
- Async support HOT 2
- Anyone using auto_mappr in production? HOT 2
- Support field name mapping in reverse mode
- Field ignores whenNull and ignoreNull HOT 3
- Generated code for classes without ctors ignores 'from' argument HOT 7
- Move where from extension to static function
- Extend tryConvert method for catching mapping exceptions
- Enum set to null for unknown values HOT 1
- Possibility to ignore elements in a list when mapping goes wrong for one element HOT 2
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 auto_mappr.