Debian Codegraph @

lib/wp/json-utils.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2023 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "json-utils.h"
10 #include "error.h"
11 #include "log.h"
12
13 #include <pipewire/pipewire.h>
14 #include <spa/utils/result.h>
15
16 WP_DEFINE_LOCAL_LOG_TOPIC ("wp-json-utils")
17
18 /*! \defgroup wpjsonutils Json Utilities */
19
20 struct match_rules_cb_data
21 {
22 WpRuleMatchCallback callback;
23 gpointer data;
24 GError **error;
25 };
26
27 static int
28 match_rules_cb (void *data, const char *location, const char *action,
29 const char *str, size_t len)
30 {
31 struct match_rules_cb_data *cb_data = data;
32 g_autoptr (WpSpaJson) json = wp_spa_json_new_wrap_stringn (str, len);
33 return cb_data->callback (cb_data->data, action, json, cb_data->error) ? 0 : -EPIPE;
34 }
35
36 /*!
37 * \brief Matches the given properties against a set of rules described in JSON
38 * and calls the given callback to perform actions on a successful match.
39 *
40 * The given JSON should be an array of objects, where each object has a
41 * "matches" and an "actions" property. The "matches" value should also be
42 * an array of objects, where each object is a set of properties to match.
43 * Inside such an object, all properties must match to consider a successful
44 * match. However, if multiple objects are provided, only one object needs
45 * to match.
46 *
47 * The "actions" value should be an object where the key is the action name
48 * and the value can be any valid JSON. Both the action name and the value are
49 * passed as-is on the \a callback.
50 *
51 * \verbatim
52 * [
53 * {
54 * matches = [
55 * # any of the items in matches needs to match, if one does,
56 * # actions are emitted.
57 * {
58 * # all keys must match the value. ! negates. ~ starts regex.
59 * <key> = <value>
60 * ...
61 * }
62 * ...
63 * ]
64 * actions = {
65 * <action> = <value>
66 * ...
67 * }
68 * }
69 * ]
70 * \endverbatim
71 *
72 * \ingroup wpjsonutils
73 * \param json a JSON array containing rules in the described format
74 * \param match_props (transfer none): the properties to match against the rules
75 * \param callback (scope call)(closure data): a function to call for each action on a successful match
76 * \param data data to be passed to \a callback
77 * \param error (out)(optional): the error that occurred, if any
78 * \returns FALSE if an error occurred, TRUE otherwise
79 */
80 gboolean
81 wp_json_utils_match_rules (WpSpaJson *json, WpProperties *match_props,
82 WpRuleMatchCallback callback, gpointer data, GError ** error)
83 {
84 g_autoptr (GError) cb_error = NULL;
85 struct match_rules_cb_data cb_data = { callback, data, &cb_error };
86
87 int res = pw_conf_match_rules (wp_spa_json_get_data (json),
88 wp_spa_json_get_size (json), NULL, wp_properties_peek_dict (match_props),
89 match_rules_cb, &cb_data);
90
91 if (res < 0) {
92 if (cb_error)
93 g_propagate_error (error, g_steal_pointer (&cb_error));
94 else
95 g_set_error (error, WP_DOMAIN_LIBRARY, WP_LIBRARY_ERROR_OPERATION_FAILED,
96 "match rules error: %s", spa_strerror (res));
97 return FALSE;
98 }
99
100 return TRUE;
101 }
102
103 struct update_props_cb_data
104 {
105 WpProperties *props;
106 gint count;
107 };
108
109 static gboolean
110 update_props_cb (gpointer data, const gchar * action, WpSpaJson * value,
111 GError ** error)
112 {
113 struct update_props_cb_data *cb_data = data;
114 if (g_str_equal (action, "update-props"))
115 cb_data->count += wp_properties_update_from_json (cb_data->props, value);
116 return TRUE;
117 }
118
119 /*!
120 * \brief Matches the given properties against a set of rules described in JSON
121 * and updates the properties if the rule actions include the "update-props"
122 * action.
123 *
124 * \ingroup wpjsonutils
125 * \param json a JSON array containing rules in the format accepted by
126 * wp_json_utils_match_rules()
127 * \param props (transfer none): the properties to match against the rules
128 * and also update, acting on the "update-props" action
129 * \returns the number of properties that were updated
130 */
131 gint
132 wp_json_utils_match_rules_update_properties (WpSpaJson *json, WpProperties *props)
133 {
134 g_autoptr (GError) cb_error = NULL;
135 struct update_props_cb_data cb_data = { props, 0 };
136
137 wp_json_utils_match_rules (json, props, update_props_cb, &cb_data, &cb_error);
138 if (cb_error)
139 wp_notice ("%s", cb_error->message);
140
141 return cb_data.count;
142 }
143
144
145 #define OVERRIDE_SECTION_PREFIX "override."
146
147 static WpSpaJson *
148 merge_json_objects (WpSpaJson *a, WpSpaJson *b)
149 {
150 g_autoptr (WpSpaJsonBuilder) builder = NULL;
151
152 builder = wp_spa_json_builder_new_object ();
153
154 /* Add all properties from 'a' that don't exist in 'b' */
155 {
156 g_autoptr (WpIterator) it = wp_spa_json_new_iterator (a);
157 g_auto (GValue) item = G_VALUE_INIT;
158 for (; wp_iterator_next (it, &item); g_value_unset (&item)) {
159 g_autoptr (WpSpaJson) key = NULL;
160 g_autoptr (WpSpaJson) val = NULL;
161 g_autoptr (WpSpaJson) j = NULL;
162 g_autofree gchar *str = NULL;
163 const gchar *key_str;
164 g_autofree gchar *override_key_str = NULL;
165 gboolean override;
166
167 key = g_value_dup_boxed (&item);
168 key_str = str = wp_spa_json_parse_string (key);
169 g_return_val_if_fail (key_str, NULL);
170 override = g_str_has_prefix (str, OVERRIDE_SECTION_PREFIX);
171 if (override)
172 key_str += strlen (OVERRIDE_SECTION_PREFIX);
173 override_key_str = g_strdup_printf (OVERRIDE_SECTION_PREFIX "%s", key_str);
174
175 g_value_unset (&item);
176 g_return_val_if_fail (wp_iterator_next (it, &item), NULL);
177 val = g_value_dup_boxed (&item);
178
179 if (!wp_spa_json_object_get (b, key_str, "J", &j, NULL) &&
180 !wp_spa_json_object_get (b, override_key_str, "J", &j, NULL)) {
181 wp_spa_json_builder_add_property (builder, key_str);
182 wp_spa_json_builder_add_json (builder, val);
183 }
184 }
185 }
186
187 /* Add properties from 'b' that don't exist in 'a'. If a property
188 * exists in 'a' and does not have the 'override.' prefix, recursively
189 * merge it before adding it. Otherwise override it. */
190 {
191 g_autoptr (WpIterator) it = wp_spa_json_new_iterator (b);
192 g_auto (GValue) item = G_VALUE_INIT;
193 for (; wp_iterator_next (it, &item); g_value_unset (&item)) {
194 g_autoptr (WpSpaJson) key = NULL;
195 g_autoptr (WpSpaJson) val = NULL;
196 g_autoptr (WpSpaJson) j = NULL;
197 g_autofree gchar *str = NULL;
198 const gchar *key_str;
199 g_autofree gchar *override_key_str = NULL;
200 gboolean override;
201
202 key = g_value_dup_boxed (&item);
203 key_str = str = wp_spa_json_parse_string (key);
204 g_return_val_if_fail (key_str, NULL);
205 override = g_str_has_prefix (str, OVERRIDE_SECTION_PREFIX);
206 if (override)
207 key_str += strlen (OVERRIDE_SECTION_PREFIX);
208 override_key_str = g_strdup_printf (OVERRIDE_SECTION_PREFIX "%s", key_str);
209
210 g_value_unset (&item);
211 g_return_val_if_fail (wp_iterator_next (it, &item), NULL);
212 val = g_value_dup_boxed (&item);
213
214 if (!override && wp_spa_json_is_container (val) &&
215 (wp_spa_json_object_get (a, key_str, "J", &j, NULL) ||
216 wp_spa_json_object_get (a, override_key_str, "J", &j, NULL))) {
217 g_autoptr (WpSpaJson) merged = wp_json_utils_merge_containers (j, val);
218 if (!merged) {
219 wp_warning ("skipping merge of %s as JSON values are not compatible containers",
220 key_str);
221 continue;
222 }
223 wp_spa_json_builder_add_property (builder, key_str);
224 wp_spa_json_builder_add_json (builder, merged);
225 } else {
226 wp_spa_json_builder_add_property (builder, key_str);
227 wp_spa_json_builder_add_json (builder, val);
228 }
229 }
230 }
231
232 return wp_spa_json_builder_end (builder);
233 }
234
235 static WpSpaJson *
236 merge_json_arrays (WpSpaJson * a, WpSpaJson * b)
237 {
238 g_autoptr (WpSpaJsonBuilder) builder = NULL;
239
240 builder = wp_spa_json_builder_new_array ();
241
242 /* Add all elements from 'a' */
243 {
244 g_autoptr (WpIterator) it = wp_spa_json_new_iterator (a);
245 g_auto (GValue) item = G_VALUE_INIT;
246 for (; wp_iterator_next (it, &item); g_value_unset (&item)) {
247 WpSpaJson *j = g_value_get_boxed (&item);
248 wp_spa_json_builder_add_json (builder, j);
249 }
250 }
251
252 /* Add all elements from 'b' */
253 {
254 g_autoptr (WpIterator) it = wp_spa_json_new_iterator (b);
255 g_auto (GValue) item = G_VALUE_INIT;
256 for (; wp_iterator_next (it, &item); g_value_unset (&item)) {
257 WpSpaJson *j = g_value_get_boxed (&item);
258 wp_spa_json_builder_add_json (builder, j);
259 }
260 }
261
262 return wp_spa_json_builder_end (builder);
263 }
264
265 /*!
266 * \brief Merges two JSON containers (objects or arrays) into one.
267 *
268 * If both \a a and \a b are objects, the result will be a new object
269 * containing all properties from both \a a and \a b. If a property exists
270 * in both \a a and \a b, the values are recursively merged. If a property
271 * exists in both \a a and \a b and the property name starts with the
272 * "override." prefix in either of those, the value from the key with the
273 * prefix is used.
274 *
275 * If both \a a and \a b are arrays, the result will be a new array
276 * containing all elements from both \a a and \a b.
277 *
278 * If \a a and \a b are not of the same type, NULL is returned.
279 *
280 * \ingroup wpjsonutils
281 * \param a (transfer none): a JSON container
282 * \param b (transfer none): a JSON container
283 * \returns a new JSON container containing the merged contents of \a a and \a b
284 * or NULL if \a a and \a b are not of the same type
285 */
286 WpSpaJson *
287 wp_json_utils_merge_containers (WpSpaJson * a, WpSpaJson * b)
288 {
289 if (wp_spa_json_is_array (a) && wp_spa_json_is_array (b))
290 return merge_json_arrays (a, b);
291 else if (wp_spa_json_is_object (a) && wp_spa_json_is_object (b))
292 return merge_json_objects (a, b);
293 return NULL;
294 }