Skip to content

Replace preferences backend with static data structures instead of hashmap #8539

Closed

Description

@frewsxcv

Instead of having a dedicated prefs.json file with this kind of logic, it'd be great if we didn't have to duplicate key names all over the place. What if we made the json into some Rust data structure to hold the default values with key names that can be referenced by other files? If not, is it possible to check the key names at compile time?

Activity

  1. added
    I-enhancement
    on Nov 16, 2015
  2. jdm commented on Nov 16, 2015

    @jdm
    Member

    We could deserialize the JSON into the struct, although I'm not certain what that code does with fields present in the JSON that aren't in the Rust structure.

  3. jdm commented on Feb 22, 2017

    @jdm
    Member

    There are a few parts to this:

    • create a Rust structure that holds all of the known preferences using native types
    • create a new API to obtain this structure and replace each use of PREFS.get with it
    • create an API that can get and set preferences via a string name and map those to the native values (this will be required by some code in components/style and components/webdriver_server)
    • create a mechanism to deserialize prefs.json and apply the values in it to the native values
  4. added
    I-refactor
    and removed
    I-enhancement
    on Feb 22, 2017
  5. 0xRampey commented on Feb 22, 2017

    @0xRampey
    Contributor

    @jdm This seems interesting. Can I work on this?

  6. added
    C-assigned
    on Feb 22, 2017
  7. KiChjang commented on Feb 22, 2017

    @KiChjang
    Contributor

    @prampey Go ahead!

  8. 0xRampey commented on Mar 5, 2017

    @0xRampey
    Contributor

    Sorry for the delay guys. Had my tests.
    @jdm I did some digging and found the serde_json crate. Based on the requirements posed, I think it would be effective to use a HashMap<String, Value> data structure. A HashMap because well, it is dynamic in nature (grows to our requirements) and it has proper error checking, so with any false-key extraction, the compiler automatically blurts out the key is not available. Also, unlike in struct, you can actually list and iterate through all the key names in a HashMap.
    The Value data type is an enum to represent JSON data, therefore, all types of JSON data can be represented with it. So we won't have any problems with extracting any type of JSON data.
    Native types can easily be extracted from Value type with conversion functions like as_i32().
    The other parts can be implemented if this data structure is accepted.

  9. jdm commented on Mar 6, 2017

    @jdm
    Member

    @prampey We already store preferences inside of a HashMap; the goal of this work is to make it a compile-time failure to use a preference name that does not exist.

  10. 0xRampey commented on Mar 6, 2017

    @0xRampey
    Contributor

    @jdm Okay, so we can either convert it into a struct which performs its own compile-time checking or we could use a compiler plugin which traces through the data structure for our keyname and then reports a result. Right?

  11. jdm commented on Mar 7, 2017

    @jdm
    Member

    That sounds correct. A structure sounds less complicated than a compiler plugin to me.

  12. 0xRampey commented on Mar 8, 2017

    @0xRampey
    Contributor

    @jdm This seems much more complicated than I thought it to be.
    In order to compile-check the validity of a key, we first need access to the JSON string during compile time. This is only possible if we have the JSON hardcoded.
    Secondly, struct poses some limitations. To start with, we cannot set preferences to it due to a fixed number of fields. Also, we need to dynamically generate the fields of the struct from the JSON string during compile time. Since macros do not have type information, I'd have to check the type of every field in the JSON literal. Luckily, the serde library provides a json! macro that checks type of every field and converts it all into a Value in the end. So I can use this code and modify it. However, I'd still have to collect all the types and key names in a vector and then generate a struct (all at once) in the end. (which I'm not sure is possible with a macro :P) This seems like a lot of effort just to leverage the compile-time checking of a struct.
    Thirdly, my previous comment on the compiler plugin is probably wrong. You can't trace through a data structure during compile time right? We'd probably have to parse the AST for our desired key name and then report accordingly. (assuming our JSON was hardcoded)

  13. 0xRampey commented on Apr 22, 2017

    @0xRampey
    Contributor

    @jdm Anything we can do about this?

  14. frewsxcv commented on Apr 23, 2017

    @frewsxcv
    ContributorAuthor

    In order to compile-check the validity of a key, we first need access to the JSON string during compile time.

    This should be possible via a cargo build script.

  15. 41 remaining items

  16. added a commit that references this issue on Mar 18, 2019
  17. added a commit that references this issue on Mar 19, 2019
  18. added a commit that references this issue on Mar 19, 2019
  19. added 2 commits that reference this issue on Mar 19, 2019
  20. added a commit that references this issue on Mar 19, 2019
  21. added a commit that references this issue on Mar 20, 2019
  22. added a commit that references this issue on Mar 20, 2019
  23. added a commit that references this issue on Mar 22, 2019
  24. added a commit that references this issue on Mar 27, 2019
  25. added a commit that references this issue on Mar 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-assignedC-has-open-prE-more-complexI-refactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions