Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
Using ImmutableMap rather than map. Formatting code using google form…
Browse files Browse the repository at this point in the history
…atter. Adding copyright header.
  • Loading branch information
siderakis committed Nov 29, 2018
1 parent e322104 commit c6c3287
Showing 1 changed file with 68 additions and 38 deletions.
Original file line number Original file line Diff line number Diff line change
@@ -1,51 +1,76 @@
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.graphql.rejoiner; package com.google.api.graphql.rejoiner;


import com.google.common.collect.ImmutableMap;
import com.google.protobuf.DescriptorProtos; import com.google.protobuf.DescriptorProtos;


import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;


class DescriptorSet { final class DescriptorSet {
private static final String DEFAULT_DESCRIPTOR_SET_FILE_LOCATION = "META-INF/proto/descriptor_set.desc"; private static final String DEFAULT_DESCRIPTOR_SET_FILE_LOCATION =

"META-INF/proto/descriptor_set.desc";
static final Map<String, String> COMMENTS = getCommentsFromDescriptorFile();


private DescriptorSet() { static final ImmutableMap<String, String> COMMENTS = getCommentsFromDescriptorFile();
}


private static Map<String, String> getCommentsFromDescriptorFile() { private DescriptorSet() {}


private static ImmutableMap<String, String> getCommentsFromDescriptorFile() {
try { try {
InputStream is = DescriptorSet.class.getClassLoader().getResourceAsStream(DEFAULT_DESCRIPTOR_SET_FILE_LOCATION); InputStream is =
DescriptorProtos.FileDescriptorSet descriptors = DescriptorProtos.FileDescriptorSet.parseFrom(is); DescriptorSet.class
.getClassLoader()
.getResourceAsStream(DEFAULT_DESCRIPTOR_SET_FILE_LOCATION);
DescriptorProtos.FileDescriptorSet descriptors =
DescriptorProtos.FileDescriptorSet.parseFrom(is);
return descriptors return descriptors
.getFileList() .getFileList()
.stream() .stream()
.flatMap(fileDescriptorProto -> parseDescriptorFile(fileDescriptorProto) .flatMap(
.entrySet() fileDescriptorProto -> parseDescriptorFile(fileDescriptorProto).entrySet().stream())
.stream() .collect(
) ImmutableMap.toImmutableMap(
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (entry, value) -> entry)); Map.Entry::getKey, Map.Entry::getValue, (entry, value) -> entry));
} catch (IOException ignored) { } catch (IOException ignored) {
} }
return Collections.emptyMap(); return ImmutableMap.of();
} }


