1
1
mirror of https://github.com/tweag/nickel.git synced 2024-09-11 03:35:37 +03:00
nickel/examples
Yann Hamdaoui 18ee89b644
Split contracts into an immediate part and a delayed part (#1975)
* Add immediate/delayed notion to the manual

* Split custom contracts in immediate and delayed part

This commit explicitly split the custom contract representation and
constructors into an immediate part, similar to a validator, and a
delayed part, which is a partial identity. The stdlib and primops are
adapted as well (`%contract/custom%` now takes two arguments, the two
parts, and `%contract/from_xxx%` operators aren't used anymore as they
are subsumed by `%contract/custom%`).

* Rework prepare_contract to be more low-level and use null comparison

* Make `%contract/get_xxx%` panic on a Type

In the long run, we want that `%contract/get_immediate%` and
`%contract/get_delayed%` works as well on types, by calling to the right
function from the internals module. However, this requires deeper
changes, and is left for future work. Since those primops have just been
introduced, there's no reason they'd be used already in the wild, so for
now, we make the implemention panic if we try to apply them to a Type.

* Update stdlib doc, manual and examples

Update various documentation to reflect the new custom contract
constructor with an immediate and a delayed part.

* Update config-gcc example with new custom contract builder

* Fix tests

* Implement review suggestions

Co-authored-by: jneem <joeneeman@gmail.com>

* Review suggestion: use concrete example for delayed checks

* Review suggestion: deduplicate custom contracts in-code doc

* Update Topiary input to avoid CI error (gitsavannah)

---------

Co-authored-by: jneem <joeneeman@gmail.com>
2024-07-03 09:38:00 +00:00
..
arrays Add documentation for extended patterns (#1924) 2024-06-07 08:58:39 +00:00
config-gcc Split contracts into an immediate part and a delayed part (#1975) 2024-07-03 09:38:00 +00:00
fibonacci Add documentation for extended patterns (#1924) 2024-06-07 08:58:39 +00:00
foreach-pattern Update/refresh examples using latest Nickel idioms (#1849) 2024-03-12 14:30:09 +00:00
imports Fix documentation (#1844) 2024-03-11 08:42:44 +00:00
merge Update/refresh examples using latest Nickel idioms (#1849) 2024-03-12 14:30:09 +00:00
merge-priorities Update/refresh examples using latest Nickel idioms (#1849) 2024-03-12 14:30:09 +00:00
polymorphism Add documentation for extended patterns (#1924) 2024-06-07 08:58:39 +00:00
record-contract Add documentation for extended patterns (#1924) 2024-06-07 08:58:39 +00:00
simple-contracts Introduce validators for building custom contracts (#1970) 2024-06-24 13:19:06 +00:00
README.md Fix documentation (#1844) 2024-03-11 08:42:44 +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 eval 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 export merge-main.ncl
    {
      "firewall": {
      ...
    }
    

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

    $ nickel query --field kind record-contract.ncl
      • 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>