Avoid new break line in groovy template
Asked Answered
P

3

11

I have YAML file with my configuration name applications.yaml, this data will be my bindings:

applications:
- name: service1
  port: 8080
  path: /servier1
- name: service2
  port: 8081
  path: /service2

Then I have a template file applications.config:

<% applications.each { application ->  %>
ApplicationName: <%= application.name %>
<% } $ %>

And putting all together:

@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml

Yaml parser = new Yaml()
Map data = parser.load(("applications.yaml" as File).text)

String template_content = new File('applications.config').text
def binding = [applications: data.applications]

def template = new groovy.text.GStringTemplateEngine().createTemplate(template_content).make(binding)
println template.toString()

The issues is now: the output of this process is:


ApplicationName: service1

ApplicationName: service2

But I want this:

ApplicationName: service1
ApplicationName: service2

I do not know why are those extra spaces there. I will like to remove those but I do not see how or when or what is putting those new or breaking lines.

Thank you.

Pogy answered 19/7, 2019 at 23:55 Comment(4)
You tried putting the applications config template code all on one line?Paleontography
nope, I am trying to do is just one line after another without the extra spaces in the output. @tim_yates, as you can see the output, is adding new lines in the loop but I don't want that.Pogy
Yes that why I suggested removing the extra newlines from your templatePaleontography
Why everything in a single line, what happens if I want an if/else or nested loops? I will like to know if there is a way to avoid that extra-space, for example in jinja2 I can use {% my_value -%} where the - allows me to skip new lines but I will like to know if the template has something like that or a configuration.Pogy
A
5

What I observed (from answer, comments and some practice) is : All new Lines, we created inside the file applications.config are considered as new line in the output. As daggett said it is a default thing.

So Here I just want to show the possible config file format, where can be applied some conditional logic as u asked and looks ok for me. ex : if()

application.config :

<% applications.each { application ->
 if (application.valid) {%>\
Type :<%=application.valid%>
ApplicationName:<%=application.name%>
Path:<%=application.path%>
Port:<%=application.port%>
<%} else{%>\
--------------------
Found invalid Application : <%= application.name %>
--------------------\
<%}}%>

application.yaml

applications:
- name: service1
  port: 8080
  path: /servier1
  valid: true
- name: service2
  port: 8081
  path: /service2
  valid: false

code.groovy

@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml

Yaml parser = new Yaml()
Map data = parser.load(("applications.yaml" as File).text)

String template_content = new File('applications.config').text

def binding = [applications: data.applications]
def template = new groovy.text.GStringTemplateEngine().createTemplate(template_content).make(binding)
println template.toString()

Output :

Type :true
ApplicationName:service1
Path:/servier1
Port:8080
--------------------
Found invalid Application : service2
--------------------
Appaloosa answered 24/7, 2019 at 7:26 Comment(1)
Thank you both for your answers but this @Bhanuchander is the solution I was looking for. Using the \ worked really fine and also allows me to remove the newlines in other places.Pogy
A
2

look at your template for example from notepad++ (JSP language):

enter image description here

at the end of first line <% applications.each { application -> %> there is a CRLF that will be printed out before each Application

and in the next line another CRLF that will be printed after each Application.

so you have two CRLF between each two apps

Arthropod answered 23/7, 2019 at 15:29 Comment(3)
how do I remove those from my vscode? I am trying to remove those?Pogy
unfortunately, standard template does not support -%> to suppress new line. so, if you want to keep multiline template, then hide new-lines that should not be printed into <% ... %> code sections. Like that: CRLF %>Application: ... or use single-liner fot this case: <% applications.each { println "ApplicationName: $it" } %>Arthropod
So everything in a single line? what happens when I want to use if/else or a more complex structure? really a shame there is no way to avoid this!Pogy
H
0

The only way to avoid the newlines is to fix your template like below

<% applications.each { application ->  %>ApplicationName: <%= application.name %>
<% } $ %>

Results

$ groovy test.groovy
ApplicationName: service1
ApplicationName: service2

or handle the newlines in code like below

@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml

Yaml parser = new Yaml()
Map data = parser.load(("applications.yaml" as File).text)

String template_content = new File('applications.config').text
def binding = [applications: data.applications]

def template = new groovy.text.GStringTemplateEngine().createTemplate(template_content).make(binding)
def output = template.toString().replaceAll(/^\n|\n$/, "").replaceAll(/\n{2,}/,"\n")
println output

Results

$ groovy test.groovy
ApplicationName: service1
ApplicationName: service2
Hostess answered 24/7, 2019 at 14:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.