adriank / ObjectPath

The agile query language for semi-structured data

Home Page:http://objectpath.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature request] Named selector and removal operator

felixhao28 opened this issue · comments

For a family like this (the actually data can go very deep):

name: Adam
gender: male
children:
  - name: Bran
    gender: male
  - name: Cindy
    gender: female
    children:
      - name: David
        gender: male
        children:
          - name: Helen
            gender: female
      - name: Eva
        gender: female
  - name: Frank
    gender: male
    children:
      - name: George
        gender: male

I'd like to find Cindy and all female offsprings of Cindy. There will be three steps to take:

  1. Find Cindy: $..*[@.name is 'Cindy'].name => Cindy
  2. Find all female offsprings of Cindy: $..*[@.name is 'Cindy'].children..*[@.gender is female].name => Helen, Eva
  3. Add them together: $..*[@.name is 'Cindy'].name + $..*[@.name is 'Cindy'].children..*[@.gender is female].name => Cindy, Helen, Eva

And now you can see the pattern is already unbearable long due to repetition of finding Cindy pattern. In more complex cases, repetition will make the pattern practically unmaintainable. Currently in my application, I am using a following pattern (with a self-written preprocessor):

$cindy := $..*[@.name is 'Cindy']
$cindy.name + $cindy.children..*[@.gender is female].name

This expands to $..*[@.name is 'Cindy'].name + $..*[@.name is 'Cindy'].children..*[@.gender is female].name.

Another thing I am looking for is the ability to delete certain objects from collection. Suppose I need to find Cindy and all female offsprings of Cindy except David's children. I don't know if it is even possible at the current stage. Since we have '+' for union, I propose use '-' for subtraction.

(The assign operators are aligned only for aesthetics reasons)

$cindy           := $..*[@.name is 'Cindy']
$cindy_bloodline := $cindy.name + $cindy.children..*[@.gender is female].name
$david_bloodline := $..*[@.name is 'David'].children..*.name
$cindy_bloodline - $david_bloodline

recursive descent operator (..) traverses the whole tree only once

So there is no caching mechanism? Since ObjectPath is a strictly immutable language, I assumed reusing the same pattern will retrieve the cached result instead of re-evaluating.

There's another issue when using Python to store the results: how do you manage multiple result collections, like A + B?

Could you please extract this feature request to another issue?

Sure