Proposal: an attribute to embed file data at compile time #2269
Replies: 5 comments 17 replies
-
This is intriguing, I wonder if it could be extended to accept a parser, think about the possibility of having views in HTML and mustache-tags replaced by actual gleam variables. |
Beta Was this translation helpful? Give feedback.
-
I think |
Beta Was this translation helpful? Give feedback.
-
Hmmm. So I think this would be a great feature and use of the new attribute syntax. I think I lean toward a very minimal version of this as opposed to some of the ideas above. I like the idea of bitstring being the only really acceptable use. That way the only error case possible is that it can't find the file (feel free to point out the other error cases I'm missing). Maybe add an I think the way to make this syntax make sense is to actually append the bitstrings, so
where the file I think the only other question was the filepath relative location. I think it should be relative to |
Beta Was this translation helpful? Give feedback.
-
Allowing arbitrary relative paths are an attack vector if we let the break package boundaries. I'd bet good money on the most-common operation being an immediate |
Beta Was this translation helpful? Give feedback.
-
I am now more uncertain if I would want this to be part of the Is this for embedding arbitrary binary data? To say include small PNGs in source code? Otherwise could this be part of a heredoc/nowdoc syntax? heredoc via file
inlined heredoc:
If we want to also include non string binaries:
|
Beta Was this translation helpful? Give feedback.
-
Following up on some discussions that were had in discord, this proposal entails the addition of the
@embed
attribute (name open for discussion, but for convenience here I will use the aforementioned attribute name), inspired by go's//go:embed
directive that was added ingo 1.16
.The goal
To add a way to include file data in gleam code at compile-time rather than depending on accessing the filesystem and hoping that certain files exist at runtime.
Scope
This proposal is limited to the inclusion of single files, i.e. 1-1 mappings of files to gleam constants, this is a subset of the functionality that go's
embed
directive provides. We aren't going to consider embedding filesystems or directories for the time being.The syntax
The impacts on gleam's syntax here (aside from the addition of the
@embed
attribute) involves 1 potential change (examples 3 and 4 below), or none at all.Given some file
foo.txt
existing on the build computer and a string constantbar
, I would expect an embed to look like:Considerations
String
seems an obvious choice, but what if a file is not guaranteed to be utf-8 encoded? We might want to allowBitString
as an option. If a constant is declared with@embed
asString
and is not valid utf-8, should the compiler fail and report an error? If a constant is declared with@embed
asBitString
should compilation succeed so long as the file is found and can be read successfully?Having the value present in examples 1 & 2 is consistent with gleam's current
const
syntax, and might be helpful if we want to consider default values for embedded files, with a warning at compile-time if the file is not found. The problem with this is we now have different behaviours for an embed when it has a value other than""
or<<>>
.My personal recommendation is as follows:
I would personally prefer to not need to provide a value for constants that use the
@embed
attribute (as show in examples 3 & 4). However, the change of allowing a value to not be provided for a constant differs from gleam's current syntax and likely has further ramifications with regards to how parsing the language is done. If allowing a value to not be provided is deemed too complicated or not a worthwhile change, then I am in favour of requiringString
values decorated with@embed
to always be given a value of""
and for BitString values decorated with@embed
to always be given a value of<<>>
.Should paths be relative to the gleam file that contains the
const
with@embed
, or should they be relative to the root of the project (wheregleam.toml
is)?Behaviour
@embed
and the file is not present at the specified path, compilation fails and an error is returned to the user explaining that the file was not found.const
Beta Was this translation helpful? Give feedback.
All reactions