rql icon indicating copy to clipboard operation
rql copied to clipboard

add field naming support

Open ashtonian opened this issue 4 years ago • 5 comments

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.

ashtonian avatar Jul 31 '19 01:07 ashtonian

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?

a8m avatar Aug 01 '19 10:08 a8m

 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.

ashtonian avatar Aug 01 '19 16:08 ashtonian

do you have any additional feedback or thoughts ?

ashtonian avatar Aug 15 '19 18:08 ashtonian

@a8m can we get this merged? I'm looking forward to these changes!

jon-whit avatar Nov 25 '19 22:11 jon-whit

@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.

ashtonian avatar Nov 25 '19 22:11 ashtonian