You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Replace preferences backend with static data structures instead of hashmap #8539
C-assignedThere is someone working on resolving the issueC-has-open-prThere is a PR open that resolves the issueE-more-complexVariable effort required; may require a mentor. Recommended solution is clearly described in the issI-refactorNo impact; the issue is one of maintainability or tidiness. Proposed solution requires refactoring.
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?
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.
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
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 Valuedata 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.
@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.
@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?
@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)
C-assignedThere is someone working on resolving the issueC-has-open-prThere is a PR open that resolves the issueE-more-complexVariable effort required; may require a mentor. Recommended solution is clearly described in the issI-refactorNo impact; the issue is one of maintainability or tidiness. Proposed solution requires refactoring.
Activity
jdm commentedon Nov 16, 2015
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.
jdm commentedon Feb 22, 2017
There are a few parts to this:
PREFS.getwith it0xRampey commentedon Feb 22, 2017
@jdm This seems interesting. Can I work on this?
KiChjang commentedon Feb 22, 2017
@prampey Go ahead!
0xRampey commentedon Mar 5, 2017
Sorry for the delay guys. Had my tests.
@jdm I did some digging and found the
serde_jsoncrate. Based on the requirements posed, I think it would be effective to use aHashMap<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 instruct, you can actually list and iterate through all the key names in aHashMap.The
Valuedata 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
Valuetype with conversion functions likeas_i32().The other parts can be implemented if this data structure is accepted.
jdm commentedon Mar 6, 2017
@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.
0xRampey commentedon Mar 6, 2017
@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?
jdm commentedon Mar 7, 2017
That sounds correct. A structure sounds less complicated than a compiler plugin to me.
0xRampey commentedon Mar 8, 2017
@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,
structposes 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, theserdelibrary provides ajson!macro that checks type of every field and converts it all into aValuein 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 astruct.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)
0xRampey commentedon Apr 22, 2017
@jdm Anything we can do about this?
frewsxcv commentedon Apr 23, 2017
This should be possible via a cargo build script.
41 remaining items
Auto merge of #22923 - peterjoel:issue_8539_prefs_refactor, r=asajeffrey
servo#8539 Config preferences backend restructure
Auto merge of #22923 - peterjoel:issue_8539_prefs_refactor, r=<try>
servo#8539 Config preferences backend restructure
servo#8539 Config preferences backend restructure
Auto merge of #22923 - peterjoel:issue_8539_prefs_refactor, r=asajeffrey
servo#8539 Config preferences backend restructure
Auto merge of #22923 - peterjoel:issue_8539_prefs_refactor, r=jdm
servo#8539 Config preferences backend restructure
Bug 1539326 - Cherry-pick changes for servo/servo#8539 (config prefer…
Bug 1539326 - Cherry-pick changes for servo/servo#8539 (config prefer…
Bug 1539326 - Cherry-pick changes for servo/servo#8539 (config prefer…
Bug 1539326 - Cherry-pick changes for servo/servo#8539 (config prefer…
Bug 1539326 - Cherry-pick changes for servo/servo#8539 (config prefer…