private static Map<String, String> parseDescriptorFile(DescriptorProtos.FileDescriptorProto descriptor) { private static Map<String, String> parseDescriptorFile(
DescriptorProtos.FileDescriptorProto descriptor) {
return descriptor return descriptor
.getSourceCodeInfo() .getSourceCodeInfo()
.getLocationList() .getLocationList()
.stream() .stream()
.filter(location -> !(location.getLeadingComments().isEmpty() && location.getTrailingComments().isEmpty())) .filter(
.map(location -> getFullName(descriptor, location.getPathList()) location ->
.map(fullName -> new AbstractMap.SimpleImmutableEntry<>(fullName, constructComment(location))) !(location.getLeadingComments().isEmpty()
) && location.getTrailingComments().isEmpty()))
.map(
location ->
getFullName(descriptor, location.getPathList())
.map(
fullName ->
new AbstractMap.SimpleImmutableEntry<>(
fullName, constructComment(location))))
.filter(Optional::isPresent) .filter(Optional::isPresent)
.map(Optional::get) .map(Optional::get)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Expand All @@ -54,23 +79,25 @@ private static Map<String, String> parseDescriptorFile(DescriptorProtos.FileDesc
private static String constructComment(DescriptorProtos.SourceCodeInfo.Location location) { private static String constructComment(DescriptorProtos.SourceCodeInfo.Location location) {
String trailingComment = location.getTrailingComments().trim(); String trailingComment = location.getTrailingComments().trim();
return (location.getLeadingComments().trim() return (location.getLeadingComments().trim()
+ (!trailingComment.isEmpty() ? (". " + trailingComment) : "") + (!trailingComment.isEmpty() ? (". " + trailingComment) : ""))
).replaceAll("^[*\\\\/.]*\\s*", ""); .replaceAll("^[*\\\\/.]*\\s*", "");
} }


/** /**
* Iterate through a component's path inside a protobufer descriptor. * Iterate through a component's path inside a proto descriptor.
* The path is a tuple of component type and a relative position
* For example:
* [4, 1, 3, 2, 2, 1] or [MESSAGE_TYPE_FIELD_NUMBER, 1, NESTED_TYPE_FIELD_NUMBER, 2, FIELD_FIELD_NUMBER, 1]
* is representing the second field of the third nested message in the second message in the file
* @see DescriptorProtos.SourceCodeInfoOrBuilder for more info
* *
* <p>
* The path is a tuple of component type and a relative position For example: [4, 1, 3, 2, 2, 1] or
* [MESSAGE_TYPE_FIELD_NUMBER, 1, NESTED_TYPE_FIELD_NUMBER, 2, FIELD_FIELD_NUMBER, 1] is
* representing the second field of the third nested message in the second message in the file
*
* @see DescriptorProtos.SourceCodeInfoOrBuilder for more info
* @param descriptor proto file descriptor * @param descriptor proto file descriptor
* @param path path of the element * @param path path of the element
* @return full element's path as a string * @return full element's path as a string
*/ */
private static Optional<String> getFullName(DescriptorProtos.FileDescriptorProto descriptor, List<Integer> path) { private static Optional<String> getFullName(
DescriptorProtos.FileDescriptorProto descriptor, List<Integer> path) {
String fullName = descriptor.getPackage(); String fullName = descriptor.getPackage();
switch (path.get(0)) { switch (path.get(0)) {
case DescriptorProtos.FileDescriptorProto.ENUM_TYPE_FIELD_NUMBER: case DescriptorProtos.FileDescriptorProto.ENUM_TYPE_FIELD_NUMBER:
Expand All @@ -80,7 +107,8 @@ private static Optional<String> getFullName(DescriptorProtos.FileDescriptorProto
DescriptorProtos.DescriptorProto message = descriptor.getMessageType(path.get(1)); DescriptorProtos.DescriptorProto message = descriptor.getMessageType(path.get(1));
return appendMessage(message, path, fullName); return appendMessage(message, path, fullName);
case DescriptorProtos.FileDescriptorProto.SERVICE_FIELD_NUMBER: case DescriptorProtos.FileDescriptorProto.SERVICE_FIELD_NUMBER:
DescriptorProtos.ServiceDescriptorProto serviceDescriptor = descriptor.getService(path.get(1)); DescriptorProtos.ServiceDescriptorProto serviceDescriptor =
descriptor.getService(path.get(1));
fullName += appendNameComponent(serviceDescriptor.getName()); fullName += appendNameComponent(serviceDescriptor.getName());
if (path.size() > 2) { if (path.size() > 2) {
fullName += appendNameComponent(serviceDescriptor.getMethod(path.get(3)).getName()); fullName += appendNameComponent(serviceDescriptor.getMethod(path.get(3)).getName());
Expand All @@ -95,13 +123,16 @@ private static Optional<String> append(
DescriptorProtos.DescriptorProto messageDescriptor, List<Integer> path, String fullName) { DescriptorProtos.DescriptorProto messageDescriptor, List<Integer> path, String fullName) {
switch (path.get(0)) { switch (path.get(0)) {
case DescriptorProtos.DescriptorProto.NESTED_TYPE_FIELD_NUMBER: case DescriptorProtos.DescriptorProto.NESTED_TYPE_FIELD_NUMBER:
DescriptorProtos.DescriptorProto nestedMessage = messageDescriptor.getNestedType(path.get(1)); DescriptorProtos.DescriptorProto nestedMessage =
messageDescriptor.getNestedType(path.get(1));
return appendMessage(nestedMessage, path, fullName); return appendMessage(nestedMessage, path, fullName);
case DescriptorProtos.DescriptorProto.ENUM_TYPE_FIELD_NUMBER: case DescriptorProtos.DescriptorProto.ENUM_TYPE_FIELD_NUMBER:
DescriptorProtos.EnumDescriptorProto enumDescriptor = messageDescriptor.getEnumType(path.get(1)); DescriptorProtos.EnumDescriptorProto enumDescriptor =
messageDescriptor.getEnumType(path.get(1));
return Optional.of(appendEnum(enumDescriptor, path, fullName)); return Optional.of(appendEnum(enumDescriptor, path, fullName));
case DescriptorProtos.DescriptorProto.FIELD_FIELD_NUMBER: case DescriptorProtos.DescriptorProto.FIELD_FIELD_NUMBER:
DescriptorProtos.FieldDescriptorProto fieldDescriptor = messageDescriptor.getField(path.get(1)); DescriptorProtos.FieldDescriptorProto fieldDescriptor =
messageDescriptor.getField(path.get(1));
return Optional.of(fullName + appendNameComponent(fieldDescriptor.getName())); return Optional.of(fullName + appendNameComponent(fieldDescriptor.getName()));
default: default:
return Optional.empty(); return Optional.empty();
Expand All @@ -111,8 +142,8 @@ private static Optional<String> append(
private static Optional<String> appendMessage( private static Optional<String> appendMessage(
DescriptorProtos.DescriptorProto message, List<Integer> path, String fullName) { DescriptorProtos.DescriptorProto message, List<Integer> path, String fullName) {
fullName += appendNameComponent(message.getName()); fullName += appendNameComponent(message.getName());
return path.size() > 2 ? return path.size() > 2
append(message, path.subList(2, path.size()), fullName) ? append(message, path.subList(2, path.size()), fullName)
: Optional.of(fullName); : Optional.of(fullName);
} }


Expand All @@ -128,5 +159,4 @@ private static String appendEnum(
private static String appendNameComponent(String component) { private static String appendNameComponent(String component) {
return "." + component; return "." + component;
} }

} }

0 comments on commit c6c3287

Please sign in to comment.