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

Configuration and loading from yaml files #61

Merged
merged 41 commits into from Jun 15, 2016

Conversation

Projects
None yet
4 participants
@Aceeri
Member

Aceeri commented May 16, 2016

Allows using macros to create a structure along with loading a yaml file into that struct.

It isn't entirely finished as I'd like to add a couple more loading types and clean it up a bit, but it would be very helpful if anyone can spot something I missed or give suggestions.

  • Basic enum loading? Complex enums would take a rather large feat of macro-ness (tokenizer hackiness?) to get working and I don't see much benefit so it might be better to only allow really basic enums e.g.
    enum Example { Option1, Option2, Option3 }
  • Hashmap loading
  • Vec loading
  • Writing to files?
  • Option loading? null return None, else Some(T)?

I'm going to be a bit busy until next wednesday, so I probably won't be making any changes until then.

src/config.rs
+
+ #[derive(Clone, Debug)]
+ pub struct $root {
+ _meta: ConfigMeta,

This comment has been minimized.

@Aceeri

Aceeri May 16, 2016

Member

Should I move this to be completely invisible/in the background to the user or keep it exposed? It may be useful if we want to do something like writing to the configs as then we would still have the location of file along with the location the Config struct is defined.

@Aceeri

Aceeri May 16, 2016

Member

Should I move this to be completely invisible/in the background to the user or keep it exposed? It may be useful if we want to do something like writing to the configs as then we would still have the location of file along with the location the Config struct is defined.

src/config.rs
+ path = path + element;
+ }
+
+ format!("{}: Failed to parse YAML: {}: expect {}", meta.path.display(), path, meta.ty)

This comment has been minimized.

@Aceeri

Aceeri May 16, 2016

Member

Should probably re-add in a check if the key is a "BadValue", if it then say "Could not find YAML" instead of "Failed to parse YAML".

@Aceeri

Aceeri May 16, 2016

Member

Should probably re-add in a check if the key is a "BadValue", if it then say "Could not find YAML" instead of "Failed to parse YAML".

src/config.rs
+
+#[derive(Clone, Debug)]
+pub struct ConfigMeta {
+ path: PathBuf, // Where the file is located, "" if not from a file.

This comment has been minimized.

@lschmierer

lschmierer May 18, 2016

Member

Maybe use Option<PathBuf>. here ;)
A little bit cleaner.

@lschmierer

lschmierer May 18, 2016

Member

Maybe use Option<PathBuf>. here ;)
A little bit cleaner.

This comment has been minimized.

@Aceeri

Aceeri May 20, 2016

Member

Decided to make it default to "config\config.yml" instead as a default directory for amethyst.

@Aceeri

Aceeri May 20, 2016

Member

Decided to make it default to "config\config.yml" instead as a default directory for amethyst.

@lschmierer

This comment has been minimized.

Show comment
Hide comment
@lschmierer

lschmierer May 18, 2016

Member

Writing to files? Not sure how necessary this would be.

I think existing Yaml files should be automatically updated to match the layout of the config!{...} defined Configs. So, when the config struct is changed, the developer does not need to take care of the Yaml config files.

Member

lschmierer commented May 18, 2016

Writing to files? Not sure how necessary this would be.

I think existing Yaml files should be automatically updated to match the layout of the config!{...} defined Configs. So, when the config struct is changed, the developer does not need to take care of the Yaml config files.

@Aceeri

This comment has been minimized.

Show comment
Hide comment
@Aceeri

Aceeri May 20, 2016

Member

Looks like this is almost done, couple of rather minor bugs to fix. Currently writing will only write to a separate file if the file already exists in config/, otherwise it will just overwrite the "extern" with the default value.

Member

Aceeri commented May 20, 2016

Looks like this is almost done, couple of rather minor bugs to fix. Currently writing will only write to a separate file if the file already exists in config/, otherwise it will just overwrite the "extern" with the default value.

@lschmierer

This comment has been minimized.

Show comment
Hide comment
@lschmierer

lschmierer May 21, 2016

Member

Does writing keep comments in the yaml files?

Member

lschmierer commented May 21, 2016

