/** * Creates a reference to a primitive type based on its name in the protocol buffer language, * with default cardinality optional. */ @Nullable public static TypeRef fromPrimitiveName(String name) { Type kind = PRIMITIVE_TYPE_MAP.get(name); return kind == null ? null : of(kind); }
@Override public String getEnumTypeName(ImportTypeTable typeTable, EnumType enumType) { // JSTypeTable produces nicknames which are qualified with the a packagePrefix which needs // to be stripped from the enum type name. List<String> enumNames = Arrays.asList( ((ModelTypeTable) typeTable).getNicknameFor(TypeRef.of(enumType)).split("\\.")); return enumNames.get(enumNames.size() - 1); }
@Override public String getMessageTypeName(ImportTypeTable typeTable, MessageType message) { // JSTypeTable produces nicknames which are qualified with the a packagePrefix which needs // to be stripped from the message type name. List<String> messageNames = Arrays.asList( ((ModelTypeTable) typeTable).getNicknameFor(TypeRef.of(message)).split("\\.")); return messageNames.get(messageNames.size() - 1); }
/** * Creates a reference to a primitive type based on its name in the protocol buffer language, * with default cardinality optional. */ @Nullable public static TypeRef fromPrimitiveName(String name) { Type kind = PRIMITIVE_TYPE_MAP.get(name); return kind == null ? null : of(kind); }
/** Renders the given value if it is a primitive type. */ String renderValueAsString(String value) { return renderPrimitiveValue(TypeRef.of(Type.TYPE_STRING), value); }
public String getMessageTypeName(ImportTypeTable typeTable, MessageType message) { return ((ModelTypeTable) typeTable).getNicknameFor(TypeRef.of(message)); }
public String getEnumTypeName(ImportTypeTable typeTable, EnumType enumType) { return ((ModelTypeTable) typeTable).getNicknameFor(TypeRef.of(enumType)); }
/** * For each ProtoFile dependency, put its TypeAlias in allTypeTable; and if it is a local import, * then put it in the given localImportNames, otherwise if it is a shared import, but it in the * given sharedImportNames. */ private void populateTypeTable( Iterable<ProtoFile> protoFileDependencies, ModelTypeTable allTypeTable, Set<String> localImportNames, Set<String> sharedImportNames, Collection<ProtoFile> localImportFiles) { for (ProtoFile protoFile : protoFileDependencies) { // For python, adding a single message from the proto file to the type table will populate // the type table with the correct imports. ImmutableList<MessageType> messages = protoFile.getMessages(); if (!messages.isEmpty()) { TypeRef typeRef = TypeRef.of(messages.get(0)); allTypeTable.getAndSaveNicknameFor(typeRef); if (localImportFiles .stream() .anyMatch(f -> f.getFullName().equals(protoFile.getFullName()))) { localImportNames.add(allTypeTable.getFullNameFor(typeRef)); } else { sharedImportNames.add(allTypeTable.getFullNameFor(typeRef)); } } } }
/** * Resolves a type based on the given partial name. This does not assume that the name, as * obtained from the descriptor, is in absolute form. */ private TypeRef resolveType(Location location, FieldDescriptorProto.Type kind, String name) { TypeRef type; switch (kind) { case TYPE_MESSAGE: case TYPE_ENUM: case TYPE_GROUP: type = symbolTable.resolveType(namespaces.peek(), name); break; default: type = TypeRef.of(kind); } if (type == null) { model.getDiagReporter().report(Diag.error(location, "Unresolved type '%s'", name)); } return type; } }
/** * Resolves a type based on the given partial name. This does not assume that the name, as * obtained from the descriptor, is in absolute form. */ private TypeRef resolveType(Location location, FieldDescriptorProto.Type kind, String name) { TypeRef type; switch (kind) { case TYPE_MESSAGE: case TYPE_ENUM: case TYPE_GROUP: type = symbolTable.resolveType(namespaces.peek(), name); break; default: type = TypeRef.of(kind); } if (type == null) { model.getDiagReporter().report(Diag.error(location, "Unresolved type '%s'", name)); } return type; } }
@VisitsBefore void visit(EnumValue value) { // The type is build from the parent, which must be an enum type. value.setType(TypeRef.of((EnumType) value.getParent())); findOptionTypes(value.getProto().getOptions().getAllFields()); }
@VisitsBefore void visit(EnumValue value) { // The type is build from the parent, which must be an enum type. value.setType(TypeRef.of((EnumType) value.getParent())); findOptionTypes(value.getProto().getOptions().getAllFields()); }
private void checkResponseObject(Method method, MethodKind kind) { if (kind != MethodKind.NONE) { WellKnownType wkt = TypeRef.of(method.getOutputMessage()).getWellKnownType(); if (!wkt.allowedAsHttpRequestResponse()) { error( ResolvedLocation.create(method.getLocation()), "type '%s' is not allowed as a response because it does not render as " + "a JSON object.", method.getOutputMessage().getFullName()); } } }
@VisitsBefore void visit(EnumType enumType) { // Add the enum type to the set of known types. addType(enumType.getLocation(), enumType.getFullName(), TypeRef.of(enumType)); // Build the field-by-name map for this enum type. Map<String, EnumValue> valueByName = Maps.newLinkedHashMap(); for (EnumValue value : enumType.getValues()) { EnumValue old = valueByName.put(value.getSimpleName(), value); if (old != null) { model .getDiagReporter() .report( Diag.error( value.getLocation(), "Duplicate declaration of enum value '%s'. Previous location: %s", value.getSimpleName(), old.getLocation().getDisplayString())); } } enumType.setValueByNameMap(ImmutableMap.copyOf(valueByName)); }
@VisitsBefore void visit(EnumType enumType) { // Add the enum type to the set of known types. addType(enumType.getLocation(), enumType.getFullName(), TypeRef.of(enumType)); // Build the field-by-name map for this enum type. Map<String, EnumValue> valueByName = Maps.newLinkedHashMap(); for (EnumValue value : enumType.getValues()) { EnumValue old = valueByName.put(value.getSimpleName(), value); if (old != null) { model .getDiagReporter() .report( Diag.error( value.getLocation(), "Duplicate declaration of enum value '%s'. Previous location: %s", value.getSimpleName(), old.getLocation().getDisplayString())); } } enumType.setValueByNameMap(ImmutableMap.copyOf(valueByName)); }
private void checkResponseObject(Method method, MethodKind kind) { if (kind != MethodKind.NONE) { WellKnownType wkt = TypeRef.of(method.getOutputMessage()).getWellKnownType(); if (!wkt.allowedAsHttpRequestResponse()) { error( ResolvedLocation.create(method.getLocation()), "type '%s' is not allowed as a response because it does not render as " + "a JSON object.", method.getOutputMessage().getFullName()); } } }
private List<GrpcElementDocView> generateEnumFileElements( ModelTypeTable typeTable, SurfaceNamer namer, ProtoContainerElement containerElement) { ImmutableList.Builder<GrpcElementDocView> elements = ImmutableList.builder(); elements.addAll( elementDocTransformer.generateEnumDocs(typeTable, namer, containerElement.getEnums())); for (MessageType message : containerElement.getMessages()) { List<GrpcElementDocView> elementDocs = generateEnumFileElements(typeTable, namer, message); if (!elementDocs.isEmpty()) { GrpcMessageDocView.Builder messageView = GrpcMessageDocView.newBuilder(); messageView.name(namer.publicClassName(Name.upperCamel(message.getSimpleName()))); messageView.fullName(typeTable.getFullNameFor(TypeRef.of(message))); messageView.fileUrl(GapicParser.getFileUrl(message.getFile())); messageView.lines(namer.getDocLines(GapicParser.getDocString(message))); messageView.properties(ImmutableList.<ParamDocView>of()); messageView.elementDocs(elementDocs); messageView.packageName(message.getFile().getFullName()); elements.add(messageView.build()); } } return elements.build(); }
@VisitsBefore void visit(MessageType message) { // Add the message to the set of known types. addType(message.getLocation(), message.getFullName(), TypeRef.of(message)); // Add the message's package to the set of known packages addPackage(message.getFile().getFullName()); // Build the field-by-name map for this message, and record field simple names. Map<String, Field> fieldByName = Maps.newLinkedHashMap(); for (Field field : message.getFields()) { fieldNames.add(field.getSimpleName()); Field old = fieldByName.put(field.getSimpleName(), field); if (old != null) { model .getDiagReporter() .report( Diag.error( field.getLocation(), "Duplicate declaration of field '%s'. Previous location: %s", field.getSimpleName(), old.getLocation().getDisplayString())); } } message.setFieldByNameMap(ImmutableMap.copyOf(fieldByName)); }
@Test public void testResolveType2() { Mockito.when(message1.getFullName()).thenReturn(m1Name); Mockito.when(message2.getFullName()).thenReturn(m2Name); Mockito.when(message3.getFullName()).thenReturn(m3Name); Mockito.when(message4.getFullName()).thenReturn(m4Name); Assert.assertSame(TypeRef.of(Type.TYPE_INT32), table.resolveType("a.b", "int32")); Assert.assertSame(m1, table.resolveType2("a.b", "m")); Assert.assertSame(m2, table.resolveType2("a.b", "m.m")); Assert.assertSame(m2, table.resolveType2("a.b.m", "m")); Assert.assertSame(m1, table.resolveType2("a.b.m", ".a.b.m")); Assert.assertSame(m4, table.resolveType2("a.b.a", "a.n")); Assert.assertNull(table.resolveType2("a.b.a", "a.m")); // Note: different from resolveType() } }
@Test public void testResolveType() { Assert.assertSame(TypeRef.of(Type.TYPE_INT32), table.resolveType("a.b", "int32")); Assert.assertSame(m1, table.resolveType("a.b", "m")); Assert.assertSame(m2, table.resolveType("a.b", "m.m")); Assert.assertSame(m2, table.resolveType("a.b.m", "m")); Assert.assertSame(m1, table.resolveType("a.b.m", ".a.b.m")); Assert.assertSame(m3, table.resolveType("a.b.a", "a.m")); // Note: different from resolveType() }