下面列出了com.google.protobuf.Descriptors.FieldDescriptor#Type ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void testFindFieldByNumber() throws Exception {
Descriptor descriptor = TypicalData.Builder.getDescriptor();
Collection<FieldDescriptor> fields = descriptor.getFields();
for (FieldDescriptor field : fields) {
FieldDescriptor.Type type = field.getType();
int fieldId = field.getNumber();
switch (fieldId) {
case 1:
assertEquals(Type.INT32, type);
break;
case 2:
assertEquals(Type.BYTES, type);
break;
case 3:
assertEquals(Type.ENUM, type);
break;
}
FieldDescriptor result = descriptor.findFieldByNumber(fieldId);
assertEquals(field.getNumber(), result.getNumber());
assertEquals(field.getName(), result.getName());
}
}
protected String literal(Object value, FieldDescriptor.Type type) {
switch (type) {
case STRING:
return quotedString((String)value);
default:
return value.toString();
}
}
public void testFieldTypeEnumMapping() throws Exception {
assertEquals(FieldDescriptor.Type.values().length,
FieldDescriptorProto.Type.values().length);
for (FieldDescriptor.Type type : FieldDescriptor.Type.values()) {
FieldDescriptorProto.Type protoType = type.toProto();
assertEquals("TYPE_" + type.name(), protoType.name());
assertEquals(type, FieldDescriptor.Type.valueOf(protoType));
}
}
/**
* Test that the FieldDescriptor.Type enum is the same as the
* WireFormat.FieldType enum.
*/
public void testFieldTypeTablesMatch() throws Exception {
FieldDescriptor.Type[] values1 = FieldDescriptor.Type.values();
WireFormat.FieldType[] values2 = WireFormat.FieldType.values();
assertEquals(values1.length, values2.length);
for (int i = 0; i < values1.length; i++) {
assertEquals(values1[i].toString(), values2[i].toString());
}
}
/**
* Returns the {@link Type} of the actual value of this field, which for map fields is the type of
* the map's value.
*/
FieldDescriptor.Type valueType() {
return valueField().descriptor().getType();
}