h2non / jsonpath-ng

Finally, a JSONPath implementation for Python that aims to be standard compliant. That's all. Enjoy!
Apache License 2.0
582 stars 85 forks source link

Cannot filter leaf objects content #64

Closed fpom closed 4 months ago

fpom commented 3 years ago

I try to match parse trees obtained from clang -cc1 -dump-ast=json, it works well expect for the leaf objects which I cannot filter against their content.

For instance, on the data provided below, I can do:

import json, pprint
from jsonpath_ng.ext import parse as jp_parse

data = json.load(open("foo.json"))

expr = jp_parse("$..*[?kind='FunctionDecl' & name='fact']"
                "..*[?kind='CallExpr']"
                "..referencedDecl")
for i, match in enumerate(expr.find(data)) :
    print(f"### {i} ###")
    pprint.pprint(match.value)

This prints two matches:

### 0 ###
{'id': '0x6fdad08',
 'kind': 'FunctionDecl',
 'name': 'fact',
 'type': {'qualType': 'int (int)'}}
### 1 ###
{'id': '0x6fdac30',
 'kind': 'ParmVarDecl',
 'name': 'n',
 'type': {'qualType': 'int'}}

But then, if I change the end of the expression to apply filters on the matches above, it does not match anything. For instance, replacing the third line of the expression with "..referencedDecl[?kind='FunctionDecl' & name='fact']", or "..referencedDecl[?name='fact']", or even "..referencedDecl[?name]"does not return any match.

Either there is something I missed in expressions meaning, or this may be a bug.

Thanks in advance for your help. Franck

The data in foo.json:

