sml

SML { ❄ }

Downloads Questions, bug reports, or just to say hi: sal@mail.swebase.cn 中文 EN

Chapter 6: Environment Variables and Escaping

This chapter discusses two small things that make configuration more secure and universal: environment variable injection andstring escaping.

6.1 Environment variables: $env.VAR

There are often sensitive values (API Key, password) or values that vary with the environment (domain name, port) in the configuration. Writing these in files is neither secure nor flexible. SML uses $env.VAR to read from environment variables during parsing:

secrets {
    resendApiKey: $env.RESEND_API_KEY
    dbPassword: $env.DB_PASSWORD
    optionalWebhook: $env.UNSET_WEBHOOK   # Unset -> empty string, no error
}

main points:

-$env.VAR is locally replaced with the value of the environment variable (replaced with a string) during parsing.

-When the variable is not set, the result is an empty string**, and there will be no error (so you can write optional options with confidence).

-This configuration can be securely submitted to the repository, and the key only exists in the variables of the runtime environment.

Equivalent JSON must write plaintext to death; SML decouples “configuration” from “key”.

6.2 String Escaping

Common escaping supported in quotation string:

Escaping Meaning
\n Line Break
\t Tab
\\ Backslash itself
\" Quotation marks themselves
\u{XXXX}/\uXXXX Unicode code point, converted to UTF-8

Example:

banner: "SML \u{1F680} go online \nthe second line\ttabulation"
label: "snowflake \u{2744} snow"
path: "C:\\Program Files\\app"

\u{2744} will be parsed into snowflake characters ❄.

Escaping only takes effect within the quoted string. Bare words (values without quotation marks) do not support escaping, and special characters are enclosed in quotation marks.

6.3 Give it a try with your hands

  1. Write configuration reference environment variables:
   database {
       url: $env.DATABASE_URL
       pool: 16
   }
  1. Set DATABASE_URL=postgres://localhost/app before running the parser, and confirm that url is correctly filled in the parsing result.

  2. Try a field with emoji:

   greeting: "你好 \u{1F44B}"

→ Chapter 7: Multilingual Use

Hands on practice

After reading this chapter, directly modify SML in the editor below and click “Run” to immediately see the parsing results or validation errors - having output is necessary for efficient learning.

✍ Hands-on exercise Write an secrets block, including apiKey: $env.RESEND_API_KEY and dbPassword: $env.DB_PASSWORD; Write another banner string using \u{1F680} to escape emojis.
💡 Hint: It is not possible to set real environment variables in the browser, so $env.X will parse as an empty string - this is the expected behavior, and validation will determine based on this.
✍ Self-check quiz: Chapter 6 Self testing: Environment Variables and Escaping Score 0 / 4
Q1. What syntax is used to inject environment variables into values?
Q2. What does $env.X usually parse into when running Playground in a browser?
Q3. Judgment: \u{1F680} will be escaped as an emoji character in the string.
Q4. Which escape is supported by SML?