├── .gitattributes ├── .gitignore ├── Gocodegen.png ├── LICENSE ├── README.md ├── dist └── go-executable-build.sh ├── examples ├── 00-markdown │ ├── samples.generated.txt │ ├── samples.tpl │ └── samples.yaml ├── 01-values │ ├── contact-key-index.tpl │ ├── contact-with.tpl │ ├── contact.generated.txt │ ├── contact.json │ ├── contact.tpl │ ├── email.generated.txt │ ├── email.json │ ├── email.tpl │ ├── email.yaml │ ├── properties-whitespace.generated.txt │ ├── properties-whitespace.json │ └── properties-whitespace.tpl ├── 02-loop │ ├── db-schema.generated.txt │ ├── db-schema.json │ └── db-schema.tpl ├── 03-conditions │ ├── logic.generated.txt │ ├── logic.json │ ├── logic.tpl │ ├── numbers.generated.txt │ ├── numbers.json │ └── numbers.tpl ├── 04-builtin-functions │ ├── escape.generated.txt │ ├── escape.json │ ├── escape.tpl │ ├── logs.generated.txt │ ├── logs.json │ ├── logs.tpl │ ├── print.generated.txt │ ├── print.json │ └── print.tpl ├── 06-custom-functions │ ├── javabean.generated.txt │ ├── javabean.json │ └── javabean.tpl ├── 07-multiplefiles │ ├── mailing.generated.txt │ ├── mailing.json │ ├── mailing.tpl │ ├── test1 │ ├── test2 │ └── test3 └── 08-subtemplate │ ├── template.generated.txt │ ├── template.json │ └── template.tpl └── src ├── go.mod ├── go.sum ├── main.go └── my-funcs.go /.gitattributes: -------------------------------------------------------------------------------- 1 | src/examples/* linguist-vendored 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | pkg 2 | .idea 3 | -------------------------------------------------------------------------------- /Gocodegen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phcollignon/Go-Template/305b538d5044d3a7cbfe9bd7f37b138d652523d9/Gocodegen.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Philippe-Collignon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | This repository contains a Go code generator based on Go Template and a set of Go Template examples. 3 | 4 | 5 | ## Go code generator 6 | Go templates are one of the best choice to build a fast and small code generation tool. 7 | 8 | The sample code generator tool presented here can generate any type of code from Go templates and json or yaml data. 9 | 10 | ![alt text](Gocodegen.png "Logo Title Text 1") 11 | 12 | 13 | # Installation 14 | Download zip archive from github or clone this repository 15 | 16 | `git clone https://github.com/phcollignon/Go-Template` 17 | 18 | 19 | # Usage 20 | You can use it as a command line or as a go library to dynamically generate code in go programs. 21 | 22 | ## build the binary 23 | Go in `Go-Template/src` directory. 24 | 25 | ``` 26 | cd Go-Template/src 27 | go build 28 | ``` 29 | 30 | Add Go-Template to your binaries or to your PATH (i.e `sudo mv Go-Template /usr/local/bin`) 31 | 32 | ## command line 33 | Go in one of the examples directory and run Go-Template with a data file and a template file as parameters. 34 | 35 | ``` 36 | cd Go-Template/examples/01-values 37 | Go-Template -d email.json -t email.tpl 38 | ``` 39 | 40 | ## output directory 41 | 42 | By default, generated files are created in the same folder as the json file. 43 | 44 | You can change the ouput directory with `-o outputdirectory` 45 | 46 | `Go-Template -d email.json -t email.tpl -o /tmp/gocodegentest` 47 | 48 | ## multiple file generation 49 | If your Json Data file is designed to generate multiple files (see Example 5 below), use the following command : 50 | 51 | `Go-Template -d mailing.json -t mailing.tpl -m multi` 52 | 53 | # Examples 54 | 55 | We provide here some code generation examples, please note that gocodgen is based on go templates, so for more advanced features, you can look at go template documentation. 56 | 57 | ## 01 : Simple code generation 58 | 59 | Let's suppose the following email template : 60 | 61 | ``` 62 | Dear {{.Name}}, 63 | 64 | Hello, 65 | 66 | We would like to assign some tasks for {{.Project}} project : 67 | {{range .Topics}} 68 | - {{.}} 69 | {{end}} 70 | 71 | Can we plan a meeting on {{.Date}} ? 72 | 73 | Regards, 74 | ``` 75 | 76 | And the following data : 77 | 78 | ``` 79 | { 80 | "Name": "Phil", 81 | "Date": "01/01/2018", 82 | "Project": "Go-Template", 83 | "Topics": [ 84 | "write documentation", 85 | "publish to github.com", 86 | "add more examples" 87 | ] 88 | } 89 | 90 | ``` 91 | 92 | The following command, 93 | 94 | `Go-Template -d 01-values/email.json -t 01-values/email.tpl` 95 | 96 | generates `email.generated.txt` file 97 | 98 | ``` 99 | 100 | Dear Phil, 101 | 102 | Hello, 103 | 104 | We would like to assign some tasks for Go-Template project : 105 | 106 | - write documentation 107 | 108 | - publish to github.com 109 | 110 | - add more examples 111 | 112 | 113 | Can we plan a meeting on 01/01/2018 ? 114 | 115 | Regards, 116 | 117 | ``` 118 | 119 | 120 | ## 02 : Loop on Json Data 121 | 122 | Let's suppose the following template to generate SQL code for table create : 123 | 124 | ``` 125 | {{range .Schema.Table}} 126 | CREATE TABLE {{ .Name}} ( 127 | {{range $idx, $column := .Column}} 128 | {{if $idx }} , {{end}}{{ $column.Name}} {{$column.Type}} {{if $column.Size }} {{$column.Size}} {{end}} 129 | {{end}} 130 | ) 131 | {{end}} 132 | 133 | ``` 134 | 135 | And the following schema definition : 136 | 137 | ``` 138 | { 139 | "Schema": { 140 | "Table": [ 141 | { 142 | "Name": "EMPLOYEE", 143 | "Column": [ 144 | { 145 | "Name": "ID", 146 | "Type": "INTEGER" 147 | }, 148 | { 149 | "Name": "FIRSTNAME", 150 | "Type": "VARCHAR", 151 | "Size": "256" 152 | }, 153 | { 154 | "Name": "LASTNAME", 155 | "Type": "VARCHAR", 156 | "Size": "256" 157 | }, 158 | { 159 | "Name": "AGE", 160 | "Type": "INTEGER" 161 | } 162 | ] 163 | }, 164 | { 165 | "Name": "PRODUCT", 166 | "Column": [ 167 | { 168 | "Name": "ID", 169 | "Type": "INTEGER" 170 | }, 171 | { 172 | "Name": "NAME", 173 | The following command, 174 | 175 | "Type": "VARCHAR", 176 | "Size": "256" 177 | }, 178 | { 179 | "Name": "PRICE", 180 | "Type": "REAL" 181 | } 182 | ] 183 | } 184 | ] 185 | } 186 | } 187 | 188 | ``` 189 | 190 | `Go-Template -d schema.json -t createtable.tpl` 191 | 192 | generates the following file 193 | 194 | ``` 195 | CREATE TABLE EMPLOYEE ( 196 | ID INTEGER 197 | , FIRSTNAME VARCHAR 256 198 | , LASTNAME VARCHAR 256 199 | , AGE INTEGER 200 | ) 201 | 202 | CREATE TABLE PRODUCT ( 203 | ID INTEGER 204 | , NAME VARCHAR 256 205 | , PRICE REAL 206 | ) 207 | ``` 208 | 209 | ## 03 : Add conditions and Golang template functions 210 | 211 | Let's imagine you want to compare some numbers : 212 | 213 | ``` 214 | {{ range .Number}} 215 | {{if eq .n1 .n2 }} 216 | {{- .n1}} = {{.n2}} 217 | {{else}} 218 | {{- if lt .n1 .n2 }} 219 | {{- .n1}} < {{.n2}} 220 | {{else}} 221 | {{- .n1}} > {{.n2}} 222 | {{end}} 223 | {{end}} 224 | {{end}} 225 | 226 | ``` 227 | 228 | And the following numbers to be compared : 229 | 230 | ``` 231 | { 232 | "Number" : [ 233 | {"n1":1,"n2":2}, 234 | {"n1":5,"n2":3}, 235 | {"n1":2,"n2":2}, 236 | {"n1":1,"n2":5}, 237 | {"n1":6,"n2":6}, 238 | {"n1":5,"n2":2} 239 | 240 | ] 241 | } 242 | 243 | ``` 244 | 245 | `Go-Template -d numbers.json -t numbers.tpl` 246 | 247 | generates the following file 248 | 249 | ``` 250 | 1 < 2 251 | 5 > 3 252 | 2 = 2 253 | 1 < 5 254 | 6 = 6 255 | 5 > 2 256 | 257 | ``` 258 | ## 04 : Custom functions 259 | 260 | Let's now consider a more advanced sample where you want to apply some custome functions in your code generation. 261 | 262 | The JavaBeans setter and getter functions must be written as `getPropertyName()` for `propertyName` property. 263 | 264 | First, add a custom function in `my-funcs.go` file 265 | 266 | 267 | ``` 268 | var MyFuncMap = map[string]interface{}{ 269 | "ToGetterName": ToGetterName, 270 | "ToSetterName": ToSetterName} 271 | 272 | func ToGetterName(name string) string { 273 | return "get" + strings.Title(name) 274 | } 275 | func ToSetterName(name string) string { 276 | return "set" + strings.Title(name) 277 | } 278 | 279 | ``` 280 | 281 | Now you can use that custom function in your Go template : 282 | 283 | ``` 284 | public class User implements java.io.Serializable { 285 | {{range .User.Property }} 286 | private {{.Type}} {{ .Name }}; 287 | {{end}} 288 | 289 | 290 | {{range .User.Property }} 291 | public {{.Type}} {{ .Name | ToGetterName }}(){ 292 | return this.{{.Name}}; 293 | } 294 | {{end}} 295 | 296 | {{range .User.Property }} 297 | public void {{ .Name | ToSetterName }}({{.Type}} {{.Name}}){ 298 | this.{{.Name}} = {{.Name}}; 299 | } 300 | {{end}} 301 | } 302 | ``` 303 | 304 | Either compile the gocodgen for your platform or install Go and run 305 | `go run main.go my-funcs.go -d numbers.json -t numbers.tpl` 306 | 307 | For the following json data, 308 | 309 | ``` 310 | { 311 | "User": { 312 | "Name" : "Person", 313 | "Property": [ 314 | { 315 | "Name": "firstName", 316 | "Type": "String" 317 | }, 318 | { 319 | "Name": "lastName", 320 | "Type": "String" 321 | }, 322 | { 323 | "Name": "age", 324 | "Type": "Integer" 325 | } 326 | ] 327 | } 328 | } 329 | 330 | ``` 331 | 332 | You get this result : 333 | 334 | ``` 335 | public class User implements java.io.Serializable { 336 | 337 | private String firstName; 338 | 339 | private String lastName; 340 | 341 | private Integer age; 342 | 343 | 344 | 345 | 346 | public String getFirstName(){ 347 | return this.firstName; 348 | } 349 | 350 | public String getLastName(){ 351 | return this.lastName; 352 | } 353 | 354 | public Integer getAge(){ 355 | return this.age; 356 | } 357 | 358 | 359 | 360 | public void setFirstName(String firstName){ 361 | this.firstName = firstName; 362 | } 363 | 364 | public void setLastName(String lastName){ 365 | this.lastName = lastName; 366 | } 367 | 368 | public void setAge(Integer age){ 369 | this.age = age; 370 | } 371 | 372 | } 373 | 374 | ``` 375 | 376 | 377 | 378 | ## 05 : Generate multiple files 379 | 380 | Sometime, you want to generate a set of files from some data. 381 | 382 | The most obvious example is a mailing .. let's have the same email as in Example 01 383 | 384 | ``` 385 | Dear {{.Name}}, 386 | 387 | Hello, 388 | 389 | We would like to assign some tasks for {{.Project}} project : 390 | {{range .Topics}} 391 | - {{.}} 392 | {{end}} 393 | 394 | Can we plan a meeting on {{.Date}} ? 395 | 396 | Regards, 397 | 398 | ``` 399 | 400 | You include in your Json Data file, a `Files` key containing a table of objects. 401 | Each object has a `FileName` used to create the file, and a `Data` object which container the code generation data. 402 | 403 | ``` 404 | { 405 | "Files": [{ 406 | "FileName": "test1", 407 | "Data": { 408 | "Name": "phil", 409 | "Date": "01/01/2018", 410 | "Project": "Go-Template", 411 | "Topics": [ 412 | "write documentation", 413 | "publish to github.com", 414 | "add more examples" 415 | ] 416 | } 417 | }, 418 | { 419 | "FileName": "test2", 420 | "Data": { 421 | "Name": "john", 422 | "Date": "01/01/2018", 423 | "Project": "Go-Template", 424 | "Topics": [ 425 | "write documentation", 426 | "publish to github.com", 427 | "add more examples" 428 | ] 429 | } 430 | }, 431 | { 432 | "FileName": "test3", 433 | "Data": { 434 | "Name": "peter", 435 | "Date": "01/01/2018", 436 | "Project": "Go-Template", 437 | "Topics": [ 438 | "write documentation", 439 | "publish to github.com", 440 | "add more examples" 441 | ] 442 | } 443 | }] 444 | } 445 | 446 | ``` 447 | 448 | The following command (do not forget `-m multi` flag) : 449 | 450 | `Go-Template -d mailing.json -t mailing.tpl -m multi` 451 | 452 | generates 3 different files : 453 | 454 | test1 : 455 | 456 | ``` 457 | Dear phil, 458 | 459 | Hello, 460 | 461 | We would like to assign some tasks for Go-Template project : 462 | 463 | - write documentation 464 | 465 | - publish to github.com 466 | 467 | - add more examples 468 | 469 | 470 | Can we plan a meeting on 01/01/2018 ? 471 | 472 | Regards, 473 | 474 | ``` 475 | 476 | test2 : 477 | 478 | ``` 479 | Dear john, 480 | 481 | Hello, 482 | 483 | We would like to assign some tasks for Go-Template project : 484 | 485 | - write documentation 486 | 487 | - publish to github.com 488 | 489 | - add more examples 490 | 491 | 492 | Can we plan a meeting on 01/01/2018 ? 493 | 494 | Regards, 495 | 496 | 497 | ``` 498 | 499 | test3 : 500 | 501 | ``` 502 | 503 | Dear peter, 504 | 505 | Hello, 506 | 507 | We would like to assign some tasks for Go-Template project : 508 | 509 | - write documentation 510 | 511 | - publish to github.com 512 | 513 | - add more examples 514 | 515 | 516 | Can we plan a meeting on 01/01/2018 ? 517 | 518 | Regards, 519 | 520 | 521 | ``` 522 | 523 | ## Go templates examples 524 | Here is the list of Go template examples. You can use them in your own code or with the generator documented below. 525 | 526 | | directory | template | values | description | 527 | | --------- | ---------------------- | ------------ | ----------------------------------------------------- | 528 | | 01-values | [contact.tpl](./examples/01-values/contact.tpl) | [contact.json](./examples/01-values/contact.json) | values interpolation | 529 | | 01-values | [contact-with.tpl](./examples/01-values/contact-with.tpl) | [contact.json](./examples/01-values/contact.json) | values interpolation with scoped object "with" action | 530 | | 01-values | [contact-key-index.tpl](./examples/01-values/contact-key-index.tpl) | [contact.json](./examples/01-values/contact.json) | values and keys interpolation with "index" function | 531 | | 01-values | [email.tpl](./examples/01-values/email.tpl) | [email.yaml](./examples/01-values/email.yaml) | values interpolation with array index | 532 | | 01-values | [properties-whitespace.tpl](./examples/01-values/properties-whitespace.tpl) | [properties-whitespace.yaml](./examples/01-values/properties-whitespace.yaml) | manage whitespace in java property file | 533 | | 02-loop | [db-schema.tpl](./examples/02-loop/db-schema.tpl) | [db-schema.json](./examples/02-loop/db-schema.json) | Iterate on values | 534 | | 03-conditions | [logic.tpl](./examples/03-conditions/logic.tpl) | [logic.json](./examples/03-conditions/logic.json) | Logic functions | 535 | | 03-conditions | [numbers.tpl](./examples/03-conditions/numbers.tpl) | [numbers.json](./examples/03-conditions/numbers.json) | Logic operators | 536 | | 04-builtin-functions | [logs.tpl](./examples/04-builtin-functions/logs.tpl) | [logs.json](./examples/04-builtin-functions/logs.json) | Index and length of a list | 537 | | 04-builtin-functions | [print.tpl](./examples/04-builtin-functions/print.tpl) | [print.json](./examples/04-builtin-functions/print.json) | Formatted print function | 538 | | 04-builtin-functions | [escape.tpl](./examples/04-builtin-functions/escape.tpl) | [escape.json](./examples/04-builtin-functions/escape.json) | URL query string, html and javascript escape | 539 | | 05-sprig-functions | N/A | N/A | [http://masterminds.github.io/sprig/](http://masterminds.github.io/sprig/) | 540 | | 06-custom-functions | [javabean.tpl](./examples/06-custom-functions/javabean.tpl) | [javabean.json](./examples/06-custom-functions/javabean.json) | Use of custom functions (defined in my-funcs.go) | 541 | | 07-multiplefiles | [mailing.tpl](./examples/07-multiplefiles/mailing.tpl) | [mailing.json](./examples/07-multiplefiles/mailing.json) | Generate multiple files with one template an one value file | 542 | | 08-subtemplate | [template.tpl](./examples/08-subtemplate/template.tpl) | [template.json](./examples/08-subtemplate/template.json) | Call a named sub-template | 543 | -------------------------------------------------------------------------------- /dist/go-executable-build.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | package=$1 4 | if [[ -z "$package" ]]; then 5 | echo "usage: $0 " 6 | exit 1 7 | fi 8 | package_split=(${package//\// }) 9 | package_name=${package_split[-1]} 10 | 11 | platforms=("linux/amd64" "windows/amd64" "windows/386" "darwin/amd64") 12 | 13 | for platform in "${platforms[@]}" 14 | do 15 | platform_split=(${platform//\// }) 16 | GOOS=${platform_split[0]} 17 | GOARCH=${platform_split[1]} 18 | output_name=$package_name'-'$GOOS'-'$GOARCH 19 | if [ $GOOS = "windows" ]; then 20 | output_name+='.exe' 21 | fi 22 | 23 | env GOOS=$GOOS GOARCH=$GOARCH go build -o $output_name $package 24 | if [ $? -ne 0 ]; then 25 | echo 'An error has occurred! Aborting the script execution...' 26 | exit 1 27 | fi 28 | done 29 | -------------------------------------------------------------------------------- /examples/00-markdown/samples.generated.txt: -------------------------------------------------------------------------------- 1 | | directory | template | values | description | 2 | | --------- | ---------------------- | ------------ | ----------------------------------------------------- | 3 | | 01-values | [contact.tpl](./src/examples/01-values/contact.tpl) | [contact.json](./src/examples/01-values/contact.json) | values interpolation | 4 | | 01-values | [contact-with.tpl](./src/examples/01-values/contact-with.tpl) | [contact.json](./src/examples/01-values/contact.json) | values interpolation with scoped object "with" action | 5 | | 01-values | [contact-key-index.tpl](./src/examples/01-values/contact-key-index.tpl) | [contact.json](./src/examples/01-values/contact.json) | values and keys interpolation with "index" function | 6 | | 01-values | [email.tpl](./src/examples/01-values/email.tpl) | [email.yaml](./src/examples/01-values/email.yaml) | values interpolation with array index | 7 | | 01-values | [properties-whitespace.tpl](./src/examples/01-values/properties-whitespace.tpl) | [properties-whitespace.yaml](./src/examples/01-values/properties-whitespace.yaml) | manage whitespace in java property file | 8 | | 02-loop | [db-schema.tpl](./src/examples/02-loop/db-schema.tpl) | [db-schema.json](./src/examples/02-loop/db-schema.json) | Iterate on values | 9 | | 03-conditions | [logic.tpl](./src/examples/03-conditions/logic.tpl) | [logic.json](./src/examples/03-conditions/logic.json) | Logic functions | 10 | | 03-conditions | [numbers.tpl](./src/examples/03-conditions/numbers.tpl) | [numbers.json](./src/examples/03-conditions/numbers.json) | Logic operators | 11 | | 04-builtin-functions | [logs.tpl](./src/examples/04-builtin-functions/logs.tpl) | [logs.json](./src/examples/04-builtin-functions/logs.json) | Index and length of a list | 12 | | 04-builtin-functions | [print.tpl](./src/examples/04-builtin-functions/print.tpl) | [print.json](./src/examples/04-builtin-functions/print.json) | Formatted print function | 13 | | 04-builtin-functions | [escape.tpl](./src/examples/04-builtin-functions/escape.tpl) | [escape.json](./src/examples/04-builtin-functions/escape.json) | URL query string, html and javascript escape | 14 | | 06-custom-functions | [javabean.tpl](./src/examples/06-custom-functions/javabean.tpl) | [javabean.json](./src/examples/06-custom-functions/javabean.json) | Use of custom functions (defined in my-funcs.go) | 15 | | 07-multiplefiles | [mailing.tpl](./src/examples/07-multiplefiles/mailing.tpl) | [mailing.json](./src/examples/07-multiplefiles/mailing.json) | Generate multiple files with one template an one value file | 16 | | 08-subtemplate | [template.tpl](./src/examples/08-subtemplate/template.tpl) | [template.json](./src/examples/08-subtemplate/template.json) | Call a named sub-template | 17 | -------------------------------------------------------------------------------- /examples/00-markdown/samples.tpl: -------------------------------------------------------------------------------- 1 | | directory | template | values | description | 2 | | --------- | ---------------------- | ------------ | ----------------------------------------------------- | 3 | {{- range .Category }} 4 | {{- $category := .Name}} 5 | {{- range .Examples }} 6 | | {{$category}} | [{{ .Template }}](./src/examples/{{ $category }}/{{ .Template}}) | [{{.Values }}](./src/examples/{{ $category }}/{{.Values }}) | {{.Description }} | 7 | {{- end }} 8 | {{- end }} 9 | -------------------------------------------------------------------------------- /examples/00-markdown/samples.yaml: -------------------------------------------------------------------------------- 1 | Category: 2 | - Name: "01-values" 3 | Examples: 4 | - Template: "contact.tpl" 5 | Values: "contact.json" 6 | Description: "values interpolation " 7 | - Template: "contact-with.tpl" 8 | Values: "contact.json" 9 | Description: "values interpolation with scoped object \"with\" action" 10 | - Template: "contact-key-index.tpl" 11 | Values: "contact.json" 12 | Description: "values and keys interpolation with \"index\" function" 13 | - Template: "email.tpl" 14 | Values: "email.yaml" 15 | Description: "values interpolation with array index" 16 | - Template: "properties-whitespace.tpl" 17 | Values: "properties-whitespace.yaml" 18 | Description: "manage whitespace in java property file" 19 | - Name: "02-loop" 20 | Examples: 21 | - Template: "db-schema.tpl" 22 | Values: "db-schema.json" 23 | Description: "Iterate on values" 24 | - Name: "03-conditions" 25 | Examples: 26 | - Template: "logic.tpl" 27 | Values: "logic.json" 28 | Description: "Logic functions" 29 | - Template: "numbers.tpl" 30 | Values: "numbers.json" 31 | Description: "Logic operators" 32 | - Name: "04-builtin-functions" 33 | Examples: 34 | - Template: "logs.tpl" 35 | Values: "logs.json" 36 | Description: "Index and length of a list" 37 | - Template: "print.tpl" 38 | Values: "print.json" 39 | Description: "Formatted print function" 40 | - Template: "escape.tpl" 41 | Values: "escape.json" 42 | Description: "URL query string, html and javascript escape" 43 | - Name: "06-custom-functions" 44 | Examples: 45 | - Template: "javabean.tpl" 46 | Values: "javabean.json" 47 | Description: "Use of custom functions (defined in my-funcs.go)" 48 | - Name: "07-multiplefiles" 49 | Examples: 50 | - Template: "mailing.tpl" 51 | Values: "mailing.json" 52 | Description: "Generate multiple files with one template an one value file" 53 | - Name: "08-subtemplate" 54 | Examples: 55 | - Template: "template.tpl" 56 | Values: "template.json" 57 | Description: "Call a named sub-template" -------------------------------------------------------------------------------- /examples/01-values/contact-key-index.tpl: -------------------------------------------------------------------------------- 1 | 2 | Contact Card: 3 | - Name : {{ .Name }}, {{ .Firstname }} 4 | - Address : 5 | {{- with .Address}} 6 | {{ .Street }} 7 | {{ .Postcode }}, {{ .City}} 8 | {{- end}} 9 | - Contact 10 | - Phones : 11 | {{- range $key,$value := .Phones}} 12 | - {{ $value }} ({{ $key }}) 13 | {{- end}} -------------------------------------------------------------------------------- /examples/01-values/contact-with.tpl: -------------------------------------------------------------------------------- 1 | 2 | Contact Card: 3 | - Name : {{ .Name }}, {{ .Firstname }} 4 | - Address : 5 | {{- with .Address}} 6 | {{ .Street }} 7 | {{ .Postcode }}, {{ .City}} 8 | {{- end}} 9 | - Contact 10 | - Phones : 11 | {{- with .Phones}} 12 | - {{ .Private }} (Private) 13 | - {{ .Work }} (Work) 14 | - {{ .Mobile }} (Mobile) 15 | {{- end}} 16 | 17 | 18 | -------------------------------------------------------------------------------- /examples/01-values/contact.generated.txt: -------------------------------------------------------------------------------- 1 | 2 | Contact Card: 3 | - Name : Doe, John 4 | - Address : 5 | Main Street, 101 6 | 1000, London 7 | - Contact 8 | - Phones : 9 | - 487-899-556 (Private) 10 | - 587-986-545 (Work) 11 | - 478-985-326 (Mobile) 12 | 13 | 14 | 15 | Contact Card: 16 | - Name : Doe, John 17 | - Address : 18 | Main Street, 101 19 | 1000, London 20 | - Contact 21 | - Phones : 22 | - 487-899-556 (Private) 23 | - 587-986-545 (Work) 24 | - 478-985-326 (Mobile) 25 | 26 | 27 | 28 | Contact Card: 29 | - Name : Doe, John 30 | - Address : 31 | Main Street, 101 32 | 1000, London 33 | - Contact 34 | - Phones : 35 | - 478-985-326 (Mobile) 36 | - 487-899-556 (Private) 37 | - 587-986-545 (Work) -------------------------------------------------------------------------------- /examples/01-values/contact.json: -------------------------------------------------------------------------------- 1 | { 2 | "Name": "Doe", 3 | "Firstname": "John", 4 | "Age": 35, 5 | "Address": { 6 | "Street": "Main Street, 101", 7 | "Postcode": "1000", 8 | "City": "London" 9 | }, 10 | "Phones": { 11 | "Private": "487-899-556", 12 | "Work": "587-986-545", 13 | "Mobile": "478-985-326" 14 | }, 15 | "Email": "john.doe@email.com", 16 | "Hobbies": [ 17 | "reading", 18 | "biking" 19 | ] 20 | } -------------------------------------------------------------------------------- /examples/01-values/contact.tpl: -------------------------------------------------------------------------------- 1 | 2 | Contact Card: 3 | - Name : {{ .Name }}, {{ .Firstname }} 4 | - Address : 5 | {{ .Address.Street }} 6 | {{ .Address.Postcode }}, {{ .Address.City}} 7 | - Contact 8 | - Phones : 9 | - {{ .Phones.Private }} (Private) 10 | - {{ .Phones.Work }} (Work) 11 | - {{ .Phones.Mobile }} (Mobile) 12 | -------------------------------------------------------------------------------- /examples/01-values/email.generated.txt: -------------------------------------------------------------------------------- 1 | 2 | Dear phil, 3 | 4 | Hello, 5 | 6 | We would like to assign some tasks for Go-Template project : 7 | 8 | - write documentation 9 | 10 | - publish to github.com 11 | 12 | - add more examples 13 | 14 | Important topic is : publish to github.com 15 | 16 | Can we plan a meeting on 01/01/2018 ? 17 | 18 | Regards, 19 | -------------------------------------------------------------------------------- /examples/01-values/email.json: -------------------------------------------------------------------------------- 1 | { 2 | "Name": "phil", 3 | "Date": "01/01/2018", 4 | "Project": "Go-Template", 5 | "Topics": [ 6 | "write documentation", 7 | "publish to github.com", 8 | "add more examples" 9 | ] 10 | } -------------------------------------------------------------------------------- /examples/01-values/email.tpl: -------------------------------------------------------------------------------- 1 | 2 | Dear {{.Name}}, 3 | 4 | Hello, 5 | 6 | We would like to assign some tasks for {{.Project}} project : 7 | {{range .Topics}} 8 | - {{. }} 9 | {{end}} 10 | Important topic is : {{ index .Topics 1 }} 11 | 12 | Can we plan a meeting on {{.Date}} ? 13 | 14 | Regards, 15 | -------------------------------------------------------------------------------- /examples/01-values/email.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | Name: phil 3 | Date: 01/01/2018 4 | Project: Go-Template 5 | Topics: 6 | - write documentation 7 | - publish to github.com 8 | - add more examples 9 | -------------------------------------------------------------------------------- /examples/01-values/properties-whitespace.generated.txt: -------------------------------------------------------------------------------- 1 | # db.properties java file 2 | # without whitespace control 3 | 4 | db.username=admin 5 | 6 | # db.password= # loaded from os env 7 | 8 | db.host=oracle 9 | db.port=1521 10 | 11 | 12 | 13 | # db.properties java file 14 | # without whitespace control 15 | 16 | db.username=admin 17 | # db.password= # loaded from os env 18 | db.host=oracle 19 | db.port=1521 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /examples/01-values/properties-whitespace.json: -------------------------------------------------------------------------------- 1 | { 2 | "Db": { 3 | "Username": "admin", 4 | "Password": "123456", 5 | "Host": "oracle", 6 | "Port": "1521" 7 | }, 8 | "Env": "prod" 9 | } -------------------------------------------------------------------------------- /examples/01-values/properties-whitespace.tpl: -------------------------------------------------------------------------------- 1 | # db.properties java file 2 | # without whitespace control 3 | {{ with .Db}} 4 | db.username={{ .Username }} 5 | {{ if eq $.Env "prod" }} 6 | # db.password= # loaded from os env 7 | {{ else }} 8 | db.password={{ .Password }} 9 | {{ end }} 10 | db.host={{ .Host }} 11 | db.port={{ .Port }} 12 | {{ end}} 13 | 14 | 15 | # db.properties java file 16 | # without whitespace control 17 | {{- with .Db }} 18 | db.username={{ .Username }} 19 | {{- if eq $.Env "prod" }} 20 | # db.password= # loaded from os env 21 | {{- else }} 22 | db.password={{ .Password }} 23 | {{- end }} 24 | db.host={{ .Host }} 25 | db.port={{ .Port }} 26 | {{- end}} 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /examples/02-loop/db-schema.generated.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | CREATE TABLE EMPLOYEE ( 4 | 5 | ID INTEGER 6 | 7 | , FIRSTNAME VARCHAR 256 8 | 9 | , LASTNAME VARCHAR 256 10 | 11 | , AGE INTEGER 12 | 13 | ) 14 | 15 | CREATE TABLE PRODUCT ( 16 | 17 | ID INTEGER 18 | 19 | , NAME VARCHAR 256 20 | 21 | , PRICE REAL 22 | 23 | ) 24 | 25 | -------------------------------------------------------------------------------- /examples/02-loop/db-schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "Schema": { 3 | "Table": [ 4 | { 5 | "Name": "EMPLOYEE", 6 | "Column": [ 7 | { 8 | "Name": "ID", 9 | "Type": "INTEGER" 10 | }, 11 | { 12 | "Name": "FIRSTNAME", 13 | "Type": "VARCHAR", 14 | "Size": "256" 15 | }, 16 | { 17 | "Name": "LASTNAME", 18 | "Type": "VARCHAR", 19 | "Size": "256" 20 | }, 21 | { 22 | "Name": "AGE", 23 | "Type": "INTEGER" 24 | } 25 | ] 26 | }, 27 | { 28 | "Name": "PRODUCT", 29 | "Column": [ 30 | { 31 | "Name": "ID", 32 | "Type": "INTEGER" 33 | }, 34 | { 35 | "Name": "NAME", 36 | "Type": "VARCHAR", 37 | "Size": "256" 38 | }, 39 | { 40 | "Name": "PRICE", 41 | "Type": "REAL" 42 | } 43 | ] 44 | } 45 | ] 46 | } 47 | } -------------------------------------------------------------------------------- /examples/02-loop/db-schema.tpl: -------------------------------------------------------------------------------- 1 | 2 | {{range .Schema.Table}} 3 | CREATE TABLE {{ .Name}} ( 4 | {{range $idx, $column := .Column}} 5 | {{if $idx }} , {{end}}{{ $column.Name}} {{$column.Type}} {{if $column.Size }} {{$column.Size}} {{end}} 6 | {{end}} 7 | ) 8 | {{end}} 9 | -------------------------------------------------------------------------------- /examples/03-conditions/logic.generated.txt: -------------------------------------------------------------------------------- 1 | 2 | true and true is true 3 | true or true is true 4 | true and not true is false 5 | 6 | false and false is false 7 | false or false is false 8 | false and not false is false 9 | 10 | true and false is false 11 | true or false is true 12 | true and not false is true 13 | 14 | false and true is false 15 | false or true is true 16 | false and not true is false 17 | 18 | 0 and 1 is 0 19 | 0 or 1 is 1 20 | 0 and not 1 is 0 21 | 22 | 1 and 0 is 0 23 | 1 or 0 is 1 24 | 1 and not 0 is true 25 | -------------------------------------------------------------------------------- /examples/03-conditions/logic.json: -------------------------------------------------------------------------------- 1 | { 2 | "Cases" : [ 3 | {"A":true,"B":true}, 4 | {"A":false,"B":false}, 5 | {"A":true,"B":false}, 6 | {"A":false,"B":true}, 7 | {"A":0,"B":1}, 8 | {"A":1,"B":0} 9 | 10 | ] 11 | } -------------------------------------------------------------------------------- /examples/03-conditions/logic.tpl: -------------------------------------------------------------------------------- 1 | {{ range .Cases}} 2 | {{ .A }} and {{ .B }} is {{ and .A .B }} 3 | {{ .A }} or {{ .B }} is {{ or .A .B }} 4 | {{ .A }} and not {{ .B }} is {{ and .A ( not .B ) }} 5 | {{end}} -------------------------------------------------------------------------------- /examples/03-conditions/numbers.generated.txt: -------------------------------------------------------------------------------- 1 | 1 < 2 2 | 5 > 3 3 | 2 = 2 4 | 1 < 5 5 | 6 = 6 6 | 5 > 2 7 | -------------------------------------------------------------------------------- /examples/03-conditions/numbers.json: -------------------------------------------------------------------------------- 1 | { 2 | "Number" : [ 3 | {"n1":1,"n2":2}, 4 | {"n1":5,"n2":3}, 5 | {"n1":2,"n2":2}, 6 | {"n1":1,"n2":5}, 7 | {"n1":6,"n2":6}, 8 | {"n1":5,"n2":2} 9 | 10 | ] 11 | } -------------------------------------------------------------------------------- /examples/03-conditions/numbers.tpl: -------------------------------------------------------------------------------- 1 | {{ range .Number}} 2 | {{- if eq .n1 .n2 }} 3 | {{- .n1}} = {{.n2}} 4 | {{- else}} 5 | {{- if lt .n1 .n2 }} 6 | {{- .n1}} < {{.n2}} 7 | {{- else}} 8 | {{- .n1}} > {{.n2}} 9 | {{- end}} 10 | {{- end}} 11 | {{end}} -------------------------------------------------------------------------------- /examples/04-builtin-functions/escape.generated.txt: -------------------------------------------------------------------------------- 1 | 2 | wget http://www.mysite.com?email%3Djohn%40doe.com%3Bmessage%3Dhello+world+%26+co+%21 3 | 4 | Is 3 < 5 ?, please enter the reponse here bellow & click enter 5 | 6 | It\'s the word \'hello\' that\'s important -------------------------------------------------------------------------------- /examples/04-builtin-functions/escape.json: -------------------------------------------------------------------------------- 1 | { 2 | "urlqs":"email=john@doe.com;message=hello world & co !", 3 | "input":"Is 3 < 5 ?, please enter the reponse here bellow & click enter", 4 | "myvar":"It's the word 'hello' that's important " 5 | } -------------------------------------------------------------------------------- /examples/04-builtin-functions/escape.tpl: -------------------------------------------------------------------------------- 1 | 2 | wget http://www.mysite.com?{{ .urlqs | urlquery }} 3 | 4 | {{ .input | html }} 5 | 6 | {{ .myvar | js}} -------------------------------------------------------------------------------- /examples/04-builtin-functions/logs.generated.txt: -------------------------------------------------------------------------------- 1 | First user : 2 | Clemensen 3 | Clemensen 4 | 5 | Number of users: 6 | 10 -------------------------------------------------------------------------------- /examples/04-builtin-functions/logs.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logs" : [{ 3 | "id": 1, 4 | "first_name": "Anna-maria", 5 | "last_name": "Clemensen", 6 | "email": "aclemensen0@seattletimes.com", 7 | "gender": "Female", 8 | "ip_address": "15.22.84.83" 9 | }, { 10 | "id": 2, 11 | "first_name": "Elvyn", 12 | "last_name": "Cokly", 13 | "email": "ecokly1@goo.gl", 14 | "gender": "Male", 15 | "ip_address": "129.188.251.204" 16 | }, { 17 | "id": 3, 18 | "first_name": "Greta", 19 | "last_name": "Kave", 20 | "email": "gkave2@google.com", 21 | "gender": "Female", 22 | "ip_address": "25.42.58.230" 23 | }, { 24 | "id": 4, 25 | "first_name": "Estella", 26 | "last_name": "Ferrone", 27 | "email": "eferrone3@google.ru", 28 | "gender": "Female", 29 | "ip_address": "137.218.157.134" 30 | }, { 31 | "id": 5, 32 | "first_name": "Gil", 33 | "last_name": "Itzchaky", 34 | "email": "gitzchaky4@telegraph.co.uk", 35 | "gender": "Male", 36 | "ip_address": "181.95.186.215" 37 | }, { 38 | "id": 6, 39 | "first_name": "Thorstein", 40 | "last_name": "State", 41 | "email": "tstate5@deliciousdays.com", 42 | "gender": "Male", 43 | "ip_address": "241.56.231.131" 44 | }, { 45 | "id": 7, 46 | "first_name": "Dolley", 47 | "last_name": "Stott", 48 | "email": "dstott6@ifeng.com", 49 | "gender": "Female", 50 | "ip_address": "198.126.235.40" 51 | }, { 52 | "id": 8, 53 | "first_name": "Alic", 54 | "last_name": "Westhead", 55 | "email": "awesthead7@about.com", 56 | "gender": "Male", 57 | "ip_address": "241.150.178.74" 58 | }, { 59 | "id": 9, 60 | "first_name": "Betteann", 61 | "last_name": "Lampe", 62 | "email": "blampe8@salon.com", 63 | "gender": "Female", 64 | "ip_address": "16.3.192.242" 65 | }, { 66 | "id": 10, 67 | "first_name": "Claudie", 68 | "last_name": "Corbin", 69 | "email": "ccorbin9@irs.gov", 70 | "gender": "Female", 71 | "ip_address": "14.229.6.16" 72 | }] 73 | } -------------------------------------------------------------------------------- /examples/04-builtin-functions/logs.tpl: -------------------------------------------------------------------------------- 1 | First user : 2 | {{ (index .Logs 0 ).last_name }} 3 | {{ (0 | index .Logs ).last_name }} 4 | 5 | Number of users: 6 | {{ .Logs | len }} 7 | 8 | -------------------------------------------------------------------------------- /examples/04-builtin-functions/print.generated.txt: -------------------------------------------------------------------------------- 1 | John Doe 2 | 5,Main St. 3 | 1000 - London 4 | Discount : 25.00 % 5 | -------------------------------------------------------------------------------- /examples/04-builtin-functions/print.json: -------------------------------------------------------------------------------- 1 | { 2 | "first_name":"John", 3 | "last_name":"Doe", 4 | "street":"Main St.", 5 | "number":"5", 6 | "zip":"1000", 7 | "city":"London", 8 | "discount":25 9 | 10 | } -------------------------------------------------------------------------------- /examples/04-builtin-functions/print.tpl: -------------------------------------------------------------------------------- 1 | {{ printf "%s %s" .first_name .last_name }} 2 | {{ printf "%s,%s" .number .street }} 3 | {{ printf "%s - %s" .zip .city }} 4 | Discount : {{ printf "%2.2f" .discount}} % 5 | -------------------------------------------------------------------------------- /examples/06-custom-functions/javabean.generated.txt: -------------------------------------------------------------------------------- 1 | public class User implements java.io.Serializable { 2 | 3 | private String firstName; 4 | 5 | private String lastName; 6 | 7 | private Integer age; 8 | 9 | 10 | 11 | 12 | public String getFirstName(){ 13 | return this.firstName; 14 | } 15 | 16 | public String getLastName(){ 17 | return this.lastName; 18 | } 19 | 20 | public Integer getAge(){ 21 | return this.age; 22 | } 23 | 24 | 25 | 26 | public void SetFirstName(String firstName){ 27 | this.firstName = firstName; 28 | } 29 | 30 | public void SetLastName(String lastName){ 31 | this.lastName = lastName; 32 | } 33 | 34 | public void SetAge(Integer age){ 35 | this.age = age; 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /examples/06-custom-functions/javabean.json: -------------------------------------------------------------------------------- 1 | { 2 | "User": { 3 | "Name" : "Person", 4 | "Property": [ 5 | { 6 | "Name": "firstName", 7 | "Type": "String" 8 | }, 9 | { 10 | "Name": "lastName", 11 | "Type": "String" 12 | }, 13 | { 14 | "Name": "age", 15 | "Type": "Integer" 16 | } 17 | ] 18 | } 19 | } -------------------------------------------------------------------------------- /examples/06-custom-functions/javabean.tpl: -------------------------------------------------------------------------------- 1 | public class User implements java.io.Serializable { 2 | {{range .User.Property }} 3 | private {{.Type}} {{ .Name }}; 4 | {{end}} 5 | 6 | 7 | {{range .User.Property }} 8 | public {{.Type}} {{ .Name | ToGetterName }}(){ 9 | return this.{{.Name}}; 10 | } 11 | {{end}} 12 | 13 | {{range .User.Property }} 14 | public void {{ .Name | ToSetterName }}({{.Type}} {{.Name}}){ 15 | this.{{.Name}} = {{.Name}}; 16 | } 17 | {{end}} 18 | } -------------------------------------------------------------------------------- /examples/07-multiplefiles/mailing.generated.txt: -------------------------------------------------------------------------------- 1 | 2 | Dear , 3 | 4 | Hello, 5 | 6 | We would like to assign some tasks for project : 7 | 8 | 9 | Can we plan a meeting on ? 10 | 11 | Regards, 12 | -------------------------------------------------------------------------------- /examples/07-multiplefiles/mailing.json: -------------------------------------------------------------------------------- 1 | { 2 | "Files": [{ 3 | "FileName": "test1", 4 | "Data": { 5 | "Name": "phil", 6 | "Date": "01/01/2018", 7 | "Project": "Go-Template", 8 | "Topics": [ 9 | "write documentation", 10 | "publish to github.com", 11 | "add more examples" 12 | ] 13 | } 14 | }, 15 | { 16 | "FileName": "test2", 17 | "Data": { 18 | "Name": "john", 19 | "Date": "01/01/2018", 20 | "Project": "Go-Template", 21 | "Topics": [ 22 | "write documentation", 23 | "publish to github.com", 24 | "add more examples" 25 | ] 26 | } 27 | }, 28 | { 29 | "FileName": "test3", 30 | "Data": { 31 | "Name": "peter", 32 | "Date": "01/01/2018", 33 | "Project": "Go-Template", 34 | "Topics": [ 35 | "write documentation", 36 | "publish to github.com", 37 | "add more examples" 38 | ] 39 | } 40 | }] 41 | } -------------------------------------------------------------------------------- /examples/07-multiplefiles/mailing.tpl: -------------------------------------------------------------------------------- 1 | 2 | Dear {{.Name}}, 3 | 4 | Hello, 5 | 6 | We would like to assign some tasks for {{.Project}} project : 7 | {{range .Topics}} 8 | - {{.}} 9 | {{end}} 10 | 11 | Can we plan a meeting on {{.Date}} ? 12 | 13 | Regards, 14 | -------------------------------------------------------------------------------- /examples/07-multiplefiles/test1: -------------------------------------------------------------------------------- 1 | 2 | Dear phil, 3 | 4 | Hello, 5 | 6 | We would like to assign some tasks for Go-Template project : 7 | 8 | - write documentation 9 | 10 | - publish to github.com 11 | 12 | - add more examples 13 | 14 | 15 | Can we plan a meeting on 01/01/2018 ? 16 | 17 | Regards, 18 | -------------------------------------------------------------------------------- /examples/07-multiplefiles/test2: -------------------------------------------------------------------------------- 1 | 2 | Dear john, 3 | 4 | Hello, 5 | 6 | We would like to assign some tasks for Go-Template project : 7 | 8 | - write documentation 9 | 10 | - publish to github.com 11 | 12 | - add more examples 13 | 14 | 15 | Can we plan a meeting on 01/01/2018 ? 16 | 17 | Regards, 18 | -------------------------------------------------------------------------------- /examples/07-multiplefiles/test3: -------------------------------------------------------------------------------- 1 | 2 | Dear peter, 3 | 4 | Hello, 5 | 6 | We would like to assign some tasks for Go-Template project : 7 | 8 | - write documentation 9 | 10 | - publish to github.com 11 | 12 | - add more examples 13 | 14 | 15 | Can we plan a meeting on 01/01/2018 ? 16 | 17 | Regards, 18 | -------------------------------------------------------------------------------- /examples/08-subtemplate/template.generated.txt: -------------------------------------------------------------------------------- 1 | 2 | - labels: 3 | app: nginx 4 | street: 5 | - labels: 6 | app: nginx 7 | name: John Doe 8 | street: 9 | - labels: 10 | app: nginx 11 | street: Main st 8 NY -------------------------------------------------------------------------------- /examples/08-subtemplate/template.json: -------------------------------------------------------------------------------- 1 | { 2 | "Name":"John Doe", 3 | "Address" : { 4 | "Street" : "Main st 8", 5 | "City": "NY" 6 | } 7 | } -------------------------------------------------------------------------------- /examples/08-subtemplate/template.tpl: -------------------------------------------------------------------------------- 1 | 2 | {{- template "mydomain.subchart" }} 3 | {{- template "mydomain.subchart" . }} 4 | {{- template "mydomain.subchart" .Address }} 5 | 6 | 7 | 8 | {{- define "mydomain.subchart" }} 9 | - labels: 10 | app: nginx 11 | {{- if .Name}} 12 | name: {{ .Name }} 13 | {{- end}} 14 | street: {{ .Street }} {{ .City }} 15 | {{- end }} -------------------------------------------------------------------------------- /src/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/phcollignon/Go-Template 2 | 3 | go 1.15 4 | 5 | require ( 6 | github.com/Masterminds/goutils v1.1.0 // indirect 7 | github.com/Masterminds/semver v1.5.0 // indirect 8 | github.com/Masterminds/sprig v2.22.0+incompatible 9 | github.com/ghodss/yaml v1.0.0 10 | github.com/google/uuid v1.1.2 // indirect 11 | github.com/huandu/xstrings v1.3.2 // indirect 12 | github.com/imdario/mergo v0.3.11 // indirect 13 | github.com/mitchellh/copystructure v1.0.0 // indirect 14 | github.com/stretchr/testify v1.6.1 // indirect 15 | golang.org/x/net v0.0.0-20201031054903-ff519b6c9102 16 | ) 17 | -------------------------------------------------------------------------------- /src/go.sum: -------------------------------------------------------------------------------- 1 | github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg= 2 | github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= 3 | github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww= 4 | github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= 5 | github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZCSYp4Z0m2dk6cEM60= 6 | github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o= 7 | github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 8 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 9 | github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= 10 | github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= 11 | github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y= 12 | github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 13 | github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw= 14 | github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= 15 | github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA= 16 | github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= 17 | github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ= 18 | github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= 19 | github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY= 20 | github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= 21 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 22 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 23 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 24 | github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= 25 | github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 26 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 27 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= 28 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 29 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 30 | golang.org/x/net v0.0.0-20201031054903-ff519b6c9102 h1:42cLlJJdEh+ySyeUUbEQ5bsTiq8voBeTuweGVkY6Puw= 31 | golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= 32 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 33 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 34 | golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 35 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 36 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 37 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 38 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 39 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 40 | gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= 41 | gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 42 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= 43 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 44 | -------------------------------------------------------------------------------- /src/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "github.com/ghodss/yaml" 6 | "flag" 7 | "fmt" 8 | "io/ioutil" 9 | "os" 10 | "path" 11 | "path/filepath" 12 | "strings" 13 | "github.com/Masterminds/sprig" 14 | "text/template" 15 | "bytes" 16 | "unicode" 17 | ) 18 | 19 | type MultipleFileData struct { 20 | Files []struct { 21 | FileName string 22 | Data interface{} 23 | } 24 | } 25 | 26 | func main() { 27 | 28 | dataFileName := flag.String("d", "", "json or yaml data file") 29 | templateFileName := flag.String("t", "", "go template file") 30 | outputDirectory := flag.String("o", ".", "output directory") 31 | multipleFiles := flag.String("m", "", "-m multi : generates one file for each File object in Json (or Yaml) data file") 32 | flag.Parse() 33 | 34 | flag.Usage = func() { 35 | fmt.Printf("Usage of %s:\n", os.Args[0]) 36 | fmt.Printf(" gocodegen -d data-file (Json or Yaml) -t template-file [-o output-directory] [-m multiple-files] \n") 37 | flag.PrintDefaults() 38 | os.Exit(1) 39 | } 40 | 41 | if *dataFileName == "" || *templateFileName == "" { 42 | flag.Usage() 43 | } 44 | 45 | hasMultipleFiles := (*multipleFiles != "") && (*multipleFiles == "multi") 46 | 47 | template, err := template.New(path.Base(*templateFileName)).Funcs(template.FuncMap(MyFuncMap)).Funcs(sprig.TxtFuncMap()).ParseFiles(*templateFileName) 48 | if err != nil { 49 | fmt.Println(err) 50 | } 51 | 52 | dataFile, err := os.Open(*dataFileName) 53 | if err != nil { 54 | fmt.Println(err) 55 | } 56 | 57 | defer dataFile.Close() 58 | 59 | byteValue, err := ioutil.ReadAll(dataFile) 60 | if err != nil { 61 | fmt.Println(err) 62 | } 63 | byteValue,err = ToJSON(byteValue) 64 | 65 | if hasMultipleFiles { 66 | var multidata MultipleFileData 67 | 68 | err = json.Unmarshal(byteValue, &multidata) 69 | if err != nil { 70 | fmt.Println(err) 71 | } 72 | 73 | for _, mfile := range multidata.Files { 74 | outputFileName := path.Join(path.Dir(*dataFileName), mfile.FileName) 75 | mdata := mfile.Data 76 | generateFile(template, *outputDirectory, outputFileName, mdata) 77 | } 78 | 79 | } else { 80 | var data interface{} 81 | err = json.Unmarshal(byteValue, &data) 82 | if err != nil { 83 | fmt.Println(err) 84 | } 85 | outputFileName := strings.TrimSuffix(*dataFileName, filepath.Ext(*dataFileName)) + ".generated.txt" 86 | generateFile(template, *outputDirectory, outputFileName, data) 87 | } 88 | } 89 | 90 | func generateFile(template *template.Template, outputDirectory string, outputFileName string, data interface{}) { 91 | absOutputFileName := path.Join(outputDirectory, outputFileName) 92 | os.MkdirAll(path.Dir(absOutputFileName), os.ModePerm) 93 | outputFile, err := os.Create(absOutputFileName) 94 | fmt.Println("Generating file : " + absOutputFileName ) 95 | defer outputFile.Close() 96 | if err != nil { 97 | fmt.Println(err) 98 | } 99 | err = template.Execute(outputFile, data) 100 | if err != nil { 101 | fmt.Println(err) 102 | } 103 | } 104 | 105 | func ToJSON(data []byte) ([]byte, error) { 106 | if hasJSONPrefix(data) { 107 | return data, nil 108 | } 109 | return yaml.YAMLToJSON(data) 110 | } 111 | 112 | var jsonPrefix = []byte("{") 113 | 114 | func hasJSONPrefix(buf []byte) bool { 115 | trim := bytes.TrimLeftFunc(buf, unicode.IsSpace) 116 | return bytes.HasPrefix(trim, jsonPrefix) 117 | } 118 | 119 | -------------------------------------------------------------------------------- /src/my-funcs.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "strings" 5 | 6 | "golang.org/x/net/html" 7 | ) 8 | 9 | var MyFuncMap = map[string]interface{}{ 10 | "ToLower": strings.ToLower, 11 | "ToUpper": strings.ToUpper, 12 | "ToGetterName": ToGetterName, 13 | "ToSetterName": ToSetterName, 14 | "ToSelector": ToSelector, 15 | "ToClassName": ToClassName, 16 | "escapeHtml": EscapeHtml, 17 | "escapeQuote": EscapeQuote, 18 | "ToImport": ToImport, 19 | } 20 | 21 | func ToGetterName(name string) string { 22 | return "get" + strings.Title(name) 23 | } 24 | func ToSetterName(name string) string { 25 | return "Set" + strings.Title(name) 26 | } 27 | func ToSelector(name string) string { 28 | if name == "isEmail" { 29 | name = "isEmailAndGmail" 30 | } 31 | var first = string(name[2]) 32 | if strings.ToLower(string(name[3])) == string(name[3]) { 33 | first = strings.ToLower(string(name[2])) 34 | } 35 | 36 | return first + name[3:] + "Validator" 37 | } 38 | func ToClassName(name string) string { 39 | if name == "isEmail" { 40 | name = "isEmailAndGmail" 41 | } 42 | return strings.ToUpper(string(name[2])) + name[3:] + "ValidatorDirective" 43 | } 44 | func ToImport(name string) string { 45 | 46 | return name[:len(name)-3] 47 | } 48 | 49 | func EscapeHtml(name string) string { 50 | return html.EscapeString(name) 51 | } 52 | func EscapeQuote(name string) string { 53 | return strings.Replace(name, "'", "\\'", 1) 54 | } 55 | --------------------------------------------------------------------------------