private SoyType doGetType(String name, SoyTypeRegistry typeRegistry, GenericDescriptor descriptor) { SoyType type; synchronized (lock) { type = typeCache.get(name); if (type == null) { if (descriptor instanceof EnumDescriptor) { type = new SoyProtoEnumType((EnumDescriptor) descriptor); } else { type = new SoyProtoType(typeRegistry, (Descriptor) descriptor, extensions.get(name)); } typeCache.put(name, type); } } return type; }
final void walkDescriptor(GenericDescriptor descriptor) { if (!visited.add(descriptor.getFullName())) { // skip if we have already seen this descriptor return; } if (descriptor instanceof FileDescriptor) { walkFileDescriptor((FileDescriptor) descriptor); } else if (descriptor instanceof Descriptor) { walkMessageDescriptor((Descriptor) descriptor); } else if (descriptor instanceof FieldDescriptor) { FieldDescriptor fieldDescriptor = (FieldDescriptor) descriptor; if (fieldDescriptor.isExtension()) { visitExtensionDescriptor(fieldDescriptor); } else { visitFieldDescriptor(fieldDescriptor); } } else if (descriptor instanceof EnumDescriptor) { visitEnumDescriptor((EnumDescriptor) descriptor); } // services, etc. not needed thus far so neither gathered nor dispatched }
/** * Recursively walks the given {@link Message} class and verifies that every field or message * linked in uses the Protocol Buffers proto2 syntax. */ static void checkProto2Syntax(Class<? extends Message> clazz, ExtensionRegistry registry) { for (GenericDescriptor d : getRecursiveDescriptorsForClass(clazz, registry)) { Syntax s = d.getFile().getSyntax(); checkArgument( s == Syntax.PROTO2, "Message %s or one of its dependencies does not use proto2 syntax: %s in file %s", clazz.getName(), d.getFullName(), d.getFile().getName()); } }
/** Helper used to test the recursive class traversal and print good error messages. */ private static Set<String> getRecursiveDescriptorFullNames( Class<? extends Message> clazz, ExtensionRegistry registry) { Set<String> result = new HashSet<>(); for (GenericDescriptor d : getRecursiveDescriptorsForClass(clazz, registry)) { result.add(d.getFullName()); } return result; }
/** Registers a collection of descriptors of any type. */ public Builder addDescriptors(Iterable<? extends GenericDescriptor> descriptorsToAdd) { for (GenericDescriptor descriptorToAdd : descriptorsToAdd) { descriptors.add(descriptorToAdd); } return this; }
@Override public SoyType getType(String name, SoyTypeRegistry typeRegistry) { GenericDescriptor descriptor = descriptors.get(name); if (descriptor == null) { return null; } return doGetType(name, typeRegistry, descriptor); }
static String calculateQualifiedJsName(GenericDescriptor descriptor) { String protoPackage = descriptor.getFile().getPackage(); // We need a semi-qualified name: including containing types but not the package. String name = descriptor.getFullName(); if (!name.startsWith(protoPackage)) { throw new AssertionError("Expected \"" + name + "\" to start with \"" + protoPackage + "\""); } String jsPackage = getJsPackage(descriptor.getFile()); // When there is no protoPackage, the semi-qualified name does not have a package prefix nor the // "." separator. if (protoPackage.isEmpty()) { return jsPackage + "." + name; } return jsPackage + name.substring(protoPackage.length()); }
/** Returns the GraphQL name of the supplied proto. */ static String getReferenceName(GenericDescriptor descriptor) { return CharMatcher.anyOf(".").replaceFrom(descriptor.getFullName(), "_"); }
/** Returns a reference to the GraphQL type corresponding to the supplied proto. */ static GraphQLTypeReference getReference(GenericDescriptor descriptor) { return new GraphQLTypeReference(getReferenceName(descriptor)); }
/** Finds the GraphQL type for a Proto descriptor. */ public static ModifiableType find(GenericDescriptor typeDescriptor) { return new ModifiableType(ProtoToGql.getReferenceName(typeDescriptor)); }
static String getReferenceName(GenericDescriptor descriptor) { return "Input_" + ProtoToGql.getReferenceName(descriptor); }
/** Registers a collection of descriptors of any type. */ public Builder addDescriptors(GenericDescriptor... descriptorsToAdd) { Collections.addAll(descriptors, descriptorsToAdd); return this; }
final void walkDescriptors(Iterable<? extends GenericDescriptor> descriptors) { for (GenericDescriptor descriptor : descriptors) { walkDescriptor(descriptor); } }
/** * Registers a collection of protocol buffer descriptors. This makes all the types defined in * the provided descriptors available to use in soy. */ public Builder addProtoDescriptors(Iterable<? extends GenericDescriptor> descriptors) { protoTypeProviderBuilder.addDescriptors(descriptors); return this; }
/** * Registers a collection of protocol buffer descriptors. This makes all the types defined in * the provided descriptors available to use in soy. */ public Builder addProtoDescriptors(GenericDescriptor... descriptors) { return addProtoDescriptors(Arrays.asList(descriptors)); }