从平坦的Protobuf消息映射到使用Mapstruct的内部类的类

发布于 2025-01-29 07:46:42 字数 1781 浏览 1 评论 0原文

我需要使用mapsstruct将从消息addressProto的映射写入class地址。但是消息addressProto仅由字符串字段组成,而地址类具有内部类。

到目前为止,我已经写了这样的映射器,但是由于消息结构和类之间的差异,我不知道如何正确映射从消息addressproto class 地址地址和后背。

@Mapper(config = MapstructConfig.class, unmappedTargetPolicy = ReportingPolicy.ERROR)
public abstract class AddressProtoMapper {

  // from proto to object
  public abstract Address mapToAddress(AddressProto address);

  // from object to proto
  public abstract AddressProto mapAddressToProto(Address address);
}

proto Message addressProto(每个字段的斜线之后,我在哪个类字段中都需要映射):

message AddressProto {
    string value = 1;               // Address.AddressValue.value
    string unrestricted_value = 2;  // Address.AddressValue.unrestrictedValue
    string country = 3;             // Address.Structure.Country.name
    string country_iso_code = 4;    // Address.Structure.Country.isoCode
    string region = 5;              // Address.Structure.Region.name
}

Java class address> address

public class Address {
    public final AddressValue value;
    public final Structure structure;


    public static class AddressValue {
        public final String value;
        public final String unrestrictedValue;
    }


    public static class Structure {
        public final Country country;
        public final Region region;

        public static class Country {
            public final String name;
            public final String isoCode;
        }

        public static class Region {
            public final String name;
        }

    }
}

I need to use mapStruct to write a mapping from message AddressProto to the class Address. But message AddressProto consists only of string fields while Address class has inner classes.

I have written such a mapper so far, but due to the difference between the message structure and the class, I don’t know how to correctly map fields from message AddressProto to class Address and back.

@Mapper(config = MapstructConfig.class, unmappedTargetPolicy = ReportingPolicy.ERROR)
public abstract class AddressProtoMapper {

  // from proto to object
  public abstract Address mapToAddress(AddressProto address);

  // from object to proto
  public abstract AddressProto mapAddressToProto(Address address);
}

proto message AddressProto (after the slash for each field, I wrote in which class field it needs to be mapped):

message AddressProto {
    string value = 1;               // Address.AddressValue.value
    string unrestricted_value = 2;  // Address.AddressValue.unrestrictedValue
    string country = 3;             // Address.Structure.Country.name
    string country_iso_code = 4;    // Address.Structure.Country.isoCode
    string region = 5;              // Address.Structure.Region.name
}

java class Address:

public class Address {
    public final AddressValue value;
    public final Structure structure;


    public static class AddressValue {
        public final String value;
        public final String unrestrictedValue;
    }


    public static class Structure {
        public final Country country;
        public final Region region;

        public static class Country {
            public final String name;
            public final String isoCode;
        }

        public static class Region {
            public final String name;
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2025-02-05 07:46:42

解决方案非常简单。甚至太多:)
您只需要编写自己的实现即可。

@Mapper(
    config = MapstructConfig.class,
    unmappedTargetPolicy = ReportingPolicy.ERROR
)
public interface AddressProtoMapper {
  default Address mapToAddress(AddressProto address){
    return new Address(
        ...
    );
  }

  default AddressProto mapAddressToProto(Address address) {
    return new AddressProto(
        ...
    );
  }
}

The solution turned out to be very simple. Even too much :)
You just need to write your own implementation.

@Mapper(
    config = MapstructConfig.class,
    unmappedTargetPolicy = ReportingPolicy.ERROR
)
public interface AddressProtoMapper {
  default Address mapToAddress(AddressProto address){
    return new Address(
        ...
    );
  }

  default AddressProto mapAddressToProto(Address address) {
    return new AddressProto(
        ...
    );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文