Turi Create  4.0
dict_transform_utils.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #include <core/data/flexible_type/flexible_type.hpp>
7 #include <functional>
8 #include <core/data/sframe/gl_sarray.hpp>
9 
10 namespace turi {
11 
12 /** Flattens list or dictionary types to a non-nested dictionary of
13  * (string key : numeric value) pairs. Each nested key is a
14  * concatenation of the keys in the separation with sep_char
15  * separating them. For example, if sep_char = ".", then
16  *
17  * {"a" : {"b" : 1}, "c" : 2}
18  *
19  * becomes
20  *
21  * {"a.b" : 1, "c" : 2}.
22  *
23  * - List and vector elements are handled by converting the index of
24  * the appropriate element to a string.
25  *
26  * - String values are handled by treating them as a single
27  * {"string_value" : 1} pair.
28  *
29  * - FLEX_UNDEFINED values are handled by replacing them with the
30  * string contents of `undefined_string`.
31  *
32  * - image and datetime types are handled by calling
33  * image_value_handler and datetime_value_handler. These
34  * functions must either throw an exception, which propegates up,
35  * return any other flexible type (e.g. dict, list, vector, etc.),
36  * or return FLEX_UNDEFINED, in which case that value is ignored.
37  *
38  *
39  */
40 flex_dict to_flat_dict(const flexible_type& dict_or_list,
41  const flex_string& separator,
42  const flex_string& undefined_string,
43  std::function<flexible_type(const flexible_type&)> nonnumeric_value_handler);
44 
45 /** Identical to to_flat_dict, except that image_policy and
46  * datetime_policy determine the handling of image and datetime types
47  * rather than a custom function. Currently, the only possible value
48  * for this is "error".
49  */
50 flex_dict to_flat_dict(const flexible_type& input,
51  const flex_string& separator,
52  const flex_string& undefined_string,
53  const std::string& image_policy = "error",
54  const std::string& datetime_policy = "error");
55 
56 /** Applies to_flat_dict to all elements in an sarray, returning
57  * the transformed sarray.
58  */
59 gl_sarray to_sarray_of_flat_dictionaries(gl_sarray input,
60  const flex_string& sep,
61  const flex_string& undefined_string,
62  const std::string& image_policy = "error",
63  const std::string& datetime_policy = "error");
64 }
std::string flex_string
std::vector< std::pair< flexible_type, flexible_type > > flex_dict
flex_dict to_flat_dict(const flexible_type &dict_or_list, const flex_string &separator, const flex_string &undefined_string, std::function< flexible_type(const flexible_type &)> nonnumeric_value_handler)
gl_sarray to_sarray_of_flat_dictionaries(gl_sarray input, const flex_string &sep, const flex_string &undefined_string, const std::string &image_policy="error", const std::string &datetime_policy="error")