Skip to content

Custom Column Type #273

Open
Open
@amanbolat

Description

@amanbolat

Is your feature request related to a problem? Please describe.

Currently, go-jet generator supports only a limited type of columns. For example, for uuid, text or bytea columns only the postgres.ColumnString type is used.

The issues I'm facing at the moment is I cannot derive the real type of the column, and therefore it's impossible to dynamically build WHERE clause.

Example SQL table:

CREATE TABLE table1 (
    id              UUID PRIMARY KEY,
    value           TEXT
);

Now, I have a function that creates postgres.BoolExpression based on the column type:

func (b JetFilterBuilder) buildEqExpression(col postgres.Column, value string) (postgres.BoolExpression, error) {
	switch typedCol := col.(type) {
	case postgres.ColumnString:
		return typedCol.EQ(postgres.String(value)), nil
        // Skip rest of the code.
}

If I use table.Table1.Id column and pass some text value, it will create SQL like this one:

WHERE id = 'value'::text

Describe the solution you'd like

Options

Option 1

Enable the user to use custom column type during the generation. Right now, I think it's not possible using the generator.
Example of my attempt:

return tableBuilder.UseColumn(func(column metadata.Column) template.TableSQLBuilderColumn {
	if column.DataType.Name == "uuid" {
		return template.TableSQLBuilderColumn{
			Name: column.Name,
			Type: "UUID",
		}
	}

	return template.DefaultTableSQLBuilderColumn(column)
})

Will generate:

type table1 struct {
	postgres.Table

	//Columns
	id               postgres.ColumnUUID // <---------- type doesn't exists.
|

Would be nice to have the same ability to pass a type, as it's done for model field type.

Option 2

Add real column type and other metadata to the Column instance. However, it might increase the size of the column object.

My current solution

I run a tool that will parse the AST tree of the generated file and change the column type to the custom one.

Custom column type example:

type ColumnUUID struct {
	postgres.ColumnString
}

func UUIDColumn(val string) ColumnUUID {
	return ColumnUUID{postgres.StringColumn(val)}
}

Final goal

Assume I had the ability to introduce my custom column, my code would look like:

func (b JetFilterBuilder) buildEqExpression(col postgres.Column, value string) (postgres.BoolExpression, error) {
	switch typedCol := col.(type) {
	case postgres.ColumnString:
		return typedCol.EQ(postgres.String(value)), nil
	case custom_package.ColumnUUID:
                uuidVal := parseUUID(value)
		return typedCol.EQ(postgres.UUID(uuidVal)), nil
        // Skip rest of the code.
}

Thoughts

Maybe I missed something, and it's still possible to achieve my goal without any changes to the current version of go-jet, yet I haven't found one.

When I faced the issue, it seemed to me that there is no value in not exposing jet internal interfaces and structs, that could make other developer's life much easier, by extending the library with custom types and other features.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions