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
Remote data #494
Remote data #494
Conversation
(Netlify is failing because it can't find the new function, which is to be expected) |
"toml" => load_toml(data), | ||
"csv" => load_csv(data), | ||
"json" => load_json(data), | ||
"plain" => to_value(data).map_err(|e| e.into()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about plain
, what is it even loading?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plain
is just the format which returns the data raw, with no additional processing. Either the request body or the file contents.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but when is it needed? I can't really think of a usecase where you would want that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There probably are some use cases out there, and seeing as it was basically free to implement, thought I might as well. It's probably something which may be critical to someone out there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a heads up - I actually would make use of plain
to load CSS and shove it inline at build-time, or to load a base64-encoded value that's ugly in code but needs to exist. They're rare use cases but they do exist.
@Keats I've made all the requested changes, and a couple bits of cleanup. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple more comments but looking good!
enum OutputFormat { | ||
TOML, | ||
JSON, | ||
CSV, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't uppercase them even if they are acronyms to be consistent with Rust style (https://doc.rust-lang.org/1.0.0/style/style/naming/README.html)
TOML, | ||
JSON, | ||
CSV, | ||
Plain |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I would still remove that for now, it can be added later if necessary
} | ||
|
||
impl OutputFormat { | ||
fn from(output_format: String) -> Result<Self> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could impl FromStr instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did think of that, but because it's possible for it to fail (ie the string doesnt match one of the enum values), it's unsafe, and TryFrom
is only in nightly right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FromStr
returns a Result
though? FromStr
and TryFrom
look to be identical for this usecase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha, I found my confusion. I was looking at From
rather than FromStr
. Yes you're right, FromStr
would be ideal to implement here!
} | ||
} | ||
|
||
fn get_cache_key(data_source: &DataSource, format: &OutputFormat) -> u64 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Making this part of the impl of DataSource would be cleaner imo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was toying with where to put it, as it's also equally valid on OutputFormat
, but as most of the computation varys for DataSource
, I guess there makes the most sense of the 2.
{{ response }} | ||
``` | ||
|
||
By default, the response body will be returned with no parsing. This can be changed by using the `format` argument as above. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I guess we need plain
because of that? How about making the format
argument mandatory?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could make it mandatory, but the fact it bases it off the file extension is a really nice touch for local data files. Having to specify that my data.json
file is a JSON file feels unnecessary when it's so easy to guess from the file extension, and change it by default if required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure myself what is the best choice there. I kind of like the nice UX for ending in JSON but in practice for remote data you will need to specify the format so it feels a bit like it could surprise the end user when something works for local data but not remote.
I would tend towards removing the special casing but not 100% convinced.
The 2 failing tests need to be fixed but I can merge it after |
We cant use a fixed hash because it's based on the absolute path, which changes for each dev (probably)
I've fixed the tests, and they run fine locally, however seem to be doing odd things on Travis. |
For |
This means tests can run on window / *nix
Tests passing 🎉 (besides netlify, but that doesn't look related to this PR?) |
Netlify is not going to work for a while with Zola sadly, not related to your PR :) Thanks! |
Nice one @RealOrangeOne !! |
Fixes #476.
Re-implements #465
Extends #379
Allow loading data from remote endpoints, and parsing through the existing formats
load_data
supports.This PR is intentionally designed to reuse as much of the code as possible when reading from data files or remote endpoints. This not only reduces the chance of odd bugs appearing, but means I don't have to write as much! Because I also moved things out into separate files, GitHub makes it looks like i've done way more work than I actually have.
kind
argumentformat
, as it's more appropriateIt would be possible to edit the headers when sending / receiving a response with the
format
argument. But i decided not to do that, and we / someone else can add it further down the line.None of this code has been run through
rustfmt
, intentionally.Note: Currently the test
can_load_remote_data
is failing, when it shouldn't be. This will fix itself once the repo move occurs.cc @kellpossible, enjoy!