Problems with Text Template

99 views
Skip to first unread message

Yamil Bracho

unread,
Jun 14, 2019, 6:09:28 PM6/14/19
to golang-nuts
I am building a kind of code generator based in golang templates and I have problems with some characters.

For example if I have got this line in my template
public class {{.Name}}Validator : AbstractValidator<{{.Name}}Input>


is converted to this in my output file
public class GetLoginMarketColorSettingByLoginIDValidator : AbstractValidator&lt; GetLoginMarketColorSettingByLoginIDInput >


Another line when I have got problems :
RuleFor(x => x.{{.Name}}).Cascade(CascadeMode.StopOnFirstFailure){{.ValidationString}};


And ValidationString is build in my code and should be (.NotEmpty().Length(1,15).WithMessage("Not a Valid LoginID");)


But the generated code for this line is
RuleFor(x => x.LoginID).Cascade(CascadeMode.StopOnFirstFailure).NotEmpty().Length(1,15).WithMessage(&#34;Not a Valid LoginID&#34;);


It seems that the template system apply url encoding or it gets confused when a special character is near substitution tag ({{}})


Any hint to solve this problem ?


TIA,
Yamil

Burak Serdar

unread,
Jun 14, 2019, 7:07:24 PM6/14/19
to Yamil Bracho, golang-nuts
On Fri, Jun 14, 2019 at 4:09 PM Yamil Bracho <yamil...@gmail.com> wrote:
>
> I am building a kind of code generator based in golang templates and I have problems with some characters.
>
> For example if I have got this line in my template
> public class {{.Name}}Validator : AbstractValidator<{{.Name}}Input>

Are you using html/template by any chance?

If so, try text/template instead.

>
>
> is converted to this in my output file
> public class GetLoginMarketColorSettingByLoginIDValidator : AbstractValidator&lt; GetLoginMarketColorSettingByLoginIDInput >
>
>
> Another line when I have got problems :
> RuleFor(x => x.{{.Name}}).Cascade(CascadeMode.StopOnFirstFailure){{.ValidationString}};
>
>
> And ValidationString is build in my code and should be (.NotEmpty().Length(1,15).WithMessage("Not a Valid LoginID");)
>
>
> But the generated code for this line is
> RuleFor(x => x.LoginID).Cascade(CascadeMode.StopOnFirstFailure).NotEmpty().Length(1,15).WithMessage(&#34;Not a Valid LoginID&#34;);
>
>
> It seems that the template system apply url encoding or it gets confused when a special character is near substitution tag ({{}})
>
>
> Any hint to solve this problem ?
>
>
> TIA,
> Yamil
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/834c04d1-0d21-4ccd-a1d5-d975f8246d0e%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Yamil Bracho

unread,
Jun 14, 2019, 9:28:03 PM6/14/19
to golang-nuts
Hi,
Yes, I am using text/template...

El viernes, 14 de junio de 2019, 19:07:24 (UTC-4), Burak Serdar escribió:
On Fri, Jun 14, 2019 at 4:09 PM Yamil Bracho <yamil...@gmail.com> wrote:
>
> I am building a kind of code generator based in golang templates and I have problems with some characters.
>
> For example if I have got this line in my template
> public class {{.Name}}Validator : AbstractValidator<{{.Name}}Input>

Are you using html/template by any chance?

If so, try text/template instead.

>
>
> is converted to this in my output file
> public class GetLoginMarketColorSettingByLoginIDValidator : AbstractValidator&lt; GetLoginMarketColorSettingByLoginIDInput >
>
>
> Another line when I have got problems :
> RuleFor(x => x.{{.Name}}).Cascade(CascadeMode.StopOnFirstFailure){{.ValidationString}};
>
>
> And ValidationString is build in my code and should be (.NotEmpty().Length(1,15).WithMessage("Not a Valid LoginID");)
>
>
> But the generated code for this line is
> RuleFor(x => x.LoginID).Cascade(CascadeMode.StopOnFirstFailure).NotEmpty().Length(1,15).WithMessage(&#34;Not a Valid LoginID&#34;);
>
>
> It seems that the template system apply url encoding or it gets confused when a special character is near substitution tag ({{}})
>
>
> Any hint to solve this problem ?
>
>
> TIA,
> Yamil
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.

Kurtis Rader

unread,
Jun 14, 2019, 11:20:11 PM6/14/19
to Yamil Bracho, golang-nuts
It is very unlikely you are using text/template. At least I can't reproduce the problem using text/template but can with html/template: https://play.golang.org/p/elqHI1EtfFa. Or you are using text/template but somewhere before executing that template your data is being converted to a HTML friendly form.

On Fri, Jun 14, 2019 at 6:28 PM Yamil Bracho <yamil...@gmail.com> wrote:
Yes, I am using text/template...

El viernes, 14 de junio de 2019, 19:07:24 (UTC-4), Burak Serdar escribió:

Are you using html/template by any chance?

If so, try text/template instead.

--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Yamil Bracho

unread,
Jun 17, 2019, 10:29:20 AM6/17/19
to golang-nuts
Yes, Kurtis you are absolutely right!!!

I just typed in "template.ParseFiles" and my editor made the changes for me in the imports and it imported "html/template" and i di not check this...

Thanks!!!
Reply all
Reply to author
Forward
0 new messages