Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Tweaking null propagation easily

Open brgrz opened this issue 1 year ago • 3 comments

Is there an easy way to change how null propagation works?

  1. If we wanted to throw an exception when null reference encountered? There are lots of various Ignore methods, similarly I wish there were Required methods or maybe an ThrowIfNull argument to be passed in
  2. If null encountered, use a provided value (I know atm it uses default or null value)

brgrz avatar Dec 11 '24 11:12 brgrz

Hello @brgrz
Do you want to prevent null leaking from Source to Destination?

From this case:

  1. If null encountered, use a provided value (I know atm it uses default or null value)

Maybe it will help you Mapping with condition

DocSvartz avatar Jan 05 '25 22:01 DocSvartz

@brgrz If you go out something like this?

I think it can be done )

TypeAdapterConfig<Source,Destination>
.NewConfig()
.Map(x=>x.Value, y=>y.Value).IfNullDefault(42)
.Map(x=>x.SomeValue, y=>y.SomeValue).IfNullAction(x=> GetThrow(x))

DocSvartz avatar Apr 11 '25 04:04 DocSvartz

If we wanted to throw an exception when null reference encountered? There are lots of various Ignore methods, similarly I wish there were Required methods or maybe an ThrowIfNull argument to be passed in

@brgrz Can you describe in more detail the conditions in which you want to use this:

  1. Should this only apply to reference types, or also to nullable ValueType?
  2. Should this work for types declared in Nullable Enable Context or only for other types?

DocSvartz avatar May 22 '25 03:05 DocSvartz