【发布时间】:2017-08-03 07:31:51
【问题描述】:
我找不到屏蔽 protobuf 结构中某些字段的方法。我确实阅读了有关 FieldMaskUtil 并尝试了一些示例,但它似乎做了相反的事情,即复制 FieldMask 中提到的与我想要的不同的字段。这是示例结构和相应的测试代码。
原型:
syntax = "proto3";
package model;
option java_package = "test.demo.services.protobuf.customer.model";
option java_outer_classname = "CustomerProto";
message Accounts {
repeated Account account = 1;
}
message Account {
int32 id = 1;
string number = 2;
int32 customer_id = 3;
}
message Customers {
repeated Customer customers = 1;
}
message Customer {
int32 id = 1;
string pesel = 2;
string name = 3;
CustomerType type = 4;
repeated Account accounts = 5;
enum CustomerType {
INDIVIDUAL = 0;
COMPANY = 1;
}
}
这里是示例测试代码
package test.demo.services.protobuf.customer.model;
import org.junit.Test;
import test.demo.services.protobuf.customer.model.CustomerProto.Customer;
import com.google.protobuf.util.FieldMaskUtil;
public class TestMerge {
@Test
public void eraseFields() {
Customer request = Customer.newBuilder().setId(10).setPesel("12345").setName("Harry Alto").build();
// Erase name
Customer.Builder modifieldRequest = Customer.newBuilder();
FieldMaskUtil.merge(FieldMaskUtil.fromString("name"), request, modifieldRequest);
System.out.println( modifieldRequest.build().toString());
}
}
这是输出:
name: "Harry Alto"
我所期望的是打印除名称以外的所有内容
id: 10
pesel: "12345"
有没有办法做我想做的事
【问题讨论】:
标签: java protocol-buffers protocol-buffers-3