-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Yaml support #10
Yaml support #10
Changes from 3 commits
88bd480
2dc27a6
c1e346e
9dfb2e4
d86a10c
fc27e2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
http: | ||
rules: | ||
- selector: TestService.TestMethod4 | ||
get: /yaml_users/{s}/{uint3}/{nt.f1} | ||
- selector: TestService.TestMethod5 | ||
get: /yaml_users/{s=hello/**}/x/{uint3}/{nt.f1}/*/**/test | ||
- selector: TestService.TestMethod6 | ||
post: /yaml_users/ | ||
body: "*" | ||
additionalBindings: | ||
- post: /yaml_users_nested | ||
body: "nt" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
import com.fullcontact.rpc.jersey.util.ProtobufDescriptorJavaUtil; | ||
|
||
import com.fullcontact.rpc.jersey.yaml.YamlHttpConfig; | ||
import com.fullcontact.rpc.jersey.yaml.YamlHttpRule; | ||
import com.github.mustachejava.DefaultMustacheFactory; | ||
import com.github.mustachejava.Mustache; | ||
import com.github.mustachejava.MustacheFactory; | ||
|
@@ -17,20 +19,13 @@ | |
import com.google.common.collect.Iterables; | ||
import com.google.common.collect.Lists; | ||
import com.google.common.collect.Sets; | ||
import com.google.protobuf.DescriptorProtos; | ||
import com.google.protobuf.Descriptors; | ||
import com.google.protobuf.*; | ||
import com.google.protobuf.compiler.PluginProtos; | ||
import lombok.Builder; | ||
import lombok.Value; | ||
|
||
import java.io.StringWriter; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.io.*; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
|
@@ -56,6 +51,9 @@ public PluginProtos.CodeGeneratorResponse generate(PluginProtos.CodeGeneratorReq | |
AnnotationsProto.getDescriptor(), | ||
HttpRule.getDescriptor().getFile() | ||
); | ||
|
||
Optional<YamlHttpConfig> yamlConfig = YamlHttpConfig.getFromOptions(options); | ||
|
||
for(DescriptorProtos.FileDescriptorProto fdProto : request.getProtoFileList()) { | ||
// Descriptors are provided in dependency-topological order | ||
// each time we collect a new FileDescriptor, we add it to a | ||
|
@@ -85,6 +83,16 @@ public PluginProtos.CodeGeneratorResponse generate(PluginProtos.CodeGeneratorReq | |
for(Descriptors.ServiceDescriptor serviceDescriptor : fd.getServices()) { | ||
DescriptorProtos.ServiceDescriptorProto serviceDescriptorProto = serviceDescriptor.toProto(); | ||
for(DescriptorProtos.MethodDescriptorProto methodProto : serviceDescriptorProto.getMethodList()) { | ||
String fullMethodName = serviceDescriptor.getFullName() +"." + methodProto.getName(); | ||
if(yamlConfig.isPresent()){ //Check to see if the rules are defined in the YAML | ||
for(YamlHttpRule rule :yamlConfig.get().getRules()){ | ||
if(rule.getSelector().equals(fullMethodName) || rule.getSelector().equals("*")){ //TODO: com.foo.* | ||
//YAML http rules override proto files. - https://cloud.google.com/endpoints/docs/grpc-service-config | ||
DescriptorProtos.MethodOptions yamlOptions = DescriptorProtos.MethodOptions.newBuilder().setExtension(AnnotationsProto.http, rule.buildHttpRule()).build(); | ||
methodProto = DescriptorProtos.MethodDescriptorProto.newBuilder().mergeFrom(methodProto).setOptions(yamlOptions).build(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This lines are really really long, I'd like to keep it at ~120 chars. |
||
} | ||
} | ||
} | ||
if(methodProto.getOptions().hasExtension(AnnotationsProto.http)) { | ||
// TODO(xorlev): support server streaming | ||
if(methodProto.getServerStreaming() || methodProto.getClientStreaming()) | ||
|
@@ -94,7 +102,6 @@ public PluginProtos.CodeGeneratorResponse generate(PluginProtos.CodeGeneratorReq | |
} | ||
} | ||
} | ||
|
||
if(!methodsToGenerate.isEmpty()) | ||
generateResource(response, lookup, fdProto, methodsToGenerate, isProxy); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.fullcontact.rpc.jersey.yaml; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
/** | ||
* Created by kylehansen @Sypticus on 12/28/16. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets make this javadoc relevant -- a good candidate would be at least a link to the doc page mentioning the YAML format. |
||
*/ | ||
public class YamlHttpConfig { | ||
public Map<String, List<YamlHttpRule>> http; | ||
public List<YamlHttpRule> getRules(){ | ||
return http.get("rules"); | ||
} | ||
|
||
public static Optional<YamlHttpConfig> getFromOptions(Set<String> options){ | ||
Optional<String> yamlOption = options.stream().filter(option -> option.startsWith("yaml=")).findFirst(); | ||
if(yamlOption.isPresent()) { | ||
String yamlPath = yamlOption.get().split("=")[1]; | ||
try { | ||
File yamlFile = new File(yamlPath); | ||
if(!yamlFile.exists()){ | ||
throw new RuntimeException("YAMLs file does not exist: "+ yamlFile.getAbsolutePath()); | ||
} | ||
InputStream yamlStream = new FileInputStream(yamlFile); | ||
|
||
ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); | ||
return Optional.of(mapper.readValue(yamlStream, YamlHttpConfig.class)); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to parse YAML", e); | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.fullcontact.rpc.jersey.yaml; | ||
|
||
import com.google.api.HttpRule; | ||
import lombok.Data; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
/** | ||
* Created by kylehansen @Sypticus on 12/28/16. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto on Javadoc, just a little bit about why it exists. |
||
@Data | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be |
||
public class YamlHttpRule { | ||
private String selector; | ||
private String get; | ||
private String post; | ||
private String put; | ||
private String delete; | ||
private String body; | ||
private List<YamlHttpRule> additionalBindings; | ||
|
||
public HttpRule buildHttpRule(){ | ||
HttpRule.Builder builder = HttpRule.newBuilder(); | ||
if(get != null){ | ||
builder.setGet(get); | ||
} | ||
if(put != null){ | ||
builder.setPut(put); | ||
} | ||
if(delete != null){ | ||
builder.setDelete(delete); | ||
} | ||
if(post != null){ | ||
builder.setPost(post); | ||
} | ||
if(body != null){ | ||
builder.setBody(body); | ||
} | ||
if(additionalBindings != null){ | ||
builder.addAllAdditionalBindings(additionalBindings.stream().map(YamlHttpRule::buildHttpRule).collect(Collectors.toList())); | ||
} | ||
|
||
return builder.build(); | ||
|
||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,17 @@ service TestService { | |
rpc TestMethod3 (TestRequest) returns (TestResponse) { | ||
option (google.api.http).get = "/users/{s=hello/**}/x/{uint3}/{nt.f1}/*/**/test"; | ||
} | ||
rpc TestMethod4 (TestRequest) returns (TestResponse) { | ||
//Defined in Yaml | ||
} | ||
rpc TestMethod5 (TestRequest) returns (TestResponse) { | ||
//Defined in Yaml | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comments are inconsistent spacing. |
||
} | ||
|
||
rpc TestMethod6 (TestRequest) returns (TestResponse) { | ||
//Defined in Yaml | ||
} | ||
|
||
} | ||
enum TestEnum { | ||
FIRST = 0; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space after
:
-- I also noticed that most lines are lacking a space between)
and{
. I don't have a problem with it in spirit, but I would like to keep the style consistent.