Skip to content

Commit

Permalink
0.7.0 - update use examples
Browse files Browse the repository at this point in the history
  • Loading branch information
carueda committed Dec 13, 2016
1 parent 189304e commit 8f696cc
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 62 deletions.
19 changes: 10 additions & 9 deletions changelog.md
@@ -1,20 +1,21 @@
2016-12-12 - 0.7.0
- capture model build warnings
- capture model build warnings

2016-12-01 - 0.7.0
- another model revision
- another model revision

2016-12-01 - 0.5.1
- put "generated by" when running from main program
- put "generated by" when running from main program

2016-11-30 - 0.5.0
- resolve #16 "rewrite code"
- resolve #15 "support list"
- resolve #10 "annotations" -- basically only the `#@optional` one
- NOTE: toString not generated anymore
- TODO revisit template generation (info moved to a wiki page)
- resolve #16 "rewrite code"
- resolve #15 "support list"
- resolve #10 "annotations" -- basically only the `#@optional` one
- NOTE: toString not generated anymore
- TODO revisit template generation (info moved to a wiki page)

2016-11-22 - 0.5.0
- advancing a rewrite...
- advancing a rewrite...

2016-11-19 - 0.3.4
- re #15 "support list"
Expand Down
27 changes: 14 additions & 13 deletions readme.md
Expand Up @@ -33,10 +33,10 @@ The tool is already pretty usable.
It supports a good part of the common types as supported by Typesafe Config
(string, int, long, double, boolean, duration, list)
and has good test coverage.
However, it's in general still work in progress.
A missing "type" is [size in bytes](https://github.com/typesafehub/config/blob/master/HOCON.md#size-in-bytes-format);
However, it can be enhanced in a number of ways, including:
command line interface can be improved;
syntax for types is not stable yet.
syntax for types is not stable yet;
and a missing "type" is [size in bytes](https://github.com/typesafehub/config/blob/master/HOCON.md#size-in-bytes-format).
Feel free to fork, enter issues, submit PRs, etc.


Expand Down Expand Up @@ -175,7 +175,7 @@ object ScalaExampleCfg {

## running tscfg

You will need a JRE 8 and the latest "fat" JAR (tscfg-x.y.z.jar)
You will need a JRE 8 and the latest fat JAR (tscfg-x.y.z.jar)
from the [releases](https://github.com/carueda/tscfg/releases).

> Or run `sbt assembly` under a clone of this repo to generate the fat jar.
Expand Down Expand Up @@ -273,7 +273,7 @@ The following basic types are supported:
#### durations

A duration type can be further qualified with a suffix consisting of a colon
an a desired time unit for the reported value.
and a desired time unit for the reported value.
For example, with the type `"duration:day"`, the reported long value will be in day units,
with conversion automatically performed if the actual configuration value is given in
any other unit as supported by Typesafe Config according to the
Expand Down Expand Up @@ -329,26 +329,27 @@ positions: [
]
```

In Java and Scala this basically becomes:
In Java this basically becomes:

```java
public class Cfg {
public final java.util.List<$Elm> positions;
public final java.util.List<Cfg.Positions$Elm> positions;

public static class $Elm {
public static class Positions$Elm {
public final double lat;
public final double lon;
}
}
```

and in Scala:
```scala
case class Cfg(
positions : List[Cfg.$Elm]
positions : List[Cfg.Positions$Elm]
)

object Cfg {
case class $Elm(
case class Positions$Elm(
lat : Double,
lon : Double
)
Expand Down Expand Up @@ -376,15 +377,15 @@ In Scala this basically becomes:
```scala
case class Cfg(
email : Option[Cfg.Email],
reals : Option[List[Cfg.$Elm]]
reals : Option[List[Cfg.Reals$Elm]]
)

object Cfg {
case class Email(
password : String,
server : String
)
case class $Elm(
case class Reals$Elm(
foo : Double
)
}
Expand All @@ -407,7 +408,7 @@ may become more apparent.

**Could tscfg generate `Optional<T>` by default for optional fields?**

Sorry, it's not implemented yet. Want to contribute?
It's not yet implemented. Want to contribute?

**What happened with the generated `toString` method?**

Expand Down
22 changes: 8 additions & 14 deletions src/main/java/tscfg/example/JavaExampleCfg.java
@@ -1,40 +1,34 @@
package tscfg.example;

public class JavaExampleCfg {
public final Endpoint endpoint;

public final JavaExampleCfg.Endpoint endpoint;
public static class Endpoint {
public final int intReq;
public final Interface_ interface_;
public final Endpoint.Interface_ interface_;
public final java.lang.String path;
public final java.lang.Integer serial;
public final java.lang.String url;

public static class Interface_ {
public final int port;
public final java.lang.String type;

public Interface_(com.typesafe.config.Config c) {
this.port = c.hasPathOrNull("port") ? c.getInt("port") : 8080;
this.type = c.hasPathOrNull("type") ? c.getString("type") : null;
}
}

public Endpoint(com.typesafe.config.Config c) {
this.intReq = c.getInt("intReq");
this.interface_ = new Interface_(_$config(c, "interface"));
this.interface_ = new Endpoint.Interface_(c.getConfig("interface"));
this.path = c.getString("path");
this.serial = c.hasPathOrNull("serial") ? c.getInt("serial") : null;
this.url = c.hasPathOrNull("url") ? c.getString("url") : "http://example.net";
}
}

public JavaExampleCfg(com.typesafe.config.Config c) {
this.endpoint = new Endpoint(_$config(c, "endpoint"));
}

private static com.typesafe.config.Config _$config(com.typesafe.config.Config c, java.lang.String path) {
return c.hasPathOrNull(path) ? c.getConfig(path) : null;
this.endpoint = new JavaExampleCfg.Endpoint(c.getConfig("endpoint"));
}
}

17 changes: 12 additions & 5 deletions src/main/java/tscfg/example/JavaUse.java
@@ -1,5 +1,7 @@
package tscfg.example;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigRenderOptions;
Expand Down Expand Up @@ -28,13 +30,18 @@ public static void main(String[] args) {
Integer serial2 = cfg.endpoint.serial;
int port2 = cfg.endpoint.interface_.port;
String type2 = cfg.endpoint.interface_.type;

System.out.println("\n*** tscfg toString: ***");
System.out.println(cfg.toString());

System.out.println("\n*** Typesafe Config toString: ***");
System.out.println("\n*** tscfg POJO structure (in JSON): *** ");
System.out.println(" " + toJson(cfg).replaceAll("\n", "\n "));
System.out.println("\n*** Typesafe rendering of input Config object: *** ");
ConfigRenderOptions options = ConfigRenderOptions.defaults()
.setFormatted(true).setComments(true).setOriginComments(false);
System.out.println(tsConfig.root().render(options));
}

private static String toJson(JavaExampleCfg cfg) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(cfg);
}
}
32 changes: 16 additions & 16 deletions src/main/scala/tscfg/example/ScalaExampleCfg.scala
Expand Up @@ -3,43 +3,43 @@ package tscfg.example
case class ScalaExampleCfg(
endpoint : ScalaExampleCfg.Endpoint
)

object ScalaExampleCfg {
case class Endpoint(
intReq : scala.Int,
interface : Endpoint.Interface,
interface : ScalaExampleCfg.Endpoint.Interface,
path : java.lang.String,
serial : scala.Option[scala.Int],
url : java.lang.String
)

object Endpoint {
case class Interface(
port : scala.Int,
`type` : scala.Option[java.lang.String]
)

object Interface {
def apply(c: com.typesafe.config.Config): Interface = {
Interface(
if(c.hasPathOrNull("port")) c.getInt("port") else 8080,
if(c.hasPathOrNull("type")) Some(c.getString("type")) else None
def apply(c: com.typesafe.config.Config): ScalaExampleCfg.Endpoint.Interface = {
ScalaExampleCfg.Endpoint.Interface(
port = if(c.hasPathOrNull("port")) c.getInt("port") else 8080,
`type` = if(c.hasPathOrNull("type")) Some(c.getString("type")) else None
)
}
}
def apply(c: com.typesafe.config.Config): Endpoint = {
Endpoint(
c.getInt("intReq"),
Interface(c.getConfig("interface")),
c.getString("path"),
if(c.hasPathOrNull("serial")) Some(c.getInt("serial")) else None,
if(c.hasPathOrNull("url")) c.getString("url") else "http://example.net"

def apply(c: com.typesafe.config.Config): ScalaExampleCfg.Endpoint = {
ScalaExampleCfg.Endpoint(
intReq = c.getInt("intReq"),
interface = ScalaExampleCfg.Endpoint.Interface(c.getConfig("interface")),
path = c.getString("path"),
serial = if(c.hasPathOrNull("serial")) Some(c.getInt("serial")) else None,
url = if(c.hasPathOrNull("url")) c.getString("url") else "http://example.net"
)
}
}

def apply(c: com.typesafe.config.Config): ScalaExampleCfg = {
ScalaExampleCfg(
Endpoint(c.getConfig("endpoint"))
endpoint = ScalaExampleCfg.Endpoint(c.getConfig("endpoint"))
)
}
}

20 changes: 15 additions & 5 deletions src/main/scala/tscfg/example/scalaUse.scala
Expand Up @@ -31,13 +31,23 @@ object scalaUse {
val port2: Int = cfg.endpoint.interface.port
val type2: Option[String] = cfg.endpoint.interface.`type`

println("\n*** tscfg toString: *** ")
println(cfg.toString)
println("\n*** tscfg case class structure: *** ")
println(" " + cfg.toString.replaceAll("\n", "\n "))
println("\n *** in JSON format: *** ")
println(" " + toJson(cfg).toString.replaceAll("\n", "\n "))

println("\n*** Typesafe Config toString: *** ")
println("\n*** Typesafe rendering of input Config object: *** ")
val options: ConfigRenderOptions = ConfigRenderOptions.defaults
.setFormatted(true).setComments(true).setOriginComments(false)
println(tsConfig.root.render(options))
println(" " + tsConfig.root.render(options).replaceAll("\n", "\n "))
}

}
def toJson(cfg: ScalaExampleCfg): String = {
import org.json4s._
import org.json4s.native.Serialization
import org.json4s.native.Serialization.writePretty
implicit val formats = Serialization.formats(NoTypeHints)

writePretty(cfg)
}
}

0 comments on commit 8f696cc

Please sign in to comment.