@Override public boolean apply(TypeRef type) { if (type.getKind() == kind) { if (type.isMessage()) { return type.getMessageType().getFullName().startsWith( typeNamePattern.substring(0, typeNamePattern.length() - 1)); } else if (type.isEnum()) { return type.getEnumType().getFullName().startsWith( typeNamePattern.substring(0, typeNamePattern.length() - 1)); } return false; } return false; } })
/** * 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 boolean isString() { return protoField.getType().equals(TYPE_STRING); }
/** * Return true of this is a cyclic message type. */ public boolean isCyclic() { return getKind() == Type.TYPE_MESSAGE && getMessageType().isCyclic(); }
private static java.lang.String generateTypeUrl(TypeRef type) { java.lang.String name; if (type.isMessage()) { name = type.getMessageType().getFullName(); } else { name = type.getEnumType().getFullName(); } return DescriptorNormalization.TYPE_SERVICE_BASE_URL + "/" + name; }
@Override public TypedValue getSnippetZeroValue(TypeRef type) { if (type.isMap()) { TypeName keyTypeName = getTypeNameForElementType(type.getMapKeyField().getType()); TypeName valueTypeName = getTypeNameForElementType(type.getMapValueField().getType()); TypeName mapTypeName = typeNameConverter.getTypeName("System.Collections.Generic.Dictionary"); TypeName genericMapTypeName = valueTypeName); return TypedValue.create(genericMapTypeName, "new %s()"); } else if (type.isRepeated()) { TypeName elementTypeName = getTypeNameForElementType(type); TypeName listTypeName = typeNameConverter.getTypeName("System.Collections.Generic.List"); listTypeName.getFullName(), listTypeName.getNickname(), "%s<%i>", elementTypeName); return TypedValue.create(genericListTypeName, "new %s()"); } else if (type.isMessage()) { if (WRAPPER_TYPE_MAP.containsKey(type.getMessageType().getFullName())) { return TypedValue.create(getTypeName(type), "null"); } else if (type.isEnum()) { return getEnumValue(type, type.getEnumType().getValues().get(0)); } else { if (type.getKind() == Type.TYPE_BYTES) { String clsName = typeNameConverter.getAndSaveNicknameFor("Google.Protobuf.ByteString"); return TypedValue.create(getTypeName(type), clsName + ".Empty"); } else { return TypedValue.create(getTypeName(type), PRIMITIVE_ZERO_VALUE.get(type.getKind()));
@Visits void reach(Field field) { TypeRef type = field.getType(); if (type.isMap()) { visitInScope(type.getMessageType()); if (type.getMapValueField().getType().isMessage()) { // Be sure to generate error message only for the value type, not for the internal // key-value message. However, the later needs to be included, therefore we did // visitInScope above. mustBeInScope(field, type.getMapValueField().getType().getMessageType()); } } else if (type.isMessage()) { mustBeInScope(field, type.getMessageType()); } else if (type.isEnum()) { mustBeInScope(field, type.getEnumType()); } markAsReachable(field); }
@Override public TypedValue getSnippetZeroValue(TypeRef type) { // Don't call getTypeName; we don't need to import these. if (type.isMap()) { return TypedValue.create(new TypeName("dict"), "{}"); } if (type.isRepeated()) { return TypedValue.create(new TypeName("list"), "[]"); } if (PRIMITIVE_ZERO_VALUE.containsKey(type.getKind())) { return TypedValue.create(getTypeName(type), PRIMITIVE_ZERO_VALUE.get(type.getKind())); } if (type.isMessage()) { return TypedValue.create(getTypeName(type), "{}"); } if (type.isEnum()) { return getEnumValue(type, type.getEnumType().getValues().get(0)); } return TypedValue.create(new TypeName(""), "None"); }
@Override public TypeName getTypeNameForElementType(TypeRef type) { switch (type.getKind()) { case TYPE_MESSAGE: return getTypeName(type.getMessageType()); case TYPE_ENUM: return getTypeName(type.getEnumType()); default: String typeName = PRIMITIVE_TYPE_MAP.get(type.getKind()); return typeNameConverter.getTypeName(typeName); } }
@Override public TypeName getTypeName(TypeRef type) { if (type.isMap()) { TypeName keyTypeName = getTypeNameForElementType(type.getMapKeyField().getType()); TypeName valueTypeName = getTypeNameForElementType(type.getMapValueField().getType()); return new TypeName("", "", "map[%i]%i", keyTypeName, valueTypeName); } else if (type.isRepeated()) { TypeName elementTypeName = getTypeNameForElementType(type); return new TypeName("", "", "[]%i", elementTypeName); } else { return getTypeNameForElementType(type); } }
private static Set<ProtoFile> getFilesForMessage(MessageType messageType, boolean messageOnly) { Set<ProtoFile> files = newFileSet(); files.add(messageType.getFile()); if (messageOnly) { return files; } for (Field field : messageType.getFields()) { TypeRef type = field.getType(); if (type.isMessage()) { files.addAll(getFilesForMessage(type.getMessageType(), type.isCyclic())); } } return files; }
/** * Helper for getting the methods output message. */ public MessageType getOutputMessage() { return outputType.getMessageType(); }
private boolean packageHasEnums(Model model) { for (TypeRef type : model.getSymbolTable().getDeclaredTypes()) { if (type.isEnum() && type.getEnumType().isReachable()) { return true; } } return false; }
ProtoMethodModel methodModel = new ProtoMethodModel(createShelvesMethod); Field bookField = Mockito.mock(Field.class); Mockito.when(bookField.getType()).thenReturn(TypeRef.of(bookType)); Mockito.when(bookField.getParent()).thenReturn(createShelvesRequest); Mockito.when(bookField.getSimpleName()).thenReturn("book"); Mockito.when(nameField.getParent()).thenReturn(createShelvesRequest); Mockito.when(createShelvesRequest.getFullName()).thenReturn("library.CreateShelvesRequest"); Mockito.when(nameField.getType()).thenReturn(TypeRef.fromPrimitiveName("string")); Mockito.when(nameField.getSimpleName()).thenReturn("name"); Mockito.when(createShelvesRequest.lookupField("book")).thenReturn(bookField); .getType() .getProtoType() .getMessageType()) .isEqualTo(bookType); assertThat(((ProtoTypeRef) bookConfig.getField().getType()).getProtoType().getMessageType()) .isEqualTo(bookType);
public static boolean s_isEmptyType(TypeRef type) { return type.isMessage() && type.getMessageType().getFullName().equals(Empty.getDescriptor().getFullName()); } }
/** * Resolves a type by its partial name within a given package context, following PB (== C++) * conventions. If the given name is a builtin type name for a primitive type in the PB * language, a reference for that type will be returned. * * Note that this differs from the proto compiler in that it will continue searching if a * partial resolution fails; see resolveType2 for details. */ @Nullable public TypeRef resolveType(String inPackage, String name) { TypeRef type = TypeRef.fromPrimitiveName(name); if (type != null) { return type; } for (String cand : nameCandidates(inPackage, name)) { type = lookupType(cand); if (type != null) { return type; } } return null; }
/** Returns the enum value string */ public String getEnumValue(TypeRef type, String value) { for (EnumValue enumValue : type.getEnumType().getValues()) { if (enumValue.getSimpleName().equals(value)) { return typeNameConverter .getEnumValue(type, enumValue) .getValueAndSaveTypeNicknameIn(typeTable); } } throw new IllegalArgumentException("Unrecognized enum value: " + value); }
private String getExtraFieldDescription(FieldModel field) { boolean fieldIsMessage = field.isMessage() && !field.isMap(); boolean fieldIsEnum = field.isEnum(); if (fieldIsMessage) { return "This object should have the same structure as " + commentReformatter.getLinkedElementName( ((ProtoTypeRef) field.getType()).getProtoType().getMessageType()); } else if (fieldIsEnum) { return "The number should be among the values of " + commentReformatter.getLinkedElementName( ((ProtoTypeRef) field.getType()).getProtoType().getEnumType()); } return ""; }
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()); } } }