private void serviceToString(Service service, StringBuilder sb) { appendLine(sb, "service %s {", service.getName()); for (ServiceMethod serviceMethod : service.getMethods()) { commentsToString(serviceMethod.getCommentLines(), sb, 2); appendLine(sb, " rpc %s (%s) returns (%s);\n", serviceMethod.getName(), serviceMethod.getArgTypeName(), serviceMethod.getReturnTypeName()); } if (!service.getMethods().isEmpty()) { sb.setLength(sb.length() - 1); } appendLine(sb, "}"); }
private void process(Module module, Service service) { ImmutableServiceDescriptor descriptor = ImmutableServiceDescriptor.builder() .type(NodeType.SERVICE) .name(service.getName()) .canonicalName(service.getCanonicalName()) .description(markdownProcessor.toHtml(service.getComments())) .options(service.getOptions().toMap()) .addAllMethods(service.getMethods().stream() .map(method -> ImmutableServiceMethod.builder() .name(method.getName()) .argTypeId(method.getArgType().getCanonicalName()) .argStream(method.isArgStream()) .returnTypeId(method.getReturnType().getCanonicalName()) .returnStream(method.isReturnStream()) .description(markdownProcessor.toHtml(method.getComments())) .options(method.getOptions().toMap()) .build()) .collect(Collectors.toList())) .build(); String output = "data/type/" + service.getCanonicalName() + ".json"; write(module, output, descriptor); }
private void resolveTypeReferences(ProtoContext context) { Proto proto = context.getProto(); Deque<String> scopeLookupList = createScopeLookupList(proto); for (Service service : proto.getServices()) { for (ServiceMethod method : service.getMethods()) { String argTypeName = method.getArgTypeName(); FieldType argType = resolveFieldType(method, context, scopeLookupList, argTypeName); if (!(argType instanceof Message)) { String format = "Cannot use '%s' as a service method argument type: not a message"; throw new ParserException(method, format, argType.getName()); } method.setArgType((Message) argType); String returnTypeName = method.getReturnTypeName(); FieldType returnType = resolveFieldType(method, context, scopeLookupList, returnTypeName); if (!(returnType instanceof Message)) { String format = "Cannot use '%s' as a service method return type: not a message"; throw new ParserException(method, format, returnType.getName()); } method.setReturnType((Message) returnType); } } resolveTypeReferences(context, scopeLookupList, proto); }
private void checkDuplicateServiceMethodNames(List<ServiceMethod> methods) { Map<String, ServiceMethod> methodsByName = new HashMap<>(); for (ServiceMethod method : methods) { String name = method.getName(); if (methodsByName.containsKey(name)) { throw new ParserException(method, "Duplicate service method name: '%s'", name); } methodsByName.put(name, method); } }
/** * Build usage index for given collection of proto files. */ public static UsageIndex build(Collection<Proto> protos) { UsageIndex usageIndex = new UsageIndex(); for (Proto proto : protos) { ProtoWalker.newInstance(proto.getContext()) .onMessage(message -> { for (Field field : message.getFields()) { usageIndex.register(field.getType(), message); } }) .onService(service -> { for (ServiceMethod serviceMethod : service.getMethods()) { usageIndex.register(serviceMethod.getArgType(), service); usageIndex.register(serviceMethod.getReturnType(), service); } }) .walk(); } return usageIndex; }
/** * Create new instance of custom extension provider. */ public CustomExtensionProvider() { super(); registerProperty(ServiceMethod.class, "javaReturnTypeFullName", serviceMethod -> { Message returnType = serviceMethod.getReturnType(); return UserTypeUtil.getCanonicalName(returnType); }); }
@Override public void enterRpcMethod(ProtoParser.RpcMethodContext ctx) { Service parent = context.peek(Service.class); ServiceMethod method = new ServiceMethod(parent); context.push(method); }
private void process(Module module, Service service) { ImmutableServiceDescriptor descriptor = ImmutableServiceDescriptor.builder() .type(NodeType.SERVICE) .name(service.getName()) .canonicalName(service.getCanonicalName()) .description(markdownProcessor.toHtml(service.getComments())) .options(service.getOptions().toMap()) .addAllMethods(service.getMethods().stream() .map(method -> ImmutableServiceMethod.builder() .name(method.getName()) .argTypeId(method.getArgType().getCanonicalName()) .argStream(method.isArgStream()) .returnTypeId(method.getReturnType().getCanonicalName()) .returnStream(method.isReturnStream()) .description(markdownProcessor.toHtml(method.getComments())) .options(method.getOptions().toMap()) .build()) .collect(Collectors.toList())) .build(); String output = "data/type/" + service.getCanonicalName() + ".json"; write(module, output, descriptor); }
private void resolveTypeReferences(ProtoContext context) { Proto proto = context.getProto(); Deque<String> scopeLookupList = createScopeLookupList(proto); for (Service service : proto.getServices()) { for (ServiceMethod method : service.getMethods()) { String argTypeName = method.getArgTypeName(); FieldType argType = resolveFieldType(method, context, scopeLookupList, argTypeName); if (!(argType instanceof Message)) { String format = "Cannot use '%s' as a service method argument type: not a message"; throw new ParserException(method, format, argType.getName()); } method.setArgType((Message) argType); String returnTypeName = method.getReturnTypeName(); FieldType returnType = resolveFieldType(method, context, scopeLookupList, returnTypeName); if (!(returnType instanceof Message)) { String format = "Cannot use '%s' as a service method return type: not a message"; throw new ParserException(method, format, returnType.getName()); } method.setReturnType((Message) returnType); } } resolveTypeReferences(context, scopeLookupList, proto); }
/** * Get a service method by it's name. */ public ServiceMethod getMethod(String name) { for (ServiceMethod serviceMethod : getMethods()) { if (serviceMethod.getName().equals(name)) { return serviceMethod; } } return null; }
/** * Build usage index for given collection of proto files. */ public static UsageIndex build(Collection<Proto> protos) { UsageIndex usageIndex = new UsageIndex(); for (Proto proto : protos) { ProtoWalker.newInstance(proto.getContext()) .onMessage(message -> { for (Field field : message.getFields()) { usageIndex.register(field.getType(), message); } }) .onService(service -> { for (ServiceMethod serviceMethod : service.getMethods()) { usageIndex.register(serviceMethod.getArgType(), service); usageIndex.register(serviceMethod.getReturnType(), service); } }) .walk(); } return usageIndex; }
@Override public void enterRpcMethod(ProtoParser.RpcMethodContext ctx) { Service parent = context.peek(Service.class); ServiceMethod method = new ServiceMethod(parent); context.push(method); }
private void serviceToString(Service service, StringBuilder sb) { appendLine(sb, "service %s {", service.getName()); for (ServiceMethod serviceMethod : service.getMethods()) { if (!serviceMethod.getCommentLines().isEmpty()) { appendLine(sb, " //" + serviceMethod.getComments()); } appendLine(sb, " rpc %s (%s) returns (%s);\n", serviceMethod.getName(), serviceMethod.getArgTypeName(), serviceMethod.getReturnTypeName()); } if (!service.getMethods().isEmpty()) { sb.setLength(sb.length() - 1); } appendLine(sb, "}"); }
/** * Get a service method by it's name. */ public ServiceMethod getMethod(String name) { for (ServiceMethod serviceMethod : getMethods()) { if (serviceMethod.getName().equals(name)) { return serviceMethod; } } return null; }
private void checkDuplicateServiceMethodNames(List<ServiceMethod> methods) { Map<String, ServiceMethod> methodsByName = new HashMap<>(); for (ServiceMethod method : methods) { String name = method.getName(); if (methodsByName.containsKey(name)) { throw new ParserException(method, "Duplicate service method name: '%s'", name); } methodsByName.put(name, method); } }
/** * Returns java method name for corresponding rpc method. */ public static String getMethodName(ServiceMethod serviceMethod) { String name = serviceMethod.getName(); String formattedName = Formatter.toCamelCase(name); if (isReservedKeyword(formattedName)) { return formattedName + '_'; } return formattedName; }
/** * Returns java method name for corresponding rpc method. */ public static String getMethodName(ServiceMethod serviceMethod) { String name = serviceMethod.getName(); String formattedName = Formatter.toCamelCase(name); if (isReservedKeyword(formattedName)) { return formattedName + '_'; } return formattedName; }