boonproject / boon

Simple opinionated Java for the novice to expert level Java Programmer. Low Ceremony. High Productivity.
http://richardhightower.github.io/site/Boon/Welcome.html
Apache License 2.0
520 stars 102 forks source link

Thrown an exception if a value is not within an Enum #346

Open saidatta opened 8 years ago

saidatta commented 8 years ago

If there is an Enum

enum capital { Sacaremento, Austin }

and you are eventually calling the fromMap in MapperComplex.java. whille it is doing the conversion for that attribute (say, attribute(is of enum type Capital)). If it cannot find within that Capital enum, then can you detect it by triggering an exception?

Example, EnumValueNotFoundException(attribute:capital, message:"Entered XYZ is not a valid capital.")

RichardHightower commented 8 years ago

Is this a question or a bug report? If a bug report, what is the proposed expected behavior and the behavior seen?

saidatta commented 8 years ago

My apologies on being ambiguous.

It is a bug report.

Observed behavior(Context): Expanding on the above example

Lets say you have a domainObject(Places) and the enum(Capital) class Places{ ... Capital capital ... }

enum Capital {.. Sacaremento, Austin .. }

When I send the below Map HashMap fieldsMap = { .. "capital" : "XYZ" ..} to convert it to a domainObject of type Places

The returned object will have the attribute's( capital) value as null (converted from HashMap's XYZ)

Bug Scenario Places places = MapObjectConversion.fromMap(fieldsMap, Places.class, {})

After executing the above call, I will receive the Object Places with the attribute as null with no indication that it couldn't find the value XYZ.

Bug Reason: This becomes a problem when the mapping is utilized for validation purposes. As it is silently substituting that attribute's value with null instead of demonstrating it found an invalid value.

Follow up question Is it possible for the entire mapping process to complete and throw a list of exceptions that it has found(if any). Currently, stops the mapping process and throws the first exception?

Thanks, Let me know if you need any further details.

RichardHightower commented 8 years ago

Ok so you have two things here. A bug. I agree that if we can't parse it to an ENUM that we should throw an exception, and the other is a feature request. You will want to file a second issue for the feature request. I can't say when I will work on this. It gets done in spurts. But it should be an easy fix. I accept pull requests.

saidatta commented 8 years ago

Sounds good. Ill create a pull request after I work on it. Thanks Richard.