-
-
Notifications
You must be signed in to change notification settings - Fork 155
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
Update value at path #153
Comments
Nope, you're not missing anything. The nodes + node_views don't have any awareness of parents so the context is lost once The lack of parent state is also why I'd really like to avoid adding parent state tracking to nodes if I can avoid it (it'd be an ABI break), but I'm also not sure what else to suggest. I'm torn between a few different solutions:
Do any of those sound helpful? |
To me the last one seems like the most sensible way to do it. Would make it possible to do things like below, which seems relatively tidy... By contrast, option 1 feels a bit hacky, and callbacks seem excessive for the problem. Option 3 would allow something like: template <typename T>
bool update_or_insert(toml::table& tbl, const std::string& path, const T& value) {
auto path = tbl.parse_path(path); // perhaps exception or return value to indicate if path not valid for this table?
auto parentNode = tbl.at_path(path.parent());
if (parentNode.is_table()) {
parentNode.as_table().insert_or_assign(path.leafName(), value);
}
} Thoughts? Could also be non-breaking if at_path still accepted a string OR a toml::path object? |
Yeah, I agree.
Indeed. If I implemented it via the I'll try to make time for this on some weekend soon-ish, but it might be a little while. Fairly busy of late. If you need a workaround in the short-term you'd get pretty far just by ripping the library's implementation of |
I've forked you and I'll have a go at getting it started. I'd agree with overloads but suggest that the |
It is perhaps 'unclean' to have two implementations, but there are real benefits to doing it separately - if you parse a full path out into some string-based data type (which is what |
That's a good point, particularly the short-circuiting of path parsing is nice. The only way to do that with a |
Sounds great! Check out CONTRIBUTING.md if you haven't already, it has most of what you need to get going. I'll also be available to answer any questions, too, of course. |
Will do! One thing not mentioned in there - do you prefer PRs to be a separate branch? Or just a main -> main PR? |
Eh, I don't really have a strong preference. People like to complain that main -> main is bad somehow, but if it's coming from a fork and I do a squash + merge, it all comes out the same colour ¯\_(ツ)_/¯ |
Actually, now that I think about it a bit more, this has the potential to be a fairly large feature so maybe shouldn't go straight into main - I might want to spend a bit of time doing a few additional bits-and-pieces. I'll create a |
Just an FYI, am getting a lot >=W4 warnings around enums missing specific cases despite there being defaults. I know your CONTRIBUTING.md indicates no warnings, but these seem pre-existing (and reasonable)? |
Heh, no worries. I do realize that the header-only + automagically-preprocessed-into-a-single-file nature of the lib does mean there's quite a bit of header + preprocessor shenanigans, and you're likely to run into a bit of this. Some advice/clarifications you might need:
|
Thanks! I had missed a TOML_DISABLE_WARNINGS macro - all works properly now! Thanks for the heads up on the std_XXX etc. stuff, that's a good catch. Thoughts on preferred error handling if an invalid path is provided? It does look like you have some places that throw parse errors, but at_path returns a nullptr which doesn't really make sense in a class that just contains a parsed path. Alternatively could return an empty path object and an toml::path::empty static to compare against for check? |
Oh, almost forgot. The single-header generator script has it's own "magic comments" for indicating that code should not be included in
I haven't bothered documenting these anywhere because until now contributors have only really wanted to fix minor bugs or do tooling-related stuff, so nobody has needed to do anything structurally-significant enough for it to matter. That will likely be true for you too, but just thought you should know that those comments actually have a semantic meaning. |
At path doesn't return a nullptr, it returns a toml::table tbl = toml::parse(/* ... */);
toml::path path = /* ... */;
auto view = tbl[path]; // or tbl.at_path(path)
if (!view)
{
system("format c:");
}
Using a |
Unless I've misunderstood and you actually literally mean a parse failure of the input path, rather than a failed lookup? In which case... can EDIT: ok, there's one parse failure path, for array indexers that are missing an valid index integer or a closing bracket... well then. Bearing in mind that the library needs to remain usable for people who choose to disable exceptions, it may be that simply having a path be 'falsy' via |
Was just going to reply and then saw your edit. I'm talking about the latter. Agree that at_path(toml::path&) should behave identically to the string version. The three failures to prase that I can find are:
I like the idea of returning an empty path and evaluating it as a falsy value. Alternatively could return the path as parsed up until the failure point, and set a flag to evaluate as falsy. Finally, I'll add a get_parse_err() method or similar. Cheers, thanks! Do you have a discord for the library at all or anything? edit: just saw your code sample system call - gave me a good chuckle! |
My personal preference here is all-or-nothing:
Since the failure surface area is limited to just broken array indexers I don't think it needs to be much more complicated than that. Partial results tend to cause as many problems as they solve as inevitably people use them without checking error states and they 'sort of' work (until they don't). If a fail reason is desired I suppose it could be delivered via exception, but that would need to be opt-in somehow in the same spirit of std::vector's
As of 5 seconds ago there is a 'gitter' for this repo: https://gitter.im/marzer/tomlplusplus |
@wakely This might be relevant to your interests, given our discussion in #118. @jonestristand has a PR open to add this feature - #156. It won't add the full path reconstructibility you wanted, but it might help simplify/improve whatever workaround you've come up with in the meantime. |
(sorry it took me so long) |
Is your feature request related to a problem? Please describe.
It would be very useful to be able to insert or modify a value at a particular path, essentially the inverse of the at_path(...).value_or(...) method chain.
Describe the solution you'd like
Something like tbl.at_path(...).insert_or_update(...). At the moment it seems like I would need to parse the path string myself to find the correct parent table to then call insert_or_assign, which is unfortunate since the path-parsing code already exists in the library. Or is there a canonical way of doing this already that I'm missing?
The text was updated successfully, but these errors were encountered: