/** Returns the type or null if it doesn't exist. */ public Type get(ProtoType protoType) { return protoTypeNames.get(protoType.toString()); }
/** * Returns true if {@code type} should be excluded, even if it is a transitive dependency of a * root. In that case, the referring member is also excluded. */ public boolean excludes(ProtoType type) { return exclude(type.toString()); }
/** * Returns the service with the fully qualified name {@code name}, or null if this schema defines * no such service. */ public Service getService(ProtoType protoType) { return getService(protoType.toString()); }
/** * Returns the type with the fully qualified name {@code name}, or null if this schema defines no * such type. */ public Type getType(ProtoType protoType) { return getType(protoType.toString()); }
/** Returns true if {@code type} is a root. */ public boolean includes(ProtoType type) { return includes(type.toString()); }
private static ImmutableMap<String, Service> buildServicesIndex(Iterable<ProtoFile> protoFiles) { ImmutableMap.Builder<String, Service> result = ImmutableMap.builder(); for (ProtoFile protoFile : protoFiles) { for (Service service : protoFile.services()) { result.put(service.type().toString(), service); } } return result.build(); }
/** Returns the config for {@code type}, or null if it is not configured. */ private @Nullable TypeConfigElement typeConfig(ProtoType type) { for (ProfileFileElement element : profileFiles) { for (TypeConfigElement typeConfig : element.typeConfigs()) { if (typeConfig.type().equals(type.toString())) return typeConfig; } } return null; } }
private static void index(Map<String, Type> typesByName, Type type) { typesByName.put(type.type().toString(), type); for (Type nested : type.nestedTypes()) { index(typesByName, nested); } }
/** Returns the field named {@code field} on the message type of {@code self}. */ Field dereference(Field self, String field) { if (field.startsWith("[") && field.endsWith("]")) { field = field.substring(1, field.length() - 1); } Type type = protoTypeNames.get(self.type().toString()); if (type instanceof MessageType) { MessageType messageType = (MessageType) type; Field messageField = messageType.field(field); if (messageField != null) return messageField; Map<String, Field> typeExtensions = messageType.extensionFieldsMap(); Field extensionField = resolve(field, typeExtensions); if (extensionField != null) return extensionField; } return null; // Unable to traverse this field path. }
private void register(Type type) { protoTypeNames.put(type.type().toString(), type); for (Type nestedType : type.nestedTypes()) { register(nestedType); } }
private String resolveContext() { for (int i = contextStack.size() - 1; i >= 0; i--) { Object context = contextStack.get(i); if (context instanceof Type) { return ((Type) context).type().toString(); } else if (context instanceof ProtoFile) { String packageName = ((ProtoFile) context).packageName(); return packageName != null ? packageName : ""; } else if (context instanceof Field && ((Field) context).isExtension()) { String packageName = ((Field) context).packageName(); return packageName != null ? packageName : ""; } } throw new IllegalStateException(); }
private String adapterString(ProtoType type) { if (type.isScalar()) { return ProtoAdapter.class.getName() + '#' + type.toString().toUpperCase(Locale.US); } AdapterConstant adapterConstant = profile.getAdapter(type); if (adapterConstant != null) { return reflectionName(adapterConstant.className) + "#" + adapterConstant.memberName; } return reflectionName((ClassName) typeName(type)) + "#ADAPTER"; }
@Test public void messageToString() throws Exception { ProtoType person = ProtoType.get("squareup.protos.person.Person"); assertThat(person.toString()).isEqualTo("squareup.protos.person.Person"); ProtoType phoneType = person.nestedType("PhoneType"); assertThat(phoneType.toString()).isEqualTo("squareup.protos.person.Person.PhoneType"); }
private ProtoType resolveType(String name, boolean messageOnly) { ProtoType type = ProtoType.get(name); if (type.isScalar()) { if (messageOnly) { addError("expected a message but was %s", name); } return type; } if (type.isMap()) { if (messageOnly) { addError("expected a message but was %s", name); } ProtoType keyType = resolveType(type.keyType().toString(), false); ProtoType valueType = resolveType(type.valueType().toString(), false); return new ProtoType(keyType, valueType, name); } Type resolved = resolve(name, protoTypeNames); if (resolved == null) { addError("unable to resolve %s", name); return ProtoType.BYTES; // Just return any placeholder. } if (messageOnly && !(resolved instanceof MessageType)) { addError("expected a message but was %s", name); return ProtoType.BYTES; // Just return any placeholder. } return resolved.type(); }
/** * Returns the service with the fully qualified name {@code name}, or null if this schema defines * no such service. */ public Service getService(ProtoType protoType) { return getService(protoType.toString()); }
/** * Returns the type with the fully qualified name {@code name}, or null if this schema defines no * such type. */ public Type getType(ProtoType protoType) { return getType(protoType.toString()); }
public Map<String, Schema> buildSchemas() { Map<String, Schema> schemas = new HashMap(); ProtoType protoType = protoTypeQueue.poll(); while (Objects.nonNull(protoType)) { Schema schema = buildSchema(protoType); schemas.put(protoType.toString(), schema); protoType = protoTypeQueue.poll(); } return schemas; }
private String adapterString(ProtoType type) { return type.isScalar() ? ProtoAdapter.class.getName() + '#' + type.toString().toUpperCase(Locale.US) : reflectionName((ClassName) typeName(type)) + "#ADAPTER"; }
private void register(Type type) { protoTypeNames.put(type.type().toString(), type); for (Type nestedType : type.nestedTypes()) { register(nestedType); } }