jet icon indicating copy to clipboard operation
jet copied to clipboard

Postgres Enum Support

Open safaci2000 opened this issue 1 year ago • 6 comments
trafficstars

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:

  • failed to generate schema tasks:
  • &{%!d(string=failed to generate model types:
  • failed to process enum types:
  • failed to save 'task_type' enum type:
  • failed to format 'task_type', check '.gen/funcap_controller/tasks/model/task_type.go' for syntax errors:
  • 16:13:
  • expected ';', found ':' (and 3 more errors)) 1374391779904}






**Environment (please complete the following information):**
 - OS: [e.g. linux, windows, macosx]
 - Database: [e.g. postgres, mysql, sqlite]
-  Database driver: [e.g. pq, pgx]
 - Jet version [e.g. 2.6.0 or branch name]

**Code snippet**

Generated Code:

```go
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.

safaci2000 avatar Nov 27 '23 13:11 safaci2000

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

houtn11 avatar Nov 28 '23 10:11 houtn11

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?

safaci2000 avatar Nov 28 '23 16:11 safaci2000

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

go-jet avatar Nov 28 '23 19:11 go-jet

Sounds good, thank you!

safaci2000 avatar Nov 28 '23 21:11 safaci2000

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.

go-jet avatar Feb 17 '24 12:02 go-jet

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

safaci2000 avatar Feb 17 '24 20:02 safaci2000

Fixed in Release v2.11.0.

go-jet avatar Feb 28 '24 13:02 go-jet