Skip to content

Remove use of ExtractType in deserialize_any#569

Merged
wycats merged 6 commits intonushell:masterfrom
est31:serde_instead_of_specialization
Sep 2, 2019
Merged

Remove use of ExtractType in deserialize_any#569
wycats merged 6 commits intonushell:masterfrom
est31:serde_instead_of_specialization

Conversation

@est31
Copy link
Contributor

@est31 est31 commented Sep 2, 2019

This removes use of the ExtractType trait from the deserialize_any function by changing the code to never call it. The ultimate goal is to remove use of specialization.

ExtractType and its use of specialization exists to allow deserialization of cli arguments. For each command, there is a struct with some members and serde's Deserialize trait is being derived. This is the argument struct for the mv command:

#[derive(Deserialize)]
pub struct MoveArgs {
    pub src: Tagged<PathBuf>,
    pub dst: Tagged<PathBuf>,
}

The cli arguments input by an user are being converted from a CallInfo struct to that command-specific struct using ConfigDeserializer. The only uses of the ExtractType trait in the codebase are in its Deserializer implementation where they help avoiding having to use serde beyond iterating over the fields of the command-specific structs: any field of a cli param is expected to be readable from a Tagged<Value>.

In order to reduce usage of ExtractType, this PR brings more data into format understandable by serde.

The command rg "\(Deserialize\)][^}]*\}" -U src/commands/* provides a list of command-param-structs that we have to handle. The field types can be extracted using rg "\(Deserialize\)][^}]*\}" -U src/commands/* | rg ": .*: " | sed 's/^.*: //' | sort | uniq:

bool,
Option<Tagged<PathBuf>>,
Option<Tagged<String>>,
Option<(Tagged<String>, Tagged<Value>)>,
Tagged<bool>,
Tagged<i64>,
Tagged<PathBuf>,
Tagged<String>,
value::Block,
Vec<Tagged<PathBuf>>,
Vec<Tagged<String>>,

Previously, all these types were parsed from Tagged<Value> instances using the ExtractType trait. Now, serde with its outside-to-inside model is being used to handle the logic to deserialize Vec<>, bool, Option<>, tuples, but we still defer to ExtractType when something is encountered that doesn't fit into the serde data model like Tagged or Block. Handling these via serde as well is still possible and subject of future PRs.

Useful commands for testing:

open tests/fixtures/formats/sample.bson | pick _id root // tests vec
config --set [specialization false] // tests Option containing a tuple

cc #362

The main point of this struct seems to be debugging,
as key_struct_field is unused except for debugging.
There are still tagged bools in use so we can't
remove the ExtractType implementation.
@est31 est31 mentioned this pull request Sep 2, 2019
12 tasks
@wycats wycats merged commit 246c9c0 into nushell:master Sep 2, 2019
@wycats
Copy link
Contributor

wycats commented Sep 2, 2019

@est31 Honestly, you know serde better than I do. Thanks for this work and all of your work getting us on stable!

@andrasio
Copy link
Member

andrasio commented Sep 9, 2019

config --set [specialization false] // tests Option containing a tuple

Thoughts on #627 ? @est31

@est31
Copy link
Contributor Author

est31 commented Sep 9, 2019

@androbtech yeah this is likely a regression caused by this PR. Sorry for the inconvenience! Will try to get a fix done by tomorrow.

@est31
Copy link
Contributor Author

est31 commented Sep 9, 2019

Hmmm nevermind, it wasn't this PR but #579.

elferherrera pushed a commit to elferherrera/nushell that referenced this pull request Feb 7, 2022
* Add metadata command

* Add string interpolation to testing
kubouch pushed a commit that referenced this pull request Feb 7, 2022
* Add metadata command

* Add string interpolation to testing
Hofer-Julian pushed a commit to Hofer-Julian/nushell that referenced this pull request Jan 27, 2023
Stars were incorrectly escaped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants