/** Helper to extract the types from the underlying model. */ private List<TypeRef> getTypes(Model model) { List<TypeRef> types = new ArrayList<>(); for (Type type : model.getServiceConfig().getTypesList()) { types.add(model.getSymbolTable().lookupType(type.getName())); } return types; }
/** * If rerouteToGrpcInterface is set, then looks up that interface and returns it, otherwise * returns the value of defaultInterface. */ public static Interface getTargetInterface( Interface defaultInterface, String rerouteToGrpcInterface) { Interface targetInterface = defaultInterface; if (!Strings.isNullOrEmpty(rerouteToGrpcInterface)) { targetInterface = defaultInterface.getModel().getSymbolTable().lookupInterface(rerouteToGrpcInterface); if (targetInterface == null) { throw new IllegalArgumentException( "reroute_to_grpc_interface not found: " + rerouteToGrpcInterface); } } return targetInterface; }
if (symbolTable.containsFieldName(m.group(0))) { m.appendReplacement(sb, CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, m.group(0))); } else if (symbolTable.lookupMethodSimpleName(m.group(0)) != null) { RestMethod restMethod = RestMethod.getPrimaryRestMethod(symbolTable.lookupMethodSimpleName(m.group(0)).get(0)); if (restMethod == null) { m.appendReplacement(sb, m.group(0));
/** * Resolves a interface by a partial name within a given package context, following PB (== C++) * conventions. */ @Nullable public Interface resolveInterface(String inPackage, String name) { for (String cand : nameCandidates(inPackage, name)) { Interface endpointInterface = lookupInterface(cand); if (endpointInterface != null) { return endpointInterface; } } return null; }
/** * 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; }
FluentIterable.from(getDeclaredTypes()) .filter(new Predicate<TypeRef>() { @Override TypeRef type = lookupType(typeNamePattern); if (type == null) { return ImmutableList.of();
TypeRef type = lookupType(id); if (type != null) { if (type.isMessage()) { Interface iface = lookupInterface(id); if (iface != null) { return iface; ProtoElement parent = resolve(id); if (parent != null) { if (parent instanceof Interface) {
firstComponent = name.substring(0, name.indexOf(".")); } else { return resolveType(inPackage, name); for (String cand : nameCandidates(inPackage, firstComponent)) { TypeRef outerType = lookupType(cand); if (outerType != null) { if (!outerType.isMessage()) { int lastDot = outerTypeName.lastIndexOf("."); String fullType = lastDot > 0 ? outerTypeName.substring(0, lastDot) + "." + name : name; return lookupType(fullType); } else if (packageNames.contains(cand)) { int lastDot = cand.lastIndexOf("."); String fullType = lastDot > 0 ? cand.substring(0, lastDot) + "." + name : name; return lookupType(fullType);
private boolean packageHasEnums(Model model) { for (TypeRef type : model.getSymbolTable().getDeclaredTypes()) { if (type.isEnum() && type.getEnumType().isReachable()) { return true; } } return false; }
/** * Return the list of API versions for all reachable API methods. */ public static List<String> getReachableRestVersions(Model model) { Set<String> versions = Sets.newLinkedHashSet(); for (Interface iface : model.getSymbolTable().getInterfaces()) { for (Method method : iface.getReachableMethods()) { if (method.hasAttribute(HttpAttribute.KEY)) { for (HttpAttribute binding : method.getAttribute(HttpAttribute.KEY).getAllBindings()) { versions.add(binding.getRestMethod().getVersionWithDefault()); } } } } List<String> versionsList = Lists.newArrayList(versions); Collections.sort(versionsList, Collections.reverseOrder(new VersionComparator())); return versionsList; } }
@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() } }
private Interface resolveInterface(String name, LocationContext location) { Interface iface = getModel().getSymbolTable().resolveInterface("", name); if (iface == null) { error(location, "The API '%s' cannot be resolved.", name); } return iface; } }
@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() }
@Test public void testContainsFieldName() { Assert.assertTrue(table.containsFieldName("foo")); Assert.assertTrue(table.containsFieldName("bar")); Assert.assertFalse(table.containsFieldName("baz")); }
@Test public void testContainsMethodName() { Assert.assertSame(rpc1, table.lookupMethodSimpleName("rpc1").get(0)); Assert.assertNull(table.lookupMethodSimpleName("rpc2")); }
/** * Get the type by its full name. */ @Nullable public TypeRef lookupType(String fullName) { return typeByName.get(getTypeNameInSymbolTable(fullName)); }
SymbolTable run() { visit(model); return new SymbolTable(interfaces, types, fieldNames, methods, packageNames); }
TypeRef type = lookupType(id); if (type != null) { if (type.isMessage()) { Interface iface = lookupInterface(id); if (iface != null) { return iface; ProtoElement parent = resolve(id); if (parent != null) { if (parent instanceof Interface) {
firstComponent = name.substring(0, name.indexOf(".")); } else { return resolveType(inPackage, name); for (String cand : nameCandidates(inPackage, firstComponent)) { TypeRef outerType = lookupType(cand); if (outerType != null) { if (!outerType.isMessage()) { int lastDot = outerTypeName.lastIndexOf("."); String fullType = lastDot > 0 ? outerTypeName.substring(0, lastDot) + "." + name : name; return lookupType(fullType); } else if (packageNames.contains(cand)) { int lastDot = cand.lastIndexOf("."); String fullType = lastDot > 0 ? cand.substring(0, lastDot) + "." + name : name; return lookupType(fullType);
FluentIterable.from(getDeclaredTypes()) .filter(new Predicate<TypeRef>() { @Override TypeRef type = lookupType(typeNamePattern); if (type == null) { return ImmutableList.of();