-
Notifications
You must be signed in to change notification settings - Fork 46
Readme #33
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
Merged
Readme #33
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7c5698e
Usage section
iamdanfox a5d5d52
Better usage section
iamdanfox 928702d
Example generated objects
iamdanfox 1a45464
Link to Jersey, Retrofit files
iamdanfox a122380
Flesh out jersey and retrofit sections
iamdanfox 55d25d7
Generated objects builder pattern & jackson example
iamdanfox d322339
Mention jackson
iamdanfox 88b0c58
Mention union visitors
iamdanfox 200696b
Bintray shield
iamdanfox 1eefd93
License badge
iamdanfox b73c540
mention unknown values for enums
iamdanfox 3c6da99
describe intended usages of aliases
iamdanfox 63fab4a
Contributing document mentions Intellij and checkstyle
iamdanfox e9b50c3
CR
iamdanfox cc6c812
Update readme.md
iamdanfox 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 hidden or 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,19 @@ | ||
| # Contributing | ||
|
|
||
| The team welcomes contributions! To make changes: | ||
|
|
||
| - Fork the repo and make a branch | ||
| - Write your code (ideally with tests) and make sure the CircleCI build passes | ||
| - Open a PR (optionally linking to a github issue) | ||
|
|
||
| ## Local development | ||
|
|
||
| We recommend using [Intellij IDEA Community Edition](https://www.jetbrains.com/idea/) for Java projects. You'll need Java 8 on your machine. | ||
|
|
||
| 1. Fork the repository | ||
| 1. Generate the IDE configuration: `./gradlew idea` | ||
| 1. Import projects into Intellij: `open *.ipr` | ||
|
|
||
| Tips: | ||
|
|
||
| - run `./gradlew checkstyleMain checkstyleTest` locally to make sure your code conforms to the code-style. | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,109 @@ | ||
| Conjure-Java | ||
| ======= | ||
| The java implementation of Conjure generator for java. | ||
| # Conjure-Java  [](https://opensource.org/licenses/Apache-2.0) | ||
|
|
||
|
|
||
|
|
||
|
|
||
| _CLI to generate Java POJOs and interfaces from [Conjure API definitions](https://github.com/palantir/conjure)._ | ||
|
|
||
| ## Usage | ||
|
|
||
| The recommended way to use conjure-java is via a build tool like [gradle-conjure](https://github.com/palantir/gradle-conjure). However, if you don't want to use gradle-conjure, there is also an executable which conforms to [RFC 002](https://github.com/palantir/conjure/blob/develop/rfc/002-contract-for-conjure-generators.md), published on [bintray](https://bintray.com/palantir/releases/conjure-java). | ||
|
|
||
| Usage: conjure-java generate <input> <output> [...options] | ||
|
|
||
| --objects [boolean] Generate POJOs for Conjure type definitions | ||
| --jersey [boolean] Generate jax-rs annotated interfaces for client or server-usage | ||
| --retrofit [boolean] Generate retrofit interfaces for streaming/async clients | ||
|
|
||
| ## Example generated objects | ||
|
|
||
| Conjure-java objects are always immutable and thread-safe. Fields are never null; instead, Java 8 Optionals are used. JSON serialization is handled using [Jackson](https://github.com/FasterXML/jackson) annotations. | ||
|
|
||
| - **Conjure object: [ManyFieldExample](./conjure-java-core/src/integrationInput/java/com/palantir/product/ManyFieldExample.java)** | ||
|
|
||
| Objects can only be instantiated using the builder pattern, or by deserializing from JSON. | ||
|
|
||
| ```java | ||
| ManyFieldExample example = ManyFieldExample.builder() | ||
| .string("foo") | ||
| .integer(123) | ||
| .optionalItem("bar") | ||
| .addAllItems(iterable) | ||
| .build(); | ||
|
|
||
| // or using Jackson (via com.palantir.remoting3:jackson-support) | ||
| ObjectMapper mapper = ObjectMappers.newServerObjectMapper(); | ||
| ManyFieldExample fromJson = mapper.readValue("{\"string\":\"foo\", ...}", ManyFieldExample.class); | ||
| ``` | ||
|
|
||
| - **Conjure union: [UnionTypeExample](./conjure-java-core/src/integrationInput/java/com/palantir/product/UnionTypeExample.java)** | ||
|
|
||
| Union types can be one of a few variants. To interact with a union value, users should use the `.accept` method and define a Visitor that handles each of the possible variants, including the possibility of an unknown variant. | ||
|
|
||
| ```java | ||
| Foo output = unionTypeExample.accept(new Visitor<Foo>() { | ||
|
|
||
| public Foo visitStringExample(StringExample value) { | ||
| // your logic here! | ||
| } | ||
|
|
||
| public Foo visitSet(Set<String> value) {} | ||
|
|
||
| // ... | ||
|
|
||
| public Foo visitUnknown(String unknownType) {} | ||
|
|
||
| }); | ||
| ``` | ||
|
|
||
| Visitors may seem clunky in Java, but they have the upside of compile-time assurance that you've handled all the possible variants. If you upgrade an API dependency and the API author added a new variant, the Java compiler will force you to explicitly deal with this new variant. We intentionally avoid `switch` statements and `instanceof` checks for this exact reason. | ||
|
|
||
| - **Conjure enum: [EnumExample](./conjure-java-core/src/integrationInput/java/com/palantir/product/EnumExample.java)** | ||
|
|
||
| Enums are subtly different from regular Java enums because they tolerate deserializing unknown values. This is important because it ensures server authors can add new variants to an enum without causing runtime errors for consumers that use older API jars. | ||
|
|
||
| ```java | ||
| EnumExample one = EnumExample.ONE; | ||
| System.out.println(one); // prints: 'ONE' | ||
|
|
||
| EnumExample fromJson = mapper.readValue("\"XYZ\"", EnumExample.class); | ||
| System.out.println(fromJson); // prints: 'XYZ' | ||
| ``` | ||
|
|
||
| - **Conjure alias: [StringAliasExample](./conjure-java-core/src/integrationInput/java/com/palantir/product/StringAliasExample.java)** | ||
|
|
||
| Aliases have exactly the same JSON representation as their inner type, so they are useful for making error-prone function signatures more bulletproof: | ||
|
|
||
| ```diff | ||
| -doSomething(String, String, String); | ||
| +doSomething(ProductId, UserId, EmailAddress); | ||
| ``` | ||
|
|
||
| ## Example Jersey interfaces | ||
|
|
||
| Conjure-java generates Java interfaces with [JAX-RS](http://jax-rs-spec.java.net/) annotations, so they can be used on the client side or on the server-side. | ||
|
|
||
| - Example jersey interface: [EteService](./conjure-java-core/src/integrationInput/java/com/palantir/product/EteService.java) | ||
|
|
||
| For **client-side usage**, use [http-remoting](https://github.com/palantir/http-remoting#jaxrs-clients) which configures Feign with sensible defaults. | ||
|
|
||
| For **server-side usage**, you need a [Jersey](https://jersey.github.io/)-compatible server. We recommend Dropwizard (which is based on Jetty), but Grizzly, Tomcat, etc should also work fine! Use [http-remoting's jersey-servers](https://github.com/palantir/http-remoting#jersey-servers) to configure Jackson and Exception mappers appropriately. | ||
|
|
||
|
|
||
| ## Example Retrofit interfaces | ||
|
|
||
| As an alternative to the JAX-RS interfaces above, conjure-java can generate equivalent interfaces with [Retrofit2](http://square.github.io/retrofit/) annotations. These clients are useful if you want to stream binary data or make non-blocking async calls: | ||
|
|
||
| - Example retrofit2 interface: [EteServiceRetrofit](./conjure-java-core/src/integrationInput/java/com/palantir/product/EteServiceRetrofit.java) | ||
|
|
||
| ```java | ||
| @GET("./base/binary") | ||
| @Streaming | ||
| Call<ResponseBody> binary(@Header("Authorization") AuthHeader authHeader); | ||
| ``` | ||
|
|
||
| You can also supply the `--retrofitCompletableFutures` flag if you prefer Java 8 CompletableFutures instead of OkHttp's Call. | ||
|
Contributor
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. should this be included in the usage?
Contributor
Author
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. It messes up all the indenting and seemed a bit unimportant |
||
|
|
||
| ## Contributing | ||
|
|
||
| For instructions on how to set up your local development environment, check out the [Contributing document][./CONTRIBUTING.md]. | ||
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.
Extra space.