webheroesinc / restruct

Python, PHP and Javascript implementations for turning SQL results into beautiful JSON structures.
Other
0 stars 1 forks source link

Find a more intuitive way for making arrays #2

Open mjbrisebois opened 6 years ago

mjbrisebois commented 6 years ago

Currently, we use an object to narrow down the results, and then convert the values into an array with the array flattening trigger __array__.

{
    "__array__": true,
    "= this.id": { ... }
}

I am hoping we can find a more intuitive way to do this.

mjbrisebois commented 6 years ago

I think it would be more intuitive to have the trigger outside of the object you are flattening, or have the trigger also be the thing that specifies the unique key.

{
    "=@array( this.id )": { ... },
}

It could also take an optional sort by function that would be run at the end

{
    "=@array( this.id )( sort by fn )": { ... },
}
mjbrisebois commented 6 years ago

Piping to a command with an optional sort function.

{
    "sections | array(<function> sort)": { ... },
}

Or perhaps a special type cast could convert the value. This makes logical sense because type casting is expected to deal with the value.

{
    "@flatten('sections', <function> sort)": { ... },
}

Or maybe this should be the "onCompletion" syntax for doing any sort of finishing touches. For example, if the key ends in a < it pipes the finished result through the chain. It must still support the evaluation syntax.

{
    "[sections] < sort() < flatten() <": { ... },
    "[= echo('sections')] < sort() < flatten() <": { ... }

    // or, assume final pass is the 'key' value
    "sections < sort() < flatten() <": { ... }, 
    "= echo('sections') < sort() < flatten() <": { ... }

    // or, keep the flow from left to right
    "sections@[ flatten() | sort() ]": { ... },
    "= echo('sections') @[ flatten() | sort() ]": { ... }
}

Since methods can make changes to the result, maybe there just needs to be built-in restruct methods. The @ sybol could represent when to run the evaluation. For example

{
    "= Restruct.array('sections')": { ... }
    "sections @end Restruct.array()": { ... }
}