github.com/mitranim/sqlb@v0.7.2/t_example_jel_test.go (about)

     1  package sqlb_test
     2  
     3  import (
     4  	"fmt"
     5  	r "reflect"
     6  	"time"
     7  
     8  	"github.com/mitranim/sqlb"
     9  )
    10  
    11  func ExampleJel() {
    12  	type Internal struct {
    13  		InternalTime *time.Time `json:"internalTime" db:"internal_time"`
    14  	}
    15  
    16  	type External struct {
    17  		ExternalName string   `json:"externalName" db:"external_name"`
    18  		Internal     Internal `json:"internal"     db:"internal"`
    19  	}
    20  
    21  	const src = `
    22  		["and",
    23  			["or",
    24  				false,
    25  				["=", "externalName", ["externalName", "literal string"]]
    26  			],
    27  			["and",
    28  				true,
    29  				["<", "internal.internalTime", ["internal.internalTime", "9999-01-01T00:00:00Z"]]
    30  			]
    31  		]
    32  	`
    33  
    34  	expr := sqlb.Jel{
    35  		Type: r.TypeOf((*External)(nil)).Elem(),
    36  		Text: src,
    37  	}
    38  
    39  	text, args := sqlb.Reify(expr)
    40  
    41  	fmt.Println(string(text))
    42  	fmt.Printf("%#v\n", args)
    43  
    44  	// Output:
    45  	// (($1 or ("external_name" = $2)) and ($3 and (("internal")."internal_time" < $4)))
    46  	// []interface {}{false, "literal string", true, time.Date(9999, time.January, 1, 0, 0, 0, 0, time.UTC)}
    47  }