當前位置: 首頁>>代碼示例>>Java>>正文


Java Type類代碼示例

本文整理匯總了Java中com.google.protobuf.Type的典型用法代碼示例。如果您正苦於以下問題:Java Type類的具體用法?Java Type怎麽用?Java Type使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Type類屬於com.google.protobuf包,在下文中一共展示了Type類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: generateFile

import com.google.protobuf.Type; //導入依賴的package包/類
private FileDescriptorProto generateFile(String name, FileContents contents) {
  FileDescriptorProto.Builder fileBuilder = FileDescriptorProto.newBuilder();
  fileBuilder.setName(name);
  if (!Strings.isNullOrEmpty(contents.packageName)) {
    fileBuilder.setPackage(contents.packageName);
  }
  for (Api api : contents.apis) {
    fileBuilder.addService(generateApi(api));
  }
  for (Type type : contents.types.values()) {
    fileBuilder.addMessageType(generateType(type, contents));
  }
  for (Enum e : contents.enums) {
    fileBuilder.addEnumType(generateEnum(e));
  }
  if (imports.containsKey(name)) {
    for (String imported : imports.get(name)) {
      fileBuilder.addDependency(imported);
    }
  }
  return fileBuilder.build();
}
 
開發者ID:googleapis,項目名稱:api-compiler,代碼行數:23,代碼來源:DescriptorGenerator.java

示例2: addTypeFromFields

import com.google.protobuf.Type; //導入依賴的package包/類
/** Create the {@link Type} with given fields. */
private void addTypeFromFields(
    Service.Builder serviceBuilder,
    String typeFullName,
    Iterable<Field> fields,
    Iterable<Option> options) {
  Type.Builder coreTypeBuilder = Type.newBuilder().setName(typeFullName);
  coreTypeBuilder.getSourceContextBuilder().setFileName(namespace);
  coreTypeBuilder.addAllFields(fields);
  coreTypeBuilder.setSyntax(Syntax.SYNTAX_PROTO3);
  if (options != null) {
    coreTypeBuilder.addAllOptions(options);
  }
  createdTypesFullName.add(coreTypeBuilder.getName());
  Type coreType = coreTypeBuilder.build();
  if (!serviceBuilder.getTypesList().contains(coreType)) {

    serviceBuilder.addTypes(coreTypeBuilder.build());
  }
}
 
開發者ID:googleapis,項目名稱:api-compiler,代碼行數:21,代碼來源:TypeBuilder.java

示例3: testJsonIoException

import com.google.protobuf.Type; //導入依賴的package包/類
@Test
public void testJsonIoException() throws Exception {
  Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance());
  final IOException ioe = new IOException();
  try {
    marshaller.parse(new ByteArrayInputStream("{}".getBytes("UTF-8")) {
      @Override
      public void close() throws IOException {
        throw ioe;
      }
    });
    fail("Exception expected");
  } catch (StatusRuntimeException ex) {
    assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode());
    assertEquals(ioe, ex.getCause());
  }
}
 
開發者ID:grpc,項目名稱:grpc-java,代碼行數:18,代碼來源:ProtoUtilsTest.java

示例4: isDefaultPackedEncoding

import com.google.protobuf.Type; //導入依賴的package包/類
/** In proto3, repeated fields of scalar numeric types use packed encoding by default */
private boolean isDefaultPackedEncoding(Field field) {
  if (field.getSyntax() == Syntax.SYNTAX_PROTO3 && field.isRepeated()) {
    FieldDescriptorProto.Type fieldType = field.getProto().getType();
    if (fieldType != FieldDescriptorProto.Type.TYPE_GROUP
        && fieldType != FieldDescriptorProto.Type.TYPE_BYTES
        && fieldType != FieldDescriptorProto.Type.TYPE_STRING
        && fieldType != FieldDescriptorProto.Type.TYPE_MESSAGE) {
      return true;
    }
  }
  return false;
}
 
開發者ID:googleapis,項目名稱:api-compiler,代碼行數:14,代碼來源:DescriptorNormalizer.java

示例5: createAdditionalServiceTypes

import com.google.protobuf.Type; //導入依賴的package包/類
/**
 * Creates additional types (Value, Struct and ListValue) to be added to the Service config.
 * TODO (guptasu): Fix this hack. Find a better way to add the predefined types.
 * TODO (guptasu): Add them only when required and not in all cases.
 */

