1
1
mirror of https://github.com/tweag/nickel.git synced 2024-08-16 23:20:38 +03:00
nickel/examples
Pim Snel d918a7c02e
Add two examples: imports and foreach pattern (#1387)
* add install info using homebrew on macos

* Update README.md

Fix markdownlint-cli not being happy

* add two examples: imports and foreach pattern

* changes according to review suggestions

* Format new Nickel examples

* Add missing test annotations to examples

* Fix failing example annotations

* Make sure the test TOML parser doesn't choke

---------

Co-authored-by: Yann Hamdaoui <yann.hamdaoui@gmail.com>
Co-authored-by: Yann Hamdaoui <yann.hamdaoui@tweag.io>
Co-authored-by: Viktor Kleen <vkleen+github@17220103.de>
2023-07-06 11:47:21 +00:00
..
arrays Add topiary to pre-commit-hooks (#1391) 2023-06-22 09:41:50 +00:00
config-gcc Add topiary to pre-commit-hooks (#1391) 2023-06-22 09:41:50 +00:00
fibonacci Add topiary to pre-commit-hooks (#1391) 2023-06-22 09:41:50 +00:00
foreach-pattern Add two examples: imports and foreach pattern (#1387) 2023-07-06 11:47:21 +00:00
imports Add two examples: imports and foreach pattern (#1387) 2023-07-06 11:47:21 +00:00
merge Add topiary to pre-commit-hooks (#1391) 2023-06-22 09:41:50 +00:00
merge-priorities Add topiary to pre-commit-hooks (#1391) 2023-06-22 09:41:50 +00:00
polymorphism Add topiary to pre-commit-hooks (#1391) 2023-06-22 09:41:50 +00:00
record-contract Add topiary to pre-commit-hooks (#1391) 2023-06-22 09:41:50 +00:00
simple-contracts Add topiary to pre-commit-hooks (#1391) 2023-06-22 09:41:50 +00:00
README.md Err on the side of too much syntax highlighting (#1295) 2023-05-04 15:44:20 +00:00

Examples

This directory contains an evolving selection of examples of Nickel programs.

Execution

Please follow the main repository's README instructions to have a working nickel executable. You can then either use the command line or launch an interactive session. Play with the values to see contracts failing when fed with invalid data!

From the command line

  • Base values: some examples just return numbers, strings or booleans. You can run them directly:

    $ nickel -f fibonacci.ncl
    55
    
  • Configurations: some examples return records representing configuration. You can run them directly as well, or use the export subcommand to see the result serialized as JSON, which might be more readable:

    $ nickel -f merge-main.ncl export
    {
      "firewall": {
      ...
    }
    

    Alternatively, you can query individual elements of a configuration, showing documentation and other metadata:

    $ nickel -f record-contract.ncl query kind
      • contract: [|'ReplicationController, 'ReplicaSet, 'Pod|]
      • documentation: The kind of the element being configured.
    

From the REPL

First start the REPL:

$ nickel repl
nickel>
  • Just import a file directly to evaluate it:

    nickel> import "fibonacci.ncl"
    55
    
    nickel>
    
  • Use std.serialize to have the same behavior as the export subcommand and print the result as JSON:

    nickel> builtin.serialize 'Json (import "merge-main.ncl")
    "{
        \"firewall\": {
        ...
    }"
    
    nickel>
    
  • Use :query to retrieve information and documentation about a field:

    nickel> let config = import "record-contract.ncl"
    nickel> :query config.kind
    • contract: [| 'ReplicationController, 'ReplicaSet, 'Pod |]
    • documentation: The kind of the element being configured.
    
    nickel>