@@ -2,6 +2,7 @@ package quamina
22
33import (
44 "bytes"
5+ "fmt"
56 "os"
67 "testing"
78)
@@ -315,6 +316,76 @@ func TestFJErrorCases(t *testing.T) {
315316 }
316317}
317318
319+ func TestSkipUnusedPaths (t * testing.T ) {
320+ // Each of theses cases has a nested object with additional values that should
321+ // be skipped after the specified paths have been checked
322+ //
323+ // e.g., take the following object and paths:
324+ //
325+ // object: { "a": { "b": 1, "c": 2} }
326+ // paths: ["a\nb", "d"]
327+ //
328+ // After the flattener evaluates a.b, it should skip a.c before looking
329+ // for d in the outer object.
330+ //
331+ // These tests make sure that the flattener correctly skips remaining
332+ // values including nested objects and arrays.
333+ //
334+ // The tests below contain an additional path to look for after the
335+ // paths in the nested object to make sure the flattener correctly
336+ // exits the nested object and begings parsing the rest of the event
337+ // at the correct location.
338+ cases := []struct {
339+ event string
340+ matcherPaths []string
341+ }{
342+ {
343+ event : `{"nested":{"thing":"whatever","extra":{}}}` ,
344+ matcherPaths : []string {"nested\n thing" , "another" },
345+ },
346+ {
347+ event : `{"nested":{"thing":"whatever","extra":{"empty": false}}}` ,
348+ matcherPaths : []string {"nested\n thing" , "another" },
349+ },
350+ {
351+ event : `{"nested":{"thing":"whatever","extra":[{}]}}` ,
352+ matcherPaths : []string {"nested\n thing" , "another" },
353+ },
354+ {
355+ event : `{"nested":{"thing":"whatever","extra":[{"empty": false}]}}` ,
356+ matcherPaths : []string {"nested\n thing" , "another" },
357+ },
358+ {
359+ event : `{"nested":{"thing":"whatever","extra":[]}}` ,
360+ matcherPaths : []string {"nested\n thing" , "another" },
361+ },
362+ {
363+ event : `{"nested":{"thing":"whatever","extra":[1,"two",true,null]}}` ,
364+ matcherPaths : []string {"nested\n thing" , "another" },
365+ },
366+ {
367+ event : `{"nested":{"thing":"whatever","extra":[],"andAnother":{}}}` ,
368+ matcherPaths : []string {"nested\n thing" , "another" },
369+ },
370+ }
371+
372+ for i , c := range cases {
373+ t .Run (fmt .Sprintf ("case_%d" , i ), func (t * testing.T ) {
374+ matcher := fakeMatcher (c .matcherPaths ... )
375+
376+ fj := newJSONFlattener ().(* flattenJSON )
377+
378+ // ignore the fields - this test isn't concerned with the flattening result
379+ _ , err := fj .Flatten ([]byte (c .event ), matcher .getSegmentsTreeTracker ())
380+
381+ // make sure the flattener didn't return an error
382+ if err != nil {
383+ t .Fatalf ("failed to flatten json: %v" , err )
384+ }
385+ })
386+ }
387+ }
388+
318389func fakeMatcher (paths ... string ) * coreMatcher {
319390 m := newCoreMatcher ()
320391 for _ , path := range paths {
0 commit comments