Skip to content

Support meta annotations for @ValueMapping #3037

@heipl-paul

Description

@heipl-paul

Use case

I write a bunch of mappers for gRPC which suggests UNSPECIFIED as a default value for integration with languages that offer the possibility of enum members that were not predefined, and auto-generates UNRECOGNIZED (at least in java, it does).

I always want to map both of these with

@ValueMapping(source = "UNSPECIFIED", target = MappingConstants.THROW_EXCEPTION)
@ValueMapping(source = "UNRECOGNIZED", target = MappingConstants.THROW_EXCEPTION)

Which, okay, isn't that big of a deal. But since I've been doing a lot of gRPC mappings, I've become accustomed to Mapping Composition. And instead of having a dozen mappings that are all ignoring various gRPC artifact fields, I extract them in their own specific annotation.

@Retention(RetentionPolicy.CLASS)
@Mapping(target = "mergeFrom", ignore = true)
@Mapping(target = "clearField", ignore = true)
@Mapping(target = "clearOneof", ignore = true)
@Mapping(target = "unknownFields", ignore = true)
@Mapping(target = "mergeUnknownFields", ignore = true)
@Mapping(target = "allFields", ignore = true)
public @interface IgnoreGrpcOnlyFields {
}

And then the following for a specific gRpc (proto) class:

@IgnoreGrpcOnlyFields
@Mapping(target = "cityBytes", ignore = true)
@Mapping(target = "provinceBytes", ignore = true)
@Mapping(target = "zipCodeBytes", ignore = true)
// etc ...
public @interface IgnoreAddressGrpcOnlyFields {
}

I assume and hope that adding Mapping Composition to @ValueMapping wouldn't represent a big technical challenge. And right now, the lack of this feature feels inconsistent.

Sample code

In the off case that I'm not clear enough, sample code of what I want to do:

@ValueMapping(source = "UNSPECIFIED", target = MappingConstants.THROW_EXCEPTION)
@ValueMapping(source = "UNRECOGNIZED", target = MappingConstants.THROW_EXCEPTION)
public @interface IgnoreGrpcEnumFields {
}
@IgnoreGrpcEnumFields
TargetEnum toEntity(SourceGrpcEnum source);

Generated Code

Self evident, it would not change generated code, this only relates to reading the annotation metadata.

Possible workarounds

Simply adding the individual annotations to each and every GRPC enum.

MapStruct Version

1.5.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions