Skip to content

Commit

Permalink
Merge pull request #73 from eed3si9n/wip/optional2
Browse files Browse the repository at this point in the history
Default to use java.util.Optional for Java
  • Loading branch information
eed3si9n authored Jan 25, 2017
2 parents 214e596 + 5138aba commit 078cb0a
Show file tree
Hide file tree
Showing 25 changed files with 132 additions and 356 deletions.
2 changes: 1 addition & 1 deletion .java-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.6
1.8
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: scala
jdk: openjdk6
jdk: oraclejdk8
scala: 2.10.6
script: sbt test scripted

Expand Down
4 changes: 2 additions & 2 deletions docs/02-code-generation.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ This schema will produce the following Scala class:

```scala
/**
* This code is generated using sbt-datatype.
* This code is generated using [[http://www.scala-sbt.org/contraband/ sbt-contraband]].
*/

// DO NOT EDIT MANUALLY
Expand Down Expand Up @@ -79,7 +79,7 @@ Here's the Java code it generates (after changing the target annotation to `Java

```java
/**
* This code is generated using sbt-datatype.
* This code is generated using [[http://www.scala-sbt.org/contraband/ sbt-contraband]].
*/

// DO NOT EDIT MANUALLY
Expand Down
4 changes: 2 additions & 2 deletions docs/ja/02-code-generation.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Person {

```scala
/**
* This code is generated using sbt-datatype.
* This code is generated using [[http://www.scala-sbt.org/contraband/ sbt-contraband]].
*/