static Iterable<Type> createAdditionalServiceTypes() {
  Map<String, DescriptorProto> additionalMessages = Maps.newHashMap();
  additionalMessages.put(Struct.getDescriptor().getFullName(),
      Struct.getDescriptor().toProto());
  additionalMessages.put(Value.getDescriptor().getFullName(),
      Value.getDescriptor().toProto());
  additionalMessages.put(ListValue.getDescriptor().getFullName(),
      ListValue.getDescriptor().toProto());
  additionalMessages.put(Empty.getDescriptor().getFullName(),
      Empty.getDescriptor().toProto());
  additionalMessages.put(Int32Value.getDescriptor().getFullName(),
      Int32Value.getDescriptor().toProto());
  additionalMessages.put(DoubleValue.getDescriptor().getFullName(),
      DoubleValue.getDescriptor().toProto());
  additionalMessages.put(BoolValue.getDescriptor().getFullName(),
      BoolValue.getDescriptor().toProto());
  additionalMessages.put(StringValue.getDescriptor().getFullName(),
      StringValue.getDescriptor().toProto());

  for (Descriptor descriptor : Struct.getDescriptor().getNestedTypes()) {
    additionalMessages.put(descriptor.getFullName(), descriptor.toProto());
  }

  // TODO (guptasu): Remove this hard coding. Without this, creation of Model from Service throws.
  // Needs investigation.
  String fileName = "struct.proto";
  List<Type> additionalTypes = Lists.newArrayList();
  for (String typeName : additionalMessages.keySet()) {
    additionalTypes.add(TypesBuilderFromDescriptor.createType(typeName,
        additionalMessages.get(typeName), fileName));
  }
  return additionalTypes;
}
 
開發者ID:googleapis,項目名稱:api-compiler,代碼行數:40,代碼來源:TypesBuilderFromDescriptor.java

示例6: createType

import com.google.protobuf.Type; //導入依賴的package包/類
/**
 * TODO (guptasu): only needed to create hard coded Types (Struct, ListValue, and Value). Check
 * if this can be removed. Create the Protobuf.Type instance from descriptorProto.
 */
private static Type createType(String typeName, DescriptorProto descriptorProto,
    String fileName) {
  Type.Builder coreTypeBuilder = Type.newBuilder().setName(typeName);

  int count = 1;
  for (FieldDescriptorProto fieldProto : descriptorProto.getFieldList()) {
    Field.Kind fieldKind = Field.Kind.valueOf(fieldProto.getType().getNumber());
    Cardinality cardinality = Cardinality.CARDINALITY_OPTIONAL;
    if (fieldProto.getLabel() == Label.LABEL_REPEATED) {
      cardinality = Cardinality.CARDINALITY_REPEATED;
    }
    Field.Builder coreFieldBuilder = Field
        .newBuilder()
        .setName(fieldProto.getName())
        .setNumber(count++)
        .setKind(fieldKind)
        .setCardinality(cardinality);
    if (fieldKind == Kind.TYPE_MESSAGE || fieldKind == Kind.TYPE_ENUM) {
      String typeFullName =
          fieldProto.getTypeName().startsWith(".") ? fieldProto.getTypeName().substring(1)
              : fieldProto.getTypeName();
      coreFieldBuilder.setTypeUrl(TYPE_SERVICE_BASE_URL + typeFullName);
    }
    coreTypeBuilder.addFields(coreFieldBuilder.build());
  }
  coreTypeBuilder.setSourceContext(SourceContext.newBuilder().setFileName(fileName));
  coreTypeBuilder.setSyntax(Syntax.SYNTAX_PROTO3);
  return coreTypeBuilder.build();
}
 
開發者ID:googleapis,項目名稱:api-compiler,代碼行數:34,代碼來源:TypesBuilderFromDescriptor.java

示例7: testRoundtrip

import com.google.protobuf.Type; //導入依賴的package包/類
@Test
public void testRoundtrip() throws Exception {
  Marshaller<Type> marshaller = ProtoUtils.marshaller(Type.getDefaultInstance());
  InputStream is = marshaller.stream(proto);
  is = new ByteArrayInputStream(ByteStreams.toByteArray(is));
  assertEquals(proto, marshaller.parse(is));
}
 
開發者ID:grpc,項目名稱:grpc-java,代碼行數:8,代碼來源:ProtoUtilsTest.java

示例8: testJsonRoundtrip

import com.google.protobuf.Type; //導入依賴的package包/類
@Test
public void testJsonRoundtrip() throws Exception {
  Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance());
  InputStream is = marshaller.stream(proto);
  is = new ByteArrayInputStream(ByteStreams.toByteArray(is));
  assertEquals(proto, marshaller.parse(is));
}
 
開發者ID:grpc,項目名稱:grpc-java,代碼行數:8,代碼來源:ProtoUtilsTest.java

