con2 / emrichen

A Template engine for YAML & JSON
MIT License
107 stars 11 forks source link

Typecasting? #49

Open lukeschlather opened 3 years ago

lukeschlather commented 3 years ago

I ran into an issue where I was trying to make a kubernetes manifest for a volumeMount and I wanted to make it configurable whether or not the volume was readOnly. I'm just using --include-env which means that when I provide the environment variable false it gets turned into the string 'false'.

I ended up doing

!Defaults
read_only_bool: !Op [!Var read_only, eq, 'true']

But it seems like it would be nice to have like a

read_only_bool: !Coerce [Boolean, !Var read_only]

That could apply standard yaml type guessing to the string and throw an error if it wasn't right (e.g. if you provided read_only=5 that would be a number and so would throw an error, or if you Coerced to numeric then you would end up with an error if you provided the string 'false'.)

japsu commented 3 years ago

Thank you for your suggestion.

We're likely to implement this as a !ToX family of tags (eg. !ToString 3.14, !ToInt value: "C0FFEE", radix: 16 etc.) as we already have !IsString etc.