-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add read_file
macro method
#6967
Conversation
I don't think the discussion will differ. Better to find a way to embed slices. That been said. I care more about API. So, if the macro would return a slice, even if the string is embedded as a first implementation, that is something I could live with. Until a better implementation without breaking the API will come. |
I've wanted to have this for a long time. Let's KISS and start with something easy to iterate on like this 👍 |
2 similar comments
I've wanted to have this for a long time. Let's KISS and start with something easy to iterate on like this 👍 |
I've wanted to have this for a long time. Let's KISS and start with something easy to iterate on like this 👍 |
Right now a string can contain any kind of bytes, so I think this is fine. If someone wants to embed a slice of bytes they can just do: DATA = {{ read_file("whatever") }}.to_slice |
I'd like a kwarg, to have a String (default) or Bytes (on demand), but maybe it would be confusing, and always having a String is better? {{ read_file("LICENSE.md") }} # => String
{{ read_file("logo.png", binary: true) }} # => Bytes |
I don't see a strong reason to have it for string, worst case it should be a |
The problem is that
In my mind it's just simpler to make |
The proper implementation should do something like #2791 (comment), so generate an AST subtree equivalent to the inline assembly call. As Brian points out, introducing it now to return a string literal would just force a breaking API change in the future. On a compromise, maybe we can do a text only version for now and call it |
If it returns an |
I'm not sure how I can present the argument any more clear than that, sorry. |
But |
Maybe it's the old discussion about what |
Also if we keep the concerns separate, that is read some text vs just give me the bytes, we can make the former pleasant to use at some point by applying any necessary encoding transformations. |
Then I guess a conclusion won't be reached here. I propose to close this then until someone figures out how to lay out everything related to strings and bytes. |
@jhass Having a |
So, if the macro name is changed and the review of @asterite regarding path is solved, this is GTG on my side. |
There's clearly two different things wanted here:
These are orthogonal and want different interfaces. The former can be |
Good point, thank you. I still think we should be cautious with introducing the first case while the second doesn't exist, because during that period people will abuse it for the second case. So I stick to preferring |
@RX14 The embedded data could be binary but also text. For text, it should simple to get a |
@straight-shoota you could just use |
Yeah, but it feels a bit weird to have |
That's a discussion we can have once we do |
But it's not the same thing, conceptually or in implementation. That was the whole point of my comment above. |
Both fits in the second bullet point. If I want to embed a file into the binary, I can use |
Any other language that lets you embed data using |
@asterite any language with Doing file embedding with The implementation doesn't matter anyway. |
NB: We should wait for 1 more core team re review before merging. And it should be merged with a squash. |
@Sija could squash it and add a nice commit message =) |
Merged :-) Thanks everybody for the discussion! |
It occured to me that perhaps it would be sensible to have nilable |
@Sija you mean that |
@bcardiff yep, exactly. Although I would (add |
@asterite true, yet it simplifies things in case like this: {% version = read_file("../../VERSION") %}
{{ version ? version.chomp : nil }} vs {{ read_file("../../VERSION").chomp }} |
{{ read_file("../../VERSION") || raise "Missing VERSION file" }} |
@straight-shoota nope, it ain't work since I'd need to use |
{{ (read_file("../../VERSION") || raise "Missing VERSION file").chomp }} |
👍 For a version that errors at compile time if the file doesn't exist (or I/O fails in any way). The second |
I agree with @woodruffw, @straight-shoota's workaround is good enough (though it still "hides" the root cause of not returning the file contents - which might be for instance permissions problem, not only missing file), yet having it "baked" into the method itself feels more natural (as long as there's nilable variant too). |
#2791, again! ;)
/cc @asterite