1
1
mirror of https://github.com/tweag/nickel.git synced 2024-10-04 15:17:34 +03:00
nickel/examples
Yann Hamdaoui 05375af534 Officially restore the syntax for enum types
The enum types synax was temporary and somehow hidden. For v0.2.0, we
make it visible again. This commits also changes the syntax such that
alternatives must be prefixed with a backtick `, as for enum tags, for
more visual consistency.

This commit also updates the manual sections accordingly.
2022-07-29 10:41:26 +02:00
..
arrays replace // by # for comments 2022-03-03 11:28:59 +01:00
config-gcc update comment syntax in anual, rfcs and READMEs 2022-03-03 11:35:18 +01:00
fibonacci Implement let rec 2022-04-21 15:22:44 +02:00
merge replace // by # for comments 2022-03-03 11:28:59 +01:00
polymorphism replace // by # for comments 2022-03-03 11:28:59 +01:00
record-contract Officially restore the syntax for enum types 2022-07-29 10:41:26 +02:00
simple-contracts replace // by # for comments 2022-03-03 11:28:59 +01:00
README.md renaming list -> array 2022-03-02 16:38:46 +01: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
    Done: Num(55.0)
    
  • Configurations: some return records representing configuration. Because of laziness, Nickel won't currently print any readable result as is. Please use the export subcommand to see the result serialized as JSON:

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

    Alternatively, you can query a configuration or a sub-element to get a list of attributes or the documentation:

    $ nickel -f record-contract.ncl query kind
      • contract: <ReplicationController, ReplicaSet, Pod>
      • value: `ReplicationController
      • 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 builtin.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>
    • value: `ReplicationController
    • documentation: The kind of the element being configured.
    
    nickel>