Skip to content

Feature: Option to handle empty filter results #22

Merged
outscale-mgo merged 2 commits intooutscale:mainfrom
MisterAssm:feat/opt-none-find-return
Mar 24, 2026
Merged

Feature: Option to handle empty filter results #22
outscale-mgo merged 2 commits intooutscale:mainfrom
MisterAssm:feat/opt-none-find-return

Conversation

@MisterAssm
Copy link
Contributor

📦 Pull Request Template

Description

At @outscale-mgo request, and to comply with the jsonpath-ng convention, an option has been added to return an empty array instead of None values for the find_first and find_all functions.

This feature is supported in both Python and C. The unit tests have been updated to test this feature (C and Python).

No breaking changes. The default behavior and the previous behavior (returns None if the filters return no results) remain unchanged. Please see the files tests/json-c/filter.c and tests/python/test_filter.py

Fixes: # (if applicable)

Type of Change

Please check the relevant option(s):

  • 🐛 Bug fix
  • ✨ New feature
  • 🧹 Code cleanup or refactor
  • 📝 Documentation update
  • 🔧 Build or CI-related change
  • 🔒 Security fix
  • Other (specify):

How Has This Been Tested?

Please describe the test strategy:

  • Manual testing
  • Unit tests
  • Integration tests
  • Not tested yet

Commands used (if applicable):

# Example my-cli-tool build --verbose my-cli-tool test

Checklist

  • I have followed the Contributing Guidelines
  • I have added tests or explained why they are not needed
  • I have updated relevant documentation (README, examples, etc.)
  • My changes follow the Conventional Commits specification
  • My commits include appropriate Gitmoji

Additional Context

Add any additional context or screenshots if necessary.

}

if (csonpath_init(ret, s) < 0) {
PyErr_NoMemory();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pretty sure, this should be return, so py_ret = PyErr_NoMemory();

PyCsonPathObject *self = (PyCsonPathObject *)subtype->tp_alloc(subtype, 0);
const char *s;
int return_empty_list = 0;
struct csonpath *ret = NULL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyObject *py_ret = NULL;

free(ret);
error:
Py_DECREF(self);
return NULL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return py_ret;


PyObject *ret = csonpath_find_all(self->cp, json);

if (ret == Py_None && self->return_empty_list) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be already done when cjp->return_empty_array = 1


PyObject *ret = csonpath_find_first(self->cp, json);

if (ret == Py_None && self->return_empty_list) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep this one, because find_first in C should not do the list creation.
or keep python consistant with C and maybe doing this for find_first was a bad idea

csonpath_do.h Outdated
CSONPATH_DO_POST_OPERATION;

return ret;
#ifdef CSONPATH_DO_RETURNS_JSON_PTR

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this, should be done in CSONPATH_DO_FIND_ALL_OUT , so I think the define just add code duplication

@MisterAssm MisterAssm force-pushed the feat/opt-none-find-return branch from 75842c1 to 37d1aa8 Compare March 24, 2026 14:41
Copy link
Contributor

@outscale-mgo outscale-mgo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the indentation could be better

@outscale-mgo outscale-mgo merged commit 6b45b8b into outscale:main Mar 24, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants