-
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
Merged
Merged
Yaml support #10
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
88bd480
Added support for httpRule definitions in YAML files
2dc27a6
code cleanup
c1e346e
Yaml path can now be passed in. Added tests around YAML functionality
9dfb2e4
removed hardcoded path
d86a10c
Cleaned up code and styling
fc27e2c
Cleaned up comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
protoc-gen-jersey/src/main/java/com/fullcontact/rpc/jersey/yaml/YamlHttpConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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; | ||
|
||
/** | ||
* | ||
* Allows HTTPRules to be generated from a .yml file instead of the .proto files. | ||
* Any rule defined in a .yml file will override rules in the .proto. | ||
* https://cloud.google.com/endpoints/docs/grpc-service-config | ||
* | ||
* Path to the .yml file should be passed in as part of the optional parameter | ||
* | ||
* @author Kyle Hansen (sypticus) | ||
*/ | ||
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(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
protoc-gen-jersey/src/main/java/com/fullcontact/rpc/jersey/yaml/YamlHttpRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.fullcontact.rpc.jersey.yaml; | ||
|
||
import com.google.api.HttpRule; | ||
import lombok.Value; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
/** | ||
* | ||
* HTTPRules defined in the .yml will be parsed into a YamlHTTPRule, from which a com.google.api.HttpRule can be generated. | ||
* | ||
* @author Kyle Hansen (sypticus) | ||
*/ | ||
|
||
@Value | ||
public class YamlHttpRule { | ||
String selector; | ||
String get; | ||
String post; | ||
String put; | ||
String delete; | ||
String body; | ||
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(); | ||
|
||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,16 @@ 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; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Ditto on Javadoc, just a little bit about why it exists.