go-jet / jet

Type safe SQL builder with code generation and automatic query result data mapping
Apache License 2.0
2.23k stars 110 forks source link

Postgres Enum Support #290

Closed safaci2000 closed 4 months ago

safaci2000 commented 7 months ago

Describe the bug Code generation fails for Postgres Enums.

--
-- Name: task_type; Type: TYPE; Schema: tasks; Owner: -
--

CREATE TYPE tasks.task_type AS ENUM (
    'S3:INIT',
    'PCAP:MERGE',
    'CTRL:SUBMIT',
    'CTRL:STOP'
);

CREATE TABLE tasks.task (
    task_id bigint NOT NULL,
    task_reference text,
    context_id smallint,
    task_type tasks.task_type NOT NULL,
    task_status tasks.task_status NOT NULL,
    creation_date timestamp with time zone DEFAULT now() NOT NULL,
    last_modified timestamp with time zone DEFAULT now() NOT NULL,
    task_data jsonb DEFAULT '{}'::jsonb,
    CONSTRAINT referencechk CHECK ((char_length(task_reference) <= 20))
);

jet -dsn="postgresql://ht_controller_user:secret@localhost:5432/funcap_controller?sslmode=disable" -schema=tasks -path=./.gen Connecting to postgres database... Retrieving schema information... FOUND 2 table(s), 0 view(s), 2 enum(s) Destination directory: .gen/funcap_controller/tasks Cleaning up destination directory... Generating table model files... Generating enum model files... Error trace:

Environment (please complete the following information):

Code snippet

Generated Code:

const (
    TaskType_S3:init TaskType = "S3:INIT"
    TaskType_Pcap:merge TaskType = "PCAP:MERGE"
    TaskType_Ctrl:submit TaskType = "CTRL:SUBMIT"
    TaskType_Ctrl:stop TaskType = "CTRL:STOP"
)

Obviously this fails as it's invalid Go syntax.

Expected behavior The code to actually generate without errors.

houten11 commented 7 months ago

You can customize the generator, and replace : character with a character of your choosing. https://github.com/go-jet/jet/wiki/Generator#generator-customization

safaci2000 commented 7 months ago

You can customize the generator, and replace : character with a character of your choosing. https://github.com/go-jet/jet/wiki/Generator#generator-customization

I'm assuming that the only way to do this, is if you invoke the Generator from code?

go-jet commented 7 months ago

I've made a quick fix for this bug on bug290 branch. Special characters are removed from go identifiers.

safaci2000 commented 7 months ago

Sounds good, thank you!

go-jet commented 4 months ago

I've changed my mind on this one. There were couple of bugs opened before for similar invalid character issues. To prevent any additional bugs from opening, I've made a change on master that does not remove but replaces all invalid ascii characters with the description. So in your case, it would be S3ColonInit for S3:Init enum value.

safaci2000 commented 4 months ago

That seems like a perfectly reasonable change. Thanks for the update.

go-jet commented 4 months ago

Fixed in Release v2.11.0.