示例9: testJsonRepresentation

import com.google.protobuf.Type; //導入依賴的package包/類
@Test
public void testJsonRepresentation() throws Exception {
  Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance());
  InputStream is = marshaller.stream(proto);
  String s = new String(ByteStreams.toByteArray(is), "UTF-8");
  assertEquals("{\"name\":\"value\"}", s.replaceAll("\\s", ""));
}
 
開發者ID:grpc,項目名稱:grpc-java,代碼行數:8,代碼來源:ProtoUtilsTest.java

示例10: testJsonInvalid

import com.google.protobuf.Type; //導入依賴的package包/類
@Ignore("https://github.com/google/protobuf/issues/1470")
@Test
public void testJsonInvalid() throws Exception {
  Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance());
  try {
    marshaller.parse(new ByteArrayInputStream("{]".getBytes("UTF-8")));
    fail("Expected exception");
  } catch (StatusRuntimeException ex) {
    assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode());
    assertNotNull(ex.getCause());
  }
}
 
開發者ID:grpc,項目名稱:grpc-java,代碼行數:13,代碼來源:ProtoUtilsTest.java

示例11: testJsonInvalidProto

import com.google.protobuf.Type; //導入依賴的package包/類
@Test
public void testJsonInvalidProto() throws Exception {
  Marshaller<Type> marshaller = ProtoUtils.jsonMarshaller(Type.getDefaultInstance());
  try {
    marshaller.parse(new ByteArrayInputStream("{\"\":3}".getBytes("UTF-8")));
    fail("Expected exception");
  } catch (StatusRuntimeException ex) {
    assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode());
    assertNotNull(ex.getCause());
  }
}
 
開發者ID:grpc,項目名稱:grpc-java,代碼行數:12,代碼來源:ProtoUtilsTest.java

示例12: testInvalidatedMessage

import com.google.protobuf.Type; //導入依賴的package包/類
@Test
public void testInvalidatedMessage() throws Exception {
  InputStream is = marshaller.stream(proto);
  // Invalidates message, and drains all bytes
  byte[] unused = ByteStreams.toByteArray(is);
  try {
    ((ProtoInputStream) is).message();
    fail("Expected exception");
  } catch (IllegalStateException ex) {
    // expected
  }
  // Zero bytes is the default message
  assertEquals(Type.getDefaultInstance(), marshaller.parse(is));
}
 
開發者ID:grpc,項目名稱:grpc-java,代碼行數:15,代碼來源:ProtoLiteUtilsTest.java

示例13: marshallerShouldNotLimitProtoSize

import com.google.protobuf.Type; //導入依賴的package包/類
@Test
public void marshallerShouldNotLimitProtoSize() throws Exception {
  // The default limit is 64MB. Using a larger proto to verify that the limit is not enforced.
  byte[] bigName = new byte[70 * 1024 * 1024];
  Arrays.fill(bigName, (byte) 32);

  proto = Type.newBuilder().setNameBytes(ByteString.copyFrom(bigName)).build();

  // Just perform a round trip to verify that it works.
  testRoundtrip();
}
 
開發者ID:grpc,項目名稱:grpc-java,代碼行數:12,代碼來源:ProtoLiteUtilsTest.java

示例14: metadataMarshaller_invalid

import com.google.protobuf.Type; //導入依賴的package包/類
@Test
public void metadataMarshaller_invalid() {
  Metadata.BinaryMarshaller<Type> metadataMarshaller =
      ProtoLiteUtils.metadataMarshaller(Type.getDefaultInstance());
  try {
    metadataMarshaller.parseBytes(new byte[] {-127});
    fail("Expected exception");
  } catch (IllegalArgumentException ex) {
    assertNotNull(((InvalidProtocolBufferException) ex.getCause()).getUnfinishedMessage());
  }
}
 
開發者ID:grpc,項目名稱:grpc-java,代碼行數:12,代碼來源:ProtoLiteUtilsTest.java

示例15: parseFromKnowLengthInputStream

import com.google.protobuf.Type; //導入依賴的package包/類
@Test
public void parseFromKnowLengthInputStream() throws Exception {
  Marshaller<Type> marshaller = ProtoLiteUtils.marshaller(Type.getDefaultInstance());
  Type expect = Type.newBuilder().setName("expected name").build();

  Type result = marshaller.parse(new CustomKnownLengthInputStream(expect.toByteArray()));
  assertEquals(expect, result);
}
 
開發者ID:grpc,項目名稱:grpc-java,代碼行數:9,代碼來源:ProtoLiteUtilsTest.java


注:本文中的com.google.protobuf.Type類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。