Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Bikeshedding Casa: import with explicit format #2039

Closed
yannham opened this issue Sep 13, 2024 · 2 comments
Closed

The Bikeshedding Casa: import with explicit format #2039

yannham opened this issue Sep 13, 2024 · 2 comments

Comments

@yannham
Copy link
Member

yannham commented Sep 13, 2024

#2036 introduces a new syntax to explicitly specify the format of an imported file. Before, this would be just guessed from the extension of the file, which has obvious limits.

This issue aims at bikeshedding the final syntax of imports with an explicit format. The one proposed in the original PR is like:

let
  html = import 'Raw "foo.html",
  foo = import 'Json "foo.json.hidden",
in
...

However some flaws were reported during further discussions,, in particular that this makes it look like import is just a standard function, while it's not. Typically you can't partially apply it and you can't even apply it further if the stuff that is imported is a function, as in import 'Nickel "foo.ncl" some_value. This was precisely done to allow the kind of syntax extension discussed here without breaking backward-compat (we could have decided to go with import "foo.html" 'Raw for example, which is totally possible today)

Another proposed syntax was to use as, for example:

let
  html = import "foo.html" as raw,
  foo = import "foo.json.hidden" as json,
in
...

It reads quite naturally, and should be doable without introducing new keywords (once again, thanks to making import special in the parser some time ago). A variant would be to use some form of qualifier, like a protocol, for example import json:"foo.json"

What other languages do

Most other config languages (Jsonnet, CUE, KCL, Nix, etc.) that we looked at usually have a read or readFile builtin that first reads the whole thing as a string, and then they just use a deserialization/unmarshaling/parsing function in a second step (would correspond to Nickel' std.deserialize).

Dhall seems to be the only one with a proper import syntax (or at least something similar to guide the interpretation of an imported blob), and they use the as keyword as well: https://docs.dhall-lang.org/tutorials/Language-Tour.html#importing-raw-text.

Webpack has something similar, with the following syntax: import 'file.txt?raw'. However there's an obvious risk of collision with filenames that contain ?. We can also imagine allowing URLs one day, and the collision will even be worse, so it's probably better to keep the format specification well separated from the file name.

@vi
Copy link
Contributor

vi commented Sep 13, 2024

Maybe even with postfixed as, it should still style it as enum tags that match std.deserialize (where possible), to signify that it is a growing list?

It would look like this: import "foo.html" as 'Raw

@yannham
Copy link
Member Author

yannham commented Oct 17, 2024

Implemented in #2070, following @vi's last suggestion. Changed raw to text though, including on the CLI (where raw is still supported but deprecated).

@yannham yannham closed this as completed Oct 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants