@@ -232,6 +232,107 @@ void test_keys_remove_subkey_key()
232
232
233
233
}
234
234
235
+ void test_remove_sibling_subkeys ()
236
+ {
237
+ int len ;
238
+ int ret ;
239
+ int type ;
240
+ size_t off = 0 ;
241
+ char * buf ;
242
+ size_t size ;
243
+ char * out_buf ;
244
+ size_t out_size ;
245
+ char * json ;
246
+ char final_json [2048 ] = {0 };
247
+ msgpack_unpacked result ;
248
+ msgpack_unpacked result_final ;
249
+ msgpack_object map ;
250
+ struct flb_mp_accessor * mpa ;
251
+ struct mk_list patterns ;
252
+
253
+ /* Sample JSON message */
254
+ json =
255
+ "{"
256
+ "\"kubernetes\": {"
257
+ "\"pod_id\": \"id\","
258
+ "\"pod_name\": \"name\","
259
+ "\"host\": \"localhost\","
260
+ "\"labels\": {"
261
+ "\"app\": \"myapp\","
262
+ "\"tier\": \"backend\""
263
+ "}"
264
+ "},"
265
+ "\"msg\": \"kernel panic\""
266
+ "}" ;
267
+
268
+ /* Convert to msgpack */
269
+ len = strlen (json );
270
+ ret = flb_pack_json (json , len , & buf , & size , & type , NULL );
271
+ TEST_CHECK (ret == 0 );
272
+ if (ret == -1 ) {
273
+ exit (EXIT_FAILURE );
274
+ }
275
+
276
+ /* Unpack the content */
277
+ msgpack_unpacked_init (& result );
278
+ msgpack_unpack_next (& result , buf , size , & off );
279
+ map = result .data ;
280
+
281
+ /* Create list of patterns */
282
+ flb_slist_create (& patterns );
283
+
284
+ /* sub key -> key */
285
+ flb_slist_add (& patterns , "$kubernetes['pod_name']" );
286
+ flb_slist_add (& patterns , "$kubernetes['foo']" );
287
+ flb_slist_add (& patterns , "$kubernetes['pod_id']" );
288
+ flb_slist_add (& patterns , "$kubernetes['labels']['tier']" );
289
+ flb_slist_add (& patterns , "$time" );
290
+
291
+
292
+ /* Create mp accessor */
293
+ mpa = flb_mp_accessor_create (& patterns );
294
+ TEST_CHECK (mpa != NULL );
295
+
296
+ /* Remove the entry that matches the pattern(s) */
297
+ ret = flb_mp_accessor_keys_remove (mpa , & map , (void * ) & out_buf , & out_size );
298
+ TEST_CHECK (ret == FLB_TRUE );
299
+
300
+ printf ("\n=== ORIGINAL ===\n" );
301
+ flb_pack_print (buf , size );
302
+ flb_free (buf );
303
+
304
+ printf ("=== FINAL MAP ===\n" );
305
+ if (ret == FLB_TRUE ) {
306
+ flb_pack_print (out_buf , out_size );
307
+ }
308
+ msgpack_unpacked_destroy (& result );
309
+
310
+ off = 0 ;
311
+ msgpack_unpacked_init (& result_final );
312
+ msgpack_unpack_next (& result_final , out_buf , out_size , & off );
313
+ flb_msgpack_to_json (& final_json [0 ], sizeof (final_json ), & result_final .data );
314
+
315
+ if (!TEST_CHECK (strstr (& final_json [0 ] ,"pod_id" ) == NULL )) {
316
+ TEST_MSG ("pod_id field should be removed" );
317
+ }
318
+ if (!TEST_CHECK (strstr (& final_json [0 ] ,"pod_name" ) == NULL )) {
319
+ TEST_MSG ("pod_name field should be removed" );
320
+ }
321
+ if (!TEST_CHECK (strstr (& final_json [0 ] ,"tier" ) == NULL )) {
322
+ TEST_MSG ("tier field should be removed" );
323
+ }
324
+ if (!TEST_CHECK (strstr (& final_json [0 ] ,"app" ) != NULL )) {
325
+ TEST_MSG ("app field should not be removed" );
326
+ }
327
+
328
+ msgpack_unpacked_destroy (& result_final );
329
+
330
+ flb_free (out_buf );
331
+ flb_mp_accessor_destroy (mpa );
332
+ flb_slist_destroy (& patterns );
333
+
334
+ }
335
+
235
336
void remove_subkey_keys (char * list [], int list_size , int index_start )
236
337
{
237
338
int len ;
@@ -444,5 +545,6 @@ TEST_LIST = {
444
545
{"accessor_keys_remove_subkey_key" , test_keys_remove_subkey_key },
445
546
{"accessor_keys_remove_subkey_keys" , test_keys_remove_subkey_keys },
446
547
{"object_to_cfl_to_msgpack" , test_object_to_cfl_to_msgpack },
548
+ {"test_remove_sibling_subkeys" , test_remove_sibling_subkeys },
447
549
{ 0 }
448
550
};
0 commit comments