webheroesinc / restruct

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

Re-design command API #1

Open mjbrisebois opened 6 years ago

mjbrisebois commented 6 years ago

The Restruct API should be re-designed so that an object will remain untouched if there are no Restruct commands in it. For example, the command value "true" tells restruct to extract the value based on the "key" string. The only way to get a raw value of "true" would be "= true". The same goes for any integers, which makes this a very intrusive design. Furthermore, we should try and minimize the number of different commands there are.

The command API should be limited to just strings so that any non-string values are guaranteed to be untouched.

To limit the different string commands, there should only be one string prefix command and one inline indicator.

Requirements:

mjbrisebois commented 6 years ago

"=" would be the only reserved prefix indicator "{{ }}" would be the only reserved inline indicator

data = {
    name: "Robin Williams",
    first: "Robin",
    last: "Williams",
    age: 63,
    funny: true
};

"= this.age" // 63
"= this.deceased" // true
"= this.name" // "Robin Williams"

"Hello, {{ first }} {{ last }}!" // "Hello, Robin Williams!"
"The {{= this.funny ? 'hilarious' : 'boring' }} {{ name }} was {{ age }} years old" // "The hilarious Robin Williams was 63 years old"
"\{{ unformatted }} \{{ name }}" // "{{ unformatted }} Robin Williams"
"\{{= unrendered }} {{ name }}" // "{{= unrendered }} Robin Williams"

"= '\{{ unformatted }} \{{ name }}'" // "{{ unformatted }} {{ name }}"
"= '\{{= unrendered }} {{ name }}'" // "{{= unrendered }} {{ name }}"

"\= '{{ unrendered }} {{ name }}'" // "= {{ unrendered }} {{ name }}"
mjbrisebois commented 6 years ago

Manual type enforcement

"= int( this.age )" // 63
"= float( this.age )" // 63.0
"= array( this.first, this.last )" // [ "Robin", "Williams" ]
"= array( [this.first, this.last] )" // [ "Robin", "Williams" ]
"= object( this )" // { ... }
"= (int) this.age" // 63

Strict type enforcement

"@int = this.age" // 63
"@float = this.age" // 63.0
"@array = [this.first, this.last]" // [ "Robin", "Williams" ]
"@object = this" // { ... }

Alternative type cast formats

"=int= this.age" // 63
"=float= this.age" // 63.0
"=array= [this.first, this.last]" // [ "Robin", "Williams" ]
"=object= this" // { ... }
mottersheadt commented 6 years ago

Here's an idea!

We simply add a populator method for doing restruct that has the $ namespace (or any other character; doesn't really matter). That way, all restruct-ing could happen like this. $(input.message)

In order to enforce types, we can do something like this:

$( input.email:string )
$( input.age:int )

or this:

$( input.email ):string
$( input.age ):int

or this:

$:string( input.message )
$:int( input.age )
mjbrisebois commented 6 years ago

Another behavior to think about is, what happens when the key is an array or an object?