// DO NOT EDIT MANUALLY
Expand Down Expand Up @@ -79,7 +79,7 @@ Java のコード生成は以下のようになっている (target アノテー

```java
/**
* This code is generated using sbt-datatype.
* This code is generated using [[http://www.scala-sbt.org/contraband/ sbt-contraband]].
*/

// DO NOT EDIT MANUALLY
Expand Down
9 changes: 8 additions & 1 deletion library/src/main/scala/sbt/contraband/CodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ abstract class CodeGenerator {

protected def generateHeader: String =
"""/**
| * This code is generated using sbt-datatype.
| * This code is generated using [[http://www.scala-sbt.org/contraband/ sbt-contraband]].
| */
|
|// DO NOT EDIT MANUALLY
Expand All @@ -218,4 +218,11 @@ abstract class CodeGenerator {

object CodeGen {
def bq(id: String): String = if (ScalaKeywords.values(id)) s"`$id`" else id

val javaOptional: String = "java.util.Optional"
val instantiateJavaOptional: (String, String) => (String) =
(tpe: String, e: String) => {
if (e == "null") s"java.util.Optional.<$tpe>empty()"
else s"java.util.Optional.<$tpe>ofNullable($e)"
}
}
2 changes: 1 addition & 1 deletion library/src/main/scala/sbt/contraband/ScalaCodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class ScalaCodeGen(javaLazy: String, javaOptional: String, instantiateJavaOption
if (intfLang == "Scala") s"Option($e)"
else {
val x = instantiateJavaOptional(javaLangBoxedType(tpe.name), e)
// com.example.Maybe.<Integer>just(number)
// java.util.Optional.<Integer>ofNullable(number)
val JavaGenericMethod = """(.+)<([^>]+)>(\w+)\((.*)\)""".r
x match {
case JavaGenericMethod(pre, typearg, mtd, arg) => s"$pre$mtd[$typearg]($arg)"
Expand Down
8 changes: 4 additions & 4 deletions library/src/test/scala/GraphQLCodecCodeGenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class GraphQLCodecCodeGenSpec extends FlatSpec with Matchers with Inside with Eq

code(new File("generated", "InterfaceExampleFormats.scala")).unindent should equalLines (
"""/**
| * This code is generated using sbt-datatype.
| * This code is generated using [[http://www.scala-sbt.org/contraband/ sbt-contraband]].
| */
|
|// DO NOT EDIT MANUALLY
Expand All @@ -24,7 +24,7 @@ class GraphQLCodecCodeGenSpec extends FlatSpec with Matchers with Inside with Eq
|}""".stripMargin.unindent)
code(new File("generated", "ChildTypeFormats.scala")).unindent should equalLines (
"""/**
| * This code is generated using sbt-datatype.
| * This code is generated using [[http://www.scala-sbt.org/contraband/ sbt-contraband]].
| */
|
|// DO NOT EDIT MANUALLY
Expand Down Expand Up @@ -60,7 +60,7 @@ class GraphQLCodecCodeGenSpec extends FlatSpec with Matchers with Inside with Eq

code(new File("generated", "InterfaceExampleFormats.scala")).unindent should equalLines (
"""/**
| * This code is generated using sbt-datatype.
| * This code is generated using [[http://www.scala-sbt.org/contraband/ sbt-contraband]].
| */
|
|// DO NOT EDIT MANUALLY
Expand All @@ -72,7 +72,7 @@ class GraphQLCodecCodeGenSpec extends FlatSpec with Matchers with Inside with Eq
code.contains(new File("generated", "MiddleInterfaceFormats.scala")) shouldEqual false
code(new File("generated", "ChildTypeFormats.scala")).unindent should equalLines (
"""/**
| * This code is generated using sbt-datatype.
| * This code is generated using [[http://www.scala-sbt.org/contraband/ sbt-contraband]].
| */
|
|// DO NOT EDIT MANUALLY
Expand Down
4 changes: 2 additions & 2 deletions library/src/test/scala/GraphQLExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ package com.example @target(Scala) @codecPackage("generated")
interface Greeting @target(Java) {
message: String!
s: String = raw"com.example.Maybe.<String>just(\"1\")" @since("0.1.0")
s: String = raw"java.util.Optional.<String>ofNullable(\"1\")" @since("0.1.0")
}
type SimpleGreeting implements Greeting @target(Scala) {
message: String!
s: String = raw"com.example.Maybe.just[String](\"1\")" @since("0.1.0")
s: String = raw"java.util.Optional.ofNullable[String](\"1\")" @since("0.1.0")
}
""".stripMargin
}
91 changes: 41 additions & 50 deletions library/src/test/scala/GraphQLJavaCodeGenSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ class GraphQLJavaCodeGenSpec extends FlatSpec with Matchers with Inside with Equ
|public final class TypeExample implements java.io.Serializable {
| // Some extra code
|
| private com.example.MyOption<java.net.URL> field;
| public TypeExample(com.example.MyOption<java.net.URL> _field) {
| private java.util.Optional<java.net.URL> field;
| public TypeExample(java.util.Optional<java.net.URL> _field) {
| super();
| field = _field;
| }
| public TypeExample(java.net.URL _field) {
| super();
| field = com.example.MyOption.<java.net.URL>just(_field);
| field = java.util.Optional.<java.net.URL>ofNullable(_field);
| }
| public com.example.MyOption<java.net.URL> field() {
| public java.util.Optional<java.net.URL> field() {
| return this.field;
| }
| public TypeExample withField(com.example.MyOption<java.net.URL> field) {
| public TypeExample withField(java.util.Optional<java.net.URL> field) {
| return new TypeExample(field);
| }
| public TypeExample withField(java.net.URL field) {
| return new TypeExample(com.example.MyOption.<java.net.URL>just(field));
| return new TypeExample(java.util.Optional.<java.net.URL>ofNullable(field));
| }
| public boolean equals(Object obj) {
| if (this == obj) {
Expand Down Expand Up @@ -77,27 +77,27 @@ class GraphQLJavaCodeGenSpec extends FlatSpec with Matchers with Inside with Equ
code.head._2.unindent should equalLines (
"""package com.example;
|public final class Growable implements java.io.Serializable {
| private com.example.MyOption<Integer> field;
| private java.util.Optional<Integer> field;
| public Growable() {
| super();
| field = com.example.MyOption.<Integer>just(0);
| field = java.util.Optional.<Integer>ofNullable(0);
| }
| public Growable(com.example.MyOption<Integer> _field) {
| public Growable(java.util.Optional<Integer> _field) {
| super();
| field = _field;
| }
| public Growable(int _field) {
| super();
| field = com.example.MyOption.<Integer>just(_field);
| field = java.util.Optional.<Integer>ofNullable(_field);
| }
| public com.example.MyOption<Integer> field() {
| public java.util.Optional<Integer> field() {
| return this.field;
| }
| public Growable withField(com.example.MyOption<Integer> field) {
| public Growable withField(java.util.Optional<Integer> field) {
| return new Growable(field);
| }
| public Growable withField(int field) {
| return new Growable(com.example.MyOption.<Integer>just(field));
| return new Growable(java.util.Optional.<Integer>ofNullable(field));
| }
| public boolean equals(Object obj) {
| if (this == obj) {
Expand Down Expand Up @@ -126,44 +126,44 @@ class GraphQLJavaCodeGenSpec extends FlatSpec with Matchers with Inside with Equ
code.head._2.unindent should equalLines (
"""package com.example;
|public final class Foo implements java.io.Serializable {
| private com.example.MyOption<Integer> x;
| private java.util.Optional<Integer> x;
| private int[] y;
| public Foo() {
| super();
| x = com.example.MyOption.<Integer>nothing();
| x = java.util.Optional.<Integer>empty();
| y = new Array {};
| }
| public Foo(com.example.MyOption<Integer> _x) {
| public Foo(java.util.Optional<Integer> _x) {
| super();
| x = _x;
| y = new Array {};
| }
| public Foo(int _x) {
| super();
| x = com.example.MyOption.<Integer>just(_x);
| x = java.util.Optional.<Integer>ofNullable(_x);
| y = new Array {};
| }
| public Foo(com.example.MyOption<Integer> _x, int[] _y) {
| public Foo(java.util.Optional<Integer> _x, int[] _y) {
| super();
| x = _x;
| y = _y;
| }
| public Foo(int _x, int[] _y) {
| super();
| x = com.example.MyOption.<Integer>just(_x);
| x = java.util.Optional.<Integer>ofNullable(_x);
| y = _y;
| }
| public com.example.MyOption<Integer> x() {
| public java.util.Optional<Integer> x() {
| return this.x;
| }
| public int[] y() {
| return this.y;
| }
| public Foo withX(com.example.MyOption<Integer> x) {
| public Foo withX(java.util.Optional<Integer> x) {
| return new Foo(x, y);
| }
| public Foo withX(int x) {
| return new Foo(com.example.MyOption.<Integer>just(x), y);
| return new Foo(java.util.Optional.<Integer>ofNullable(x), y);
| }
| public Foo withY(int[] y) {
| return new Foo(x, y);
Expand Down Expand Up @@ -199,16 +199,16 @@ class GraphQLJavaCodeGenSpec extends FlatSpec with Matchers with Inside with Equ
|public abstract class InterfaceExample implements java.io.Serializable {
|
| // Some extra code
| private com.example.MyOption<Integer> field;
| public InterfaceExample(com.example.MyOption<Integer> _field) {
| private java.util.Optional<Integer> field;
| public InterfaceExample(java.util.Optional<Integer> _field) {
| super();
| field = _field;
| }
| public InterfaceExample(int _field) {
| super();
| field = com.example.MyOption.<Integer>just(_field);
| field = java.util.Optional.<Integer>ofNullable(_field);
| }
| public com.example.MyOption<Integer> field() {
| public java.util.Optional<Integer> field() {
| return this.field;
| }
| public boolean equals(Object obj) {
Expand All @@ -231,29 +231,29 @@ class GraphQLJavaCodeGenSpec extends FlatSpec with Matchers with Inside with Equ
code2 should equalLines (
"""package com.example;
|public final class ChildType extends com.example.InterfaceExample {
| private com.example.MyOption<String> name;
| public ChildType(com.example.MyOption<String> _name, com.example.MyOption<Integer> _field) {
| private java.util.Optional<String> name;
| public ChildType(java.util.Optional<String> _name, java.util.Optional<Integer> _field) {
| super(_field);
| name = _name;
| }
| public ChildType(String _name, int _field) {
| super(com.example.MyOption.<Integer>just(_field));
| name = com.example.MyOption.<String>just(_name);
| super(java.util.Optional.<Integer>ofNullable(_field));
| name = java.util.Optional.<String>ofNullable(_name);
| }
| public com.example.MyOption<String> name() {
| public java.util.Optional<String> name() {
| return this.name;
| }
| public ChildType withName(com.example.MyOption<String> name) {
| public ChildType withName(java.util.Optional<String> name) {
| return new ChildType(name, field());
| }
| public ChildType withName(String name) {
| return new ChildType(com.example.MyOption.<String>just(name), field());
| return new ChildType(java.util.Optional.<String>ofNullable(name), field());
| }
| public ChildType withField(com.example.MyOption<Integer> field) {
| public ChildType withField(java.util.Optional<Integer> field) {
| return new ChildType(name, field);
| }
| public ChildType withField(int field) {
| return new ChildType(name, com.example.MyOption.<Integer>just(field));
| return new ChildType(name, java.util.Optional.<Integer>ofNullable(field));
| }
| public boolean equals(Object obj) {
| if (this == obj) {
Expand Down Expand Up @@ -281,16 +281,16 @@ class GraphQLJavaCodeGenSpec extends FlatSpec with Matchers with Inside with Equ
"""package com.example;
|public abstract class IntfExample implements java.io.Serializable {
| /** I'm a field. */
| private com.example.MyOption<Integer> field;
| public IntfExample(com.example.MyOption<Integer> _field) {
| private java.util.Optional<Integer> field;
| public IntfExample(java.util.Optional<Integer> _field) {
| super();
| field = _field;
| }
| public IntfExample(int _field) {
| super();
| field = com.example.MyOption.<Integer>just(_field);
| field = java.util.Optional.<Integer>ofNullable(_field);
| }
| public com.example.MyOption<Integer> field() {
| public java.util.Optional<Integer> field() {
| return this.field;
| }
| /**
Expand All @@ -300,7 +300,7 @@ class GraphQLJavaCodeGenSpec extends FlatSpec with Matchers with Inside with Equ
| Make sure it is awesome.
| * @param arg1 This argument is not important, so it gets single line doc.
| */
| public abstract int[] messageExample(com.example.MyLazy<int[]> arg0,com.example.MyOption<Boolean> arg1);
| public abstract int[] messageExample(com.example.MyLazy<int[]> arg0,java.util.Optional<Boolean> arg1);
| public boolean equals(Object obj) {
| if (this == obj) {
| return true;
Expand All @@ -320,16 +320,7 @@ class GraphQLJavaCodeGenSpec extends FlatSpec with Matchers with Inside with Equ
|}""".stripMargin.unindent
)
}
lazy val instantiateJavaOptional: (String, String) => String =
{
(tpe: String, e: String) =>
e match {
case "null" => s"com.example.MyOption.<$tpe>nothing()"
case e => s"com.example.MyOption.<$tpe>just($e)"
}
}

def mkJavaCodeGen: JavaCodeGen =
new JavaCodeGen("com.example.MyLazy", "com.example.MyOption", instantiateJavaOptional,
new JavaCodeGen("com.example.MyLazy", CodeGen.javaOptional, CodeGen.instantiateJavaOptional,
wrapOption = true)
}
Loading

0 comments on commit 078cb0a

Please sign in to comment.