rql
rql copied to clipboard
add field naming support
This allows setting an external naming convention that is different than the db column and will map accordingly.
User can query filter:{someField:5}
and it will be mapped to where some_field = ?
following the functions.
Needs documentation, tests coverage, will add next.
User can add name
struct tag to specify the name manually or they can add FieldNameFn
to the config.
it should be backwards compatible as it default to the current behavior. There is 1 TODO that I'm not sure is correct.
Hey @Ashtonian, thanks for your contribution.
In the current API, you can either set a column
tag to override the column name in SQL, or provide your own ColumnFn
that will do this mapping for you.
What use case doesn't not work you?
I have conflicting naming schemas. This is being used for an api that returns json and uses a cammelCase naming convention, where the db uses _. I would like the users to be able to query the external naming convention instead of the internal one and I would like rql to map them correctly. Also planning on using this inside another template language that follows different naming convention and would like to have control over external naming vs internal.
type Thing struct {
ID int `db:"id" rql:"filter,sort" json:"id,omitempty" example:"5"`
CreatedAt time.Time `db:"created_at" rql:"filter,sort,column=created_at,name=createdAt" json:"createdAt,omitempty" example:"2018-06-25T23:37:02.445322Z"`
UpdatedAt time.Time `db:"updated_at" rql:"filter,sort,column=updated_at,name=updatedAt" json:"updatedAt,omitempty" example:"2018-06-25T23:37:02.445322Z"`
}
// given
Query
{
filter:[]byte(`{
"filter": {
"createdAt": "2019"
},
"sort": ["-createdAt"]
}`),
}
// Desired
Params:{
FilterExp: "created_at = ?",
SortExp: "created_at desc"
}
// Current
Params:{
FilterExp: "createdAt = ?",
SortExp: "createdAt desc"
}
To allow for this mapping behavior I renamed the old field.Name
and references to field.ColumnName
, and reintroduced the field.Name
field. This name field can be populated by a function passed into the config, or using the new name=
struct tag. If neither are implemented the name field defaults back to field.ColumnName
to maintain current behavior.
do you have any additional feedback or thoughts ?
@a8m can we get this merged? I'm looking forward to these changes!
@a8m sorry for the delay, been working on other projects. I agree with your feedback and think a converterFn would be a better approach then variable defaulting. Not sure when I will be able to get back to this unfortunately.