-
Notifications
You must be signed in to change notification settings - Fork 8
Translating Map Properties
Map properties can be translated with explicit configuration or implicitly.
When a class is translated, Moo will search that class for properties using the default Access Mode (see Access Modes). Any properties found this way will be translated from the source, if they can be found. If those properties are instances of Map
, they will become collection properties.
You can configure the default access mode in the Moo Configuration
class, or you can change the access mode for a class using the @AccessMode
annotation. Alternately, you can configure properties of the object for explicit translation.
In addition to any implicitly-located properties, you might choose to explicitly configure the properties on a class. There are lots of reasons to do this and lots of ways to do this.
The most common would be to include the @MapProperty
annotation on a property, but the @Ignore
or @Optionality
attributes would also qualify.
You can mark properties as being ignored for translation with the @Ignore
annotation if they're properties that might be picked up implicitly and you don't want them to be picked up (see Ignoring Properties).
If you don't want to ignore the property, you want it to be translated when it's available but get no complaints when it is not, then you might want to look at the @Optionality
annotation or the optionality attribute on the Property annotation (see Optional and Required Properties).
You might mark a field or method with the @MapProperty
annotation if you just want to pick up a property that might not otherwise be found:
@Access(AccessMode.METHOD)
public MyClass {
@MapProperty
private Map<KeyClass,ValueClass> other;
}
Or you might need one of the many attributes on the @MapProperty annotation.
Some properties should be totally replaced with a new translation, others can be updated from the source object or from the translation. The update attribute allows you to control whether the object can be updated (update=true
) or should be replaced (update=false
).
If you want the map's keys or values to get translated, you can use the keyClass
and/or the valueClass
to specify the destination type. Otherwise, the key or value will be copied.
When updating a Map, if Moo can't find a key in the destination that it has in the source, it can either leave the map entry the way it is, or remove it. You can configure this behaviour with the removeOrphans
attribute.
In some cases, the source object for the key or value might not be a simple copy or a simple object translation. You might have a different name on the source, or you might need to translate only a single property of the source, or you might have a more complicated translation that requires the assistance of outside objects. The keySource
and valueSource
attributes can help you with all of these cases.
(see Source Expressions)
As above, sometimes an attribute might be optional. You can use the optionality attribute or the optionality annotation to let Moo know.
(see Optional and Required Properties)
Moo will create the destination object for you when a translation is required, but sometimes the destination object that Moo would create isn't the one you would have picked. In that case, you might need a TranslationTargetFactory
to help out.
(see Translation Target Factories)
Some implementations of Map
are fine with null keys, others are not. If you want Moo to potentially insert null keys if the source contains one or the translation results in one, then leave this with the default value of true (or mark it explicitly true). On the other hand, if you want null keys to be discarded, set nullKeys=false
.
- Five Minute Introduction
- Getting Moo
- Translating Object Properties
- Translating Collection Properties
- Translating Map Properties
- Source Expressions
- Access Modes
- Translating to Value Types
- Extensions
- External Objects in Translation
- Ignoring Properties
- Updating Collections
- Translation Target Factories
- Nested Updates
- Optional and Required Properties
- Order of Property Translation
- Constructors
- Lookup on Update
- Caching
Releases
Background