{
  "id": "0x6f9b4f8",
  "kind": "TranslationUnitDecl",
  "loc": {},
  "range": {
    "begin": {},
    "end": {}
  },
  "inner": [
    {
      "id": "0x6f9bdb8",
      "kind": "TypedefDecl",
      "loc": {},
      "range": {
        "begin": {},
        "end": {}
      },
      "isImplicit": true,
      "name": "__int128_t",
      "type": {
        "qualType": "__int128"
      },
      "inner": [
        {
          "id": "0x6f9ba90",
          "kind": "BuiltinType",
          "type": {
            "qualType": "__int128"
          }
        }
      ]
    },
    {
      "id": "0x6f9be28",
      "kind": "TypedefDecl",
      "loc": {},
      "range": {
        "begin": {},
        "end": {}
      },
      "isImplicit": true,
      "name": "__uint128_t",
      "type": {
        "qualType": "unsigned __int128"
      },
      "inner": [
        {
          "id": "0x6f9bab0",
          "kind": "BuiltinType",
          "type": {
            "qualType": "unsigned __int128"
          }
        }
      ]
    },
    {
      "id": "0x6f9c130",
      "kind": "TypedefDecl",
      "loc": {},
      "range": {
        "begin": {},
        "end": {}
      },
      "isImplicit": true,
      "name": "__NSConstantString",
      "type": {
        "qualType": "struct __NSConstantString_tag"
      },
      "inner": [
        {
          "id": "0x6f9bf00",
          "kind": "RecordType",
          "type": {
            "qualType": "struct __NSConstantString_tag"
          },
          "decl": {
            "id": "0x6f9be80",
            "kind": "RecordDecl",
            "name": "__NSConstantString_tag"
          }
        }
      ]
    },
    {
      "id": "0x6f9c1d8",
      "kind": "TypedefDecl",
      "loc": {},
      "range": {
        "begin": {},
        "end": {}
      },
      "isImplicit": true,
      "name": "__builtin_ms_va_list",
      "type": {
        "qualType": "char *"
      },
      "inner": [
        {
          "id": "0x6f9c190",
          "kind": "PointerType",
          "type": {
            "qualType": "char *"
          },
          "inner": [
            {
              "id": "0x6f9b590",
              "kind": "BuiltinType",
              "type": {
                "qualType": "char"
              }
            }
          ]
        }
      ]
    },
    {
      "id": "0x6fdabc0",
      "kind": "TypedefDecl",
      "loc": {},
      "range": {
        "begin": {},
        "end": {}
      },
      "isImplicit": true,
      "name": "__builtin_va_list",
      "type": {
        "qualType": "struct __va_list_tag [1]"
      },
      "inner": [
        {
          "id": "0x6f9c470",
          "kind": "ConstantArrayType",
          "type": {
            "qualType": "struct __va_list_tag [1]"
          },
          "size": 1,
          "inner": [
            {
              "id": "0x6f9c2b0",
              "kind": "RecordType",
              "type": {
                "qualType": "struct __va_list_tag"
              },
              "decl": {
                "id": "0x6f9c230",
                "kind": "RecordDecl",
                "name": "__va_list_tag"
              }
            }
          ]
        }
      ]
    },
    {
      "id": "0x6fdad08",
      "kind": "FunctionDecl",
      "loc": {
        "offset": 4,
        "file": "foo.c",
        "line": 1,
        "col": 5,
        "tokLen": 4
      },
      "range": {
        "begin": {
          "offset": 0,
          "col": 1,
          "tokLen": 3
        },
        "end": {
          "offset": 88,
          "line": 7,
          "col": 1,
          "tokLen": 1
        }
      },
      "isReferenced": true,
      "name": "fact",
      "mangledName": "fact",
      "type": {
        "qualType": "int (int)"
      },
      "inner": [
        {
          "id": "0x6fdac30",
          "kind": "ParmVarDecl",
          "loc": {
            "offset": 14,
            "line": 1,
            "col": 15,
            "tokLen": 1
          },
          "range": {
            "begin": {
              "offset": 10,
              "col": 11,
              "tokLen": 3
            },
            "end": {
              "offset": 14,
              "col": 15,
              "tokLen": 1
            }
          },
          "isUsed": true,
          "name": "n",
          "mangledName": "n",
          "type": {
            "qualType": "int"
          }
        },
        {
          "id": "0x6fdb068",
          "kind": "CompoundStmt",
          "range": {
            "begin": {
              "offset": 17,
              "col": 18,
              "tokLen": 1
            },
            "end": {
              "offset": 88,
              "line": 7,
              "col": 1,
              "tokLen": 1
            }
          },
          "inner": [
            {
              "id": "0x6fdb040",
              "kind": "IfStmt",
              "range": {
                "begin": {
                  "offset": 21,
                  "line": 2,
                  "col": 3,
                  "tokLen": 2
                },
                "end": {
                  "offset": 86,
                  "line": 6,
                  "col": 3,
                  "tokLen": 1
                }
              },
              "hasElse": true,
              "inner": [
                {
                  "id": "0x6fdae50",
                  "kind": "BinaryOperator",
                  "range": {
                    "begin": {
                      "offset": 25,
                      "line": 2,
                      "col": 7,
                      "tokLen": 1
                    },
                    "end": {
                      "offset": 30,
                      "col": 12,
                      "tokLen": 1
                    }
                  },
                  "type": {
                    "qualType": "int"
                  },
                  "valueCategory": "rvalue",
                  "opcode": "<=",
                  "inner": [
                    {
                      "id": "0x6fdae38",
                      "kind": "ImplicitCastExpr",
                      "range": {
                        "begin": {
                          "offset": 25,
                          "col": 7,
                          "tokLen": 1
                        },
                        "end": {
                          "offset": 25,
                          "col": 7,
                          "tokLen": 1
                        }
                      },
                      "type": {
                        "qualType": "int"
                      },
                      "valueCategory": "rvalue",
                      "castKind": "LValueToRValue",
                      "inner": [
                        {
                          "id": "0x6fdadf8",
                          "kind": "DeclRefExpr",
                          "range": {
                            "begin": {
                              "offset": 25,
                              "col": 7,
                              "tokLen": 1
                            },
                            "end": {
                              "offset": 25,
                              "col": 7,
                              "tokLen": 1
                            }
                          },
                          "type": {
                            "qualType": "int"
                          },
                          "valueCategory": "lvalue",
                          "referencedDecl": {
                            "id": "0x6fdac30",
                            "kind": "ParmVarDecl",
                            "name": "n",
                            "type": {
                              "qualType": "int"
                            }
                          }
                        }
                      ]
                    },
                    {
                      "id": "0x6fdae18",
                      "kind": "IntegerLiteral",
                      "range": {
                        "begin": {
                          "offset": 30,
                          "col": 12,
                          "tokLen": 1
                        },
                        "end": {
                          "offset": 30,
                          "col": 12,
                          "tokLen": 1
                        }
                      },
                      "type": {
                        "qualType": "int"
                      },
                      "valueCategory": "rvalue",
                      "value": "0"
                    }
                  ]
                },
                {
                  "id": "0x6fdaea0",
                  "kind": "CompoundStmt",
                  "range": {
                    "begin": {
                      "offset": 33,
                      "col": 15,
                      "tokLen": 1
                    },
                    "end": {
                      "offset": 51,
                      "line": 4,
                      "col": 3,
                      "tokLen": 1
                    }
                  },
                  "inner": [
                    {
                      "id": "0x6fdae90",
                      "kind": "ReturnStmt",
                      "range": {
                        "begin": {
                          "offset": 39,
                          "line": 3,
                          "col": 5,
                          "tokLen": 6
                        },
                        "end": {
                          "offset": 46,
                          "col": 12,
                          "tokLen": 1
                        }
                      },
                      "inner": [
                        {
                          "id": "0x6fdae70",
                          "kind": "IntegerLiteral",
                          "range": {
                            "begin": {
                              "offset": 46,
                              "col": 12,
                              "tokLen": 1
                            },
                            "end": {
                              "offset": 46,
                              "col": 12,
                              "tokLen": 1
                            }
                          },
                          "type": {
                            "qualType": "int"
                          },
                          "valueCategory": "rvalue",
                          "value": "1"
                        }
                      ]
                    }
                  ]
                },
                {
                  "id": "0x6fdb028",
                  "kind": "CompoundStmt",
                  "range": {
                    "begin": {
                      "offset": 58,
                      "line": 4,
                      "col": 10,
                      "tokLen": 1
                    },
                    "end": {
                      "offset": 86,
                      "line": 6,
                      "col": 3,
                      "tokLen": 1
                    }
                  },
                  "inner": [
                    {
                      "id": "0x6fdb018",
                      "kind": "ReturnStmt",
                      "range": {
                        "begin": {
                          "offset": 64,
                          "line": 5,
                          "col": 5,
                          "tokLen": 6
                        },
                        "end": {
                          "offset": 81,
                          "col": 22,
                          "tokLen": 1
                        }
                      },
                      "inner": [
                        {
                          "id": "0x6fdaff8",
                          "kind": "BinaryOperator",
                          "range": {
                            "begin": {
                              "offset": 71,
                              "col": 12,
                              "tokLen": 1
                            },
                            "end": {
                              "offset": 81,
                              "col": 22,
                              "tokLen": 1
                            }
                          },
                          "type": {
                            "qualType": "int"
                          },
                          "valueCategory": "rvalue",
                          "opcode": "*",
                          "inner": [
                            {
                              "id": "0x6fdafe0",
                              "kind": "ImplicitCastExpr",
                              "range": {
                                "begin": {
                                  "offset": 71,
                                  "col": 12,
                                  "tokLen": 1
                                },
                                "end": {
                                  "offset": 71,
                                  "col": 12,
                                  "tokLen": 1
                                }
                              },
                              "type": {
                                "qualType": "int"
                              },
                              "valueCategory": "rvalue",
                              "castKind": "LValueToRValue",
                              "inner": [
                                {
                                  "id": "0x6fdaeb8",
                                  "kind": "DeclRefExpr",
                                  "range": {
                                    "begin": {
                                      "offset": 71,
                                      "col": 12,
                                      "tokLen": 1
                                    },
                                    "end": {
                                      "offset": 71,
                                      "col": 12,
                                      "tokLen": 1
                                    }
                                  },
                                  "type": {
                                    "qualType": "int"
                                  },
                                  "valueCategory": "lvalue",
                                  "referencedDecl": {
                                    "id": "0x6fdac30",
                                    "kind": "ParmVarDecl",
                                    "name": "n",
                                    "type": {
                                      "qualType": "int"
                                    }
                                  }
                                }
                              ]
                            },
                            {
                              "id": "0x6fdafb8",
                              "kind": "CallExpr",
                              "range": {
                                "begin": {
                                  "offset": 73,
                                  "col": 14,
                                  "tokLen": 4
                                },
                                "end": {
                                  "offset": 81,
                                  "col": 22,
                                  "tokLen": 1
                                }
                              },
                              "type": {
                                "qualType": "int"
                              },
                              "valueCategory": "rvalue",
                              "inner": [
                                {
                                  "id": "0x6fdafa0",
                                  "kind": "ImplicitCastExpr",
                                  "range": {
                                    "begin": {
                                      "offset": 73,
                                      "col": 14,
                                      "tokLen": 4
                                    },
                                    "end": {
                                      "offset": 73,
                                      "col": 14,
                                      "tokLen": 4
                                    }
                                  },
                                  "type": {
                                    "qualType": "int (*)(int)"
                                  },
                                  "valueCategory": "rvalue",
                                  "castKind": "FunctionToPointerDecay",
                                  "inner": [
                                    {
                                      "id": "0x6fdaed8",
                                      "kind": "DeclRefExpr",
                                      "range": {
                                        "begin": {
                                          "offset": 73,
                                          "col": 14,
                                          "tokLen": 4
                                        },
                                        "end": {
                                          "offset": 73,
                                          "col": 14,
                                          "tokLen": 4
                                        }
                                      },
                                      "type": {
                                        "qualType": "int (int)"
                                      },
                                      "valueCategory": "rvalue",
                                      "referencedDecl": {
                                        "id": "0x6fdad08",
                                        "kind": "FunctionDecl",
                                        "name": "fact",
                                        "type": {
                                          "qualType": "int (int)"
                                        }
                                      }
                                    }
                                  ]
                                },
                                {
                                  "id": "0x6fdaf50",
                                  "kind": "BinaryOperator",
                                  "range": {
                                    "begin": {
                                      "offset": 78,
                                      "col": 19,
                                      "tokLen": 1
                                    },
                                    "end": {
                                      "offset": 80,
                                      "col": 21,
                                      "tokLen": 1
                                    }
                                  },
                                  "type": {
                                    "qualType": "int"
                                  },
                                  "valueCategory": "rvalue",
                                  "opcode": "-",
                                  "inner": [
                                    {
                                      "id": "0x6fdaf38",
                                      "kind": "ImplicitCastExpr",
                                      "range": {
                                        "begin": {
                                          "offset": 78,
                                          "col": 19,
                                          "tokLen": 1
                                        },
                                        "end": {
                                          "offset": 78,
                                          "col": 19,
                                          "tokLen": 1
                                        }
                                      },
                                      "type": {
                                        "qualType": "int"
                                      },
                                      "valueCategory": "rvalue",
                                      "castKind": "LValueToRValue",
                                      "inner": [
                                        {
                                          "id": "0x6fdaef8",
                                          "kind": "DeclRefExpr",
                                          "range": {
                                            "begin": {
                                              "offset": 78,
                                              "col": 19,
                                              "tokLen": 1
                                            },
                                            "end": {
                                              "offset": 78,
                                              "col": 19,
                                              "tokLen": 1
                                            }
                                          },
                                          "type": {
                                            "qualType": "int"
                                          },
                                          "valueCategory": "lvalue",
                                          "referencedDecl": {
                                            "id": "0x6fdac30",
                                            "kind": "ParmVarDecl",
                                            "name": "n",
                                            "type": {
                                              "qualType": "int"
                                            }
                                          }
                                        }
                                      ]
                                    },
                                    {
                                      "id": "0x6fdaf18",
                                      "kind": "IntegerLiteral",
                                      "range": {
                                        "begin": {
                                          "offset": 80,
                                          "col": 21,
                                          "tokLen": 1
                                        },
                                        "end": {
                                          "offset": 80,
                                          "col": 21,
                                          "tokLen": 1
                                        }
                                      },
                                      "type": {
                                        "qualType": "int"
                                      },
                                      "valueCategory": "rvalue",
                                      "value": "1"
                                    }
                                  ]
                                }
                              ]
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}
e-d-n-a commented 4 months ago

The problem is, that filters do not work on dicts with this package (see #30)

You have to apply filters on lists, so this expression does work: $..*[?kind='FunctionDecl' & name='fact']..*[?kind='CallExpr']..inner[?referencedDecl.name='fact'].referencedDecl

Full code to test against foo.json:

import json
from jsonpath_ng.ext import parse as jpx

data = json.load(open("foo.json"))

expr = jpx("$..*[?kind='FunctionDecl' & name='fact']"
           "..*[?kind='CallExpr']"
           "..inner[?referencedDecl.name='fact'].referencedDecl")

print([p.value for p in expr.find(data)])

This is an old issue, but this comment should be able to close it with a solution and also due to it being a duplicate.

fpom commented 4 months ago

Thank you!