-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0.8.3: resolve #6: "generate conf template"
- Loading branch information
Showing
9 changed files
with
218 additions
and
45 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
2017-09-05 - 0.8.3 | ||
|
||
- resolve #6: "generate conf template" | ||
|
||
2017-07-13 | ||
|
||
- basic experimentation with static-config | ||
|
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 |
---|---|---|
@@ -1 +1 @@ | ||
tscfg.version = 0.8.2 | ||
tscfg.version = 0.8.3 |
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,87 @@ | ||
package tscfg.generators | ||
|
||
import tscfg.model._ | ||
|
||
case class TemplateOpts(indent: String = " ", | ||
commentPrefix: String = "##" | ||
) | ||
|
||
class TemplateGenerator(opts: TemplateOpts) { | ||
def generate(o: ObjectType): String = genObjectTypeMembers(o) | ||
|
||
private def genObjectType(o: ObjectType): String = { | ||
val members = opts.indent + genObjectTypeMembers(o).replaceAll("\n", "\n" + opts.indent) | ||
s"""{ | ||
|$members | ||
|}""".stripMargin | ||
} | ||
|
||
private def genObjectTypeMembers(o: ObjectType): String = { | ||
val symbols = o.members.keys.toList.sorted | ||
symbols.map { symbol ⇒ | ||
val a = o.members(symbol) | ||
|
||
val (annotations, comments) = a.comments match { | ||
case None ⇒ (List.empty, List.empty) | ||
case Some(str) ⇒ | ||
val lines = str.split("\n").toList.filterNot(_.startsWith("!")) | ||
lines.partition(_.startsWith("@")) | ||
} | ||
|
||
val cmn = if (comments.isEmpty) "" else { | ||
opts.commentPrefix + comments.mkString("\n" + opts.commentPrefix) + "\n" | ||
} | ||
|
||
val opt = if (a.isOptional) "optional" else "required" | ||
val dfl = a.default.map(d ⇒ s". Default: $d").getOrElse("") | ||
val (typ, abbrev) = gen(a.t) | ||
val abbrev2 = if (abbrev == abbrevObject) "section" else abbrev | ||
val decl = opts.commentPrefix + s" '$symbol': $opt $abbrev2$dfl.\n" | ||
|
||
val sysPropOpt: Option[String] = annotations.find(_.startsWith(sysPropAnn)) | ||
.map(_.substring(sysPropAnn.length).trim) | ||
|
||
val envVarOpt: Option[String] = annotations.find(_.startsWith(envVarAnn)) | ||
.map(_.substring(envVarAnn.length).trim) | ||
|
||
val assign = if (abbrev == abbrevObject) | ||
symbol + " " + typ // do not include `=' for an object | ||
else if (isBasicAbbrev(abbrev)) | ||
symbol + " = " + "?" | ||
else | ||
symbol + " = " + typ | ||
|
||
val assignSysProp = sysPropOpt.map(e ⇒ s"\n$symbol = $${$e}").getOrElse("") | ||
val assignEnvVar = envVarOpt.map(e ⇒ s"\n$symbol = $${?$e}").getOrElse("") | ||
|
||
decl + | ||
cmn + | ||
(if (a.isOptional) "#" + assign.replaceAll("\n", "\n#") else assign) + | ||
assignSysProp + | ||
assignEnvVar | ||
|
||
}.mkString("\n\n") | ||
} | ||
|
||
private def gen(typ: Type): (String,String) = typ match { | ||
case o:ObjectType ⇒ | ||
(genObjectType(o), abbrevObject) | ||
|
||
case b:BasicType ⇒ | ||
val lc = b.toString.toLowerCase | ||
(lc, lc) | ||
|
||
case ListType(t) ⇒ | ||
val (subTyp, abbrev) = gen(t) | ||
(s"[$subTyp, ...]", abbrevListOf + " " + abbrev) | ||
} | ||
|
||
private def isBasicAbbrev(abbrev: String): Boolean = | ||
abbrev != abbrevObject && !abbrev.startsWith(abbrevListOf) | ||
|
||
private val abbrevObject = "object" | ||
private val abbrevListOf = "list of" | ||
|
||
private val envVarAnn = "@envvar" | ||
private val sysPropAnn = "@sysprop" | ||
} |
This file was deleted.
Oops, something went wrong.
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,33 @@ | ||
#! Comments starting with #! are not transferred to template. | ||
# Description of the required endpoint section. | ||
endpoint { | ||
# The path associated with the endpoint. | ||
# For example, "/home/foo/bar" | ||
#@envvar ENDPOINT_PATH | ||
path = "string" | ||
|
||
# Port for the endpoint service. | ||
#@envvar ENDPOINT_PORT | ||
port = "int | 8080" | ||
|
||
# Configuration for notifications | ||
notifications { | ||
# Emails to send notifications to. | ||
emails = [ { | ||
email: string | ||
|
||
name: "string?" | ||
} ] | ||
} | ||
|
||
# Some optional stuff. | ||
#@optional | ||
stuff { | ||
# Coeficient matrix | ||
coefs: [[double]] | ||
|
||
#@sysprop endpoint.port | ||
port2: int | ||
} | ||
} | ||
|
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,40 @@ | ||
## 'endpoint': required section. | ||
## Description of the required endpoint section. | ||
endpoint { | ||
## 'notifications': required section. | ||
## Configuration for notifications | ||
notifications { | ||
## 'emails': required list of object. | ||
## Emails to send notifications to. | ||
emails = [{ | ||
## 'email': required string. | ||
email = ? | ||
|
||
## 'name': optional string. | ||
#name = ? | ||
}, ...] | ||
} | ||
|
||
## 'path': required string. | ||
## The path associated with the endpoint. | ||
## For example, "/home/foo/bar" | ||
path = ? | ||
path = ${?ENDPOINT_PATH} | ||
|
||
## 'port': optional integer. Default: 8080. | ||
## Port for the endpoint service. | ||
#port = ? | ||
port = ${?ENDPOINT_PORT} | ||
|
||
## 'stuff': optional section. | ||
## Some optional stuff. | ||
#stuff { | ||
# ## 'coefs': required list of list of double. | ||
# ## Coeficient matrix | ||
# coefs = [[double, ...], ...] | ||
# | ||
# ## 'port2': required integer. | ||
# port2 = ? | ||
# port2 = ${endpoint.port} | ||
#} | ||
} |