dynamic_view for safer traversal of dynamics
Summary: Existing accessors on dynamic are cumbersome when operating on dynamics that have optional keys or nullable values. Safely accessing these ends up in very bloated or ugly client code, even when using getDefault(). Additionally, the move semantics of `dynamic` are usually inferred based on the cv-ref qualification of the dynamic, and results in inconsistent syntax depending on the situation. `dynamic_view` attempts to resolve these issues by providing a new and explicit API for traversing and accessing a `dynamic`. The main new API is `descend`, which takes a varags list of keys and, functionally, repeatedly applies `operator[]` and either returns a view of the resulting value or an empty view if it doesn't exist or a type error occured during descent. It only throws if another unexpected exception occurs. `const_dynamic_view` contains copying accessors which will not throw on a type mismatch, or if invoked on an empty view, but instead will return a mandatory default value provided by the caller. The other new API is only provided by the non-const `dynamic_view` class. It is a set of value accessors which explicitly move the underlying value out of the viewed dynamic. There is a specialization for string values and a generic version for `dynamic`. A helper method, `folly::make_dynamic_view` is provided which determines the appropriate variant for a given dynamic ref. `friend` access to `dynamic` allows optimized checks and accesses over the regular `dynamic` APIs. Reviewed By: yfeldblum Differential Revision: D4917260 fbshipit-source-id: a07d26ed502aa22a5f19dd8521373a9e83fd6c02
Showing
Please register or sign in to comment