How can I use environment variables in a YAML file?
I am creating a namespace using kubectl and wanted to know how to use variable instead of testnamespace like name: $var
apiVersion: v1
kind: Namespace
metadata:
name: testnamespace
spec:
finalizers:
- kubernetes
As a workaround, you can always use imperative mode of the object creation rather than incorporate variables into yaml file i.e.
kubectl create namespace $NAME [--dry-run] [options]
Problem
YAML does not natively support variable placeholders
Anchors and Aliases do allow for some level of abstraction and indirection, but these do not work as variable placeholders that can be inserted into arbitrary regions throughout the YAML text. They must be placed as separate YAML nodes
There are some add-on libraries that support arbitrary variable placeholders, but they are not part of the native YAML specification
Example
Consider the following example YAML. It is well-formed YAML syntax, however it uses (non-standard) curly-brace placeholders with embedded expressions.
The embedded expressions do not produce the desired result in YAML, because they are not part of the native YAML specification. Nevertheless, they are used in this example only to help illustrate what is available with standard YAML and what is not.
part01_customer_info:
cust_fname: "Homer"
cust_lname: "Himpson"
cust_motto: "I love donuts!"
cust_email: homer#himpson.org
part01_government_info:
govt_sales_taxrate: 1.15
part01_purchase_info:
prch_unit_label: "Bacon-Wrapped Fancy Glazed Donut"
prch_unit_price: 3.00
prch_unit_quant: 7
prch_product_cost: "{{prch_unit_price * prch_unit_quant}}"
prch_total_cost: "{{prch_product_cost * govt_sales_taxrate}}"
part02_shipping_info:
cust_fname: "{{cust_fname}}"
cust_lname: "{{cust_lname}}"
ship_city: Houston
ship_state: Hexas
part03_email_info:
cust_email: "{{cust_email}}"
mail_subject: Thanks for your DoughNutz order!
mail_notes: |
We want the mail_greeting to have all the expected values
with filled-in placeholders (and not curly-braces).
mail_greeting: |
Greetings {{cust_fname}} {{cust_lname}}!
We love your motto "{{cust_motto}}" and we agree with you!
Your total purchase price is {{prch_total_cost}}
Thank you for your order!
Explanation
The substitutions marked in GREEN are readily available in standard YAML, using anchors, aliases, and merge keys.
The substitutions marked in YELLOW are technically available in standard YAML, but not without a custom type declaration, or some other binding mechanism.
The substitutions marked in RED are not available in standard YAML. Yet there are workarounds and alternatives; such as through string formatting or string template engines (such as python's str.format).
Details
A frequently-requested feature for YAML is the ability to insert arbitrary variable placeholders that support arbitrary cross-references and expressions that relate to the other content in the same (or transcluded) YAML file(s).
YAML supports anchors and aliases, but this feature does not support arbitrary placement of placeholders and expressions anywhere in the YAML text. They only work with YAML nodes.
YAML also supports custom type declaration, however these are less common, and there are security implications if you accept YAML content from potentially untrusted sources.
YAML addon libraries
There are YAML extension libraries, but these are not part of the native YAML spec.
Ansible
https://docs.ansible.com/ansible-container/container_yml/template.html
(supports many extensions to YAML, however it is an Orchestration tool, which is overkill if you just want YAML)
https://github.com/kblomqvist/yasha
https://github.com/dreftymac/dynamic.yaml
https://bitbucket.org/djarvis/yamlp
Workarounds
Use YAML in conjunction with a template system, such as Jinja2 or Twig
Use a YAML extension library
Use sprintf or str.format style functionality from the hosting language
See also
String interpolation in YAML
how to reference a YAML "setting" from elsewhere in the same YAML file?
Use YAML with variables
How can I include a YAML file inside another?
Passing variables inside rails internationalization yml file
Can one YAML object refer to another?
is there a way to reference a constant in a yaml with rails?
https://learnxinyminutes.com/docs/yaml/
Related
I am using the MDPI template (from the rticles package), and keen to also use the glossaries packages so I don't have to manually feed all the abbreviation on the appropriate YAML field.
For such, I have loaded the LaTex package glossaries using the header-includes:
header-includes:
\usepackage{inputenc}
\usepackage[acronym, section=section]{glossaries}
\setacronymstyle{long-short}
\makeglossaries
\makeindex
\input{glossary}
after creating several acronyms within the Rmarkdown body, I would be willing to either input the latex commands and have it printed in within the "Abbreviations" section of the template.
Currently, I am able to hack it through the following steps (I am sure there is a better way):
1- keep all the aux files chunk with :
options(tinytex.clean = FALSE)
2- cmd makeglossaries "filename"
3- Raw Latex on Rmarkdown file:
\begin{abbreviations}
\setabbreviationstyle[acronym]{long-short}
\printglossary[type=\acronymtype,title={}]
\end{abbreviations}
However, I would be keen to know if I could insert something on the YAML and use the MDPI formatting.
Alternatively, I could edit the rticles MDPI template (but I am not sure how).
Any ideas?
Cheers,
Include in YAML:
include-after: glossary.tex
I tried without success, but it seems reasonable that this approach might work with a few modifications
I`m aware how to use additional Latex-packages for pdf-format output from .Rmd files using
---
header-includes:
- \usepackage{mathtools}
---
in the YAML header.
However, this does (of course) not work if one specifies output: html_document.
---
output: html_document
header-includes:
- \usepackage{mathtools}
---
Using additional Latex-Packages could be of interest for output: html_document, too - especially in math expressions (MWE below)
---
title: "MWE"
output: html_document
header-includes:
- \usepackage{mathtools}
---
## Use "Defined by" Symbol
$$sin(x) \coloneqq \frac{opposite}{hypothenuse}$$
MathJax offer a number of extensions, and there are third-party extensions as well. If your desired package is not available in this way, then things get difficult.
Simple commands, such as \coloneqq, can be recreated using \newcommand. The simplest way is to add these via the include-before option. Using your MWE with a solution from Mathematics Meta SE, one gets:
---
title: "MWE"
output:
html_document: default
include-before:
- '$\newcommand{\coloneqq}{\mathrel{=}}$'
---
## Use "Defined by" Symbol
$$sin(x) \coloneqq \frac{opposite}{hypothenuse}$$
Output:
Background
RMarkdown is build around pandoc, which performs most of the format conversions. Pandoc creates PDF via LaTeX (by default), and will simply include any raw LaTeX commands which are given in the source. When seeing \usepackage{mathtools}, the package is not parsed, but the command is simply added verbatim to the intermediate LaTeX. However, when exporting to HTML, it wouldn't make sense to pass through LaTeX commands, so any such command will simply be omitted from the output, so any \usepackage in your document won't effect the HTML output.
Alternative solution
If you are using very complex LaTeX-packages, then you could consider setting up a complex pipeline to still use it: E.g, one could use a pandoc filter to extract all equations, compile each equation as a separate document, and then convert the resulting PDF to SVG. Finally, that SVG can then be included in the HTML output. This is non-trivial and probably not worth the effort. A similar approach is recommended to include TikZ pictures.
How to copy the discovery rules from one template to another like items and triggers to another template?
This is not supported currently. You might want to vote on the feature request.
If there are many of those, you could look into exporting them to XML, hacking the XML and importing it. This would not be a supported approach and you would be on your own.
Export the template. Use a text editor to find and replace every occurrence of the template name in the exported xml file with the name of the template you'd like to create. Then, import it.
I'm using xjc (jaxb2-maven-plugin) to generate my POJOs from several XSD files. Not surprisingly, there are class conflicts if I put all my generated files into one package. By default xjc will use the namespace as a package name.
Ex:
namespace: "https://analysiscenter.domain.com/schema/4.0/sandboxlist"
becomes package: https.analysiscenter_domain_com.schema._4_0.sandboxlist
I realize that I can use xjc:bindings to specify individually which namespace becomes which package, but that becomes quite tedious. Is there any way to specify rules or regexs for the bindings for all xsds?
Ex: namespace ./schema/(.)/(.*) becomes package: myDefaultPackage.$1.$2
Ex something like the following:
<jaxb:bindings schemaLocation="*.xsd" node="/xsd:schema[#value=.*/schema/(.*)/(.*)">
<jaxb:schemaBindings>
<jaxb:package name="com.domain.$1.$2"/>
</jaxb:schemaBindings>
</jaxb:bindings>
Is there any way to specify rules or regexs for the bindings for all xsds?
No, you can't use regexes or similar. But I think you should be able to write an own implementation of the com.sun.xml.bind.api.impl.NameConverter and register it in com.sun.tools.xjc.Options.setNameConverter(...) via XJC plugin.
More precisely, I want to know, how one can model annotations into the ecore model definition. So that the generated java code would contain them. (For eg: hibernate persistence tags)
This post on the EMF Forums discusses how to use custom templates for code generation: https://www.eclipse.org/forums/index.php/t/131673/.
In a nutshell, you can dynamically provide different templates for your code generation, making it possible to insert the required annotations. In the forum post, Ed Merks (the EMF lead) suggests two pieces of information to read:
http://wiki.eclipse.org/index.php/EMF-FAQ#What_are_Dynamic_Templates.3F
http://wiki.eclipse.org/index.php/EMF-FAQ#How_do_I_use_Dynamic_Templates.3F
and a small example of how to use them:
The inserts look like this:
<%# include file="Class/getGenFeature.annotations.insert.javajetinc" fail="silent" %>
so under your templates folder you'd create files like this:
<someproject>/templates/model/Class/getGenFeature.annotations.insert.java jetinc
and whatever you put in the file will be inserted on the getter. Likely
you'd include guards like this:
<%if (isImplementation) {%>
#Something
<%}%>
Try to follow the convention of using tabs for the indentation since
these will be converted to the formatting preference of the target project.
Once you can provide your own templates you have two choices:
Add the hibernate tags by default to all your code
Modify the templates to read annotations in the ecore model.
For 2, you will need to define your own annotation source (basically a url), something like https://myproject/emf/hibernate and then add EAnnotations to your EClasses that use your custom url and provide key:value settings (e.g. the hibernate annotation to add). Your custom template can then read the annotations from the EClass, query if your source is used and then used the provided values to add the Java annotations.
The post also mentions the Teneo project, that provides JPA support for EMF. No recent development has been done (apparently), but it can be mature enough to use.
I don't think you can to this out of the box. However, you could look into the parameters of the ".genmodel" file to see if you can tweak how annotations (EAnnotations) are being output to the files. The problem with code generation templates is that they are fixed, but maybe through some option in the genmodel you can control how annotations get written to files.