Does writing keep comments in the yaml files?

@Aceeri

This comment has been minimized.

Show comment
Hide comment
@Aceeri

Aceeri May 21, 2016

Member

@lschmierer Not at the moment, hadn't thought of that.

Member

Aceeri commented May 21, 2016

@lschmierer Not at the moment, hadn't thought of that.

@thiolliere

This comment has been minimized.

Show comment
Hide comment
@thiolliere

thiolliere May 22, 2016

I think that from_file_raw must result in an error if there is there is both display.yml and display/config.yml

I think that from_file_raw must result in an error if there is there is both display.yml and display/config.yml

@thiolliere

This comment has been minimized.

Show comment
Hide comment
@thiolliere

thiolliere May 22, 2016

it can also be cool in case display.yml have a key not in its structure definition to warn unexpected key.

it can also be cool in case display.yml have a key not in its structure definition to warn unexpected key.

@Aceeri

This comment has been minimized.

Show comment
Hide comment
@Aceeri

Aceeri Jun 1, 2016

Member

Alright! I added a warning for unexpected keys in the yaml. from_file() now outputs a warning/error if there are multiple files and defaults instead of loading. And finally, documentation for configuration structures and enums work properly now in the macro, along with custom derives so there is less need for comments inside of the .yml/.yaml files.

So far I've tested on windows and linux and it works great.

Member

Aceeri commented Jun 1, 2016

Alright! I added a warning for unexpected keys in the yaml. from_file() now outputs a warning/error if there are multiple files and defaults instead of loading. And finally, documentation for configuration structures and enums work properly now in the macro, along with custom derives so there is less need for comments inside of the .yml/.yaml files.

So far I've tested on windows and linux and it works great.

+path = "src/config/"
+version = "0.1.0"
+
+[dependencies]

This comment has been minimized.

@kvark

kvark Jun 7, 2016

Member

missing new line

@kvark

kvark Jun 7, 2016

Member

missing new line

src/config/examples/main.rs
+use std::path::Path;
+
+fn main() {
+ let config = amethyst_config::Config::from_file(Path::new("../../config/config.yml"));

This comment has been minimized.

@kvark

kvark Jun 10, 2016

Member

If you use AsRef<Path>, like File::open does, you could avoid Path::new() here.

@kvark

kvark Jun 10, 2016

Member

If you use AsRef<Path>, like File::open does, you could avoid Path::new() here.

+ if index != 0 {
+ tree = tree + "->";
+ }
+

This comment has been minimized.

@kvark

kvark Jun 10, 2016

Member

You do seem to like empty lines a lot :)

@kvark

kvark Jun 10, 2016

Member

You do seem to like empty lines a lot :)

This comment has been minimized.

@Aceeri

Aceeri Jun 11, 2016

Member

Just a habit I have, I usually separate things that modify scope. I should probably clean up some of these whitespaces though.

@Aceeri

Aceeri Jun 11, 2016

Member

Just a habit I have, I usually separate things that modify scope. I should probably clean up some of these whitespaces though.

+ }
+ }
+
+ if array.len() > 10 {

This comment has been minimized.

@kvark

kvark Jun 10, 2016

Member

declare 10 as some sort of a constant for clarity?

@kvark

kvark Jun 10, 2016

Member

declare 10 as some sort of a constant for clarity?

+yaml_array!(7 => 0 1 2 3 4 5 6);
+yaml_array!(8 => 0 1 2 3 4 5 6 7);
+yaml_array!(9 => 0 1 2 3 4 5 6 7 8);
+yaml_array!(10 => 0 1 2 3 4 5 6 7 8 9);

This comment has been minimized.

@kvark

kvark Jun 10, 2016

Member

ah, this is where 10 came from, I see

@kvark

kvark Jun 10, 2016

Member

ah, this is where 10 came from, I see

@kvark

This comment has been minimized.

Show comment
Hide comment
@kvark

kvark Jun 15, 2016

Member

Thanks @Aceeri !

Member

kvark commented Jun 15, 2016

Thanks @Aceeri !

@kvark kvark merged commit 34a746a into amethyst:develop Jun 15, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment