gen icon indicating copy to clipboard operation
gen copied to clipboard

[error] generate model struct fail: cannot format file: /path/to/Repositories/model/tasks.gen.go:23:2: expected '}', found 0 (and 1 more errors)

Open chase-isabelle-roostify opened this issue 3 years ago • 6 comments

GORM Playground Link

no link - this is a bug in the cli binary

Description

looks like the tool is failing to format some generated code

gentool --db postgres --dsn 'host=localhost user=user password=password dbname=db/development port=5432 sslmode=disable' --tables 'tasks'

...

2022/07/29 16:21:19 /path/to/go/pkg/mod/gorm.io/[email protected]/generator.go:254
[error] generate model struct fail: cannot format file: /path/to/Repositories/model/tasks.gen.go:23:2: expected '}', found 0 (and 1 more errors)
panic: generate model struct fail

goroutine 1 [running]:
gorm.io/gen.(*Generator).Execute(0x1400043c380)
	/path/to/go/pkg/mod/gorm.io/[email protected]/generator.go:255 +0x1f8
main.main()
	/path/to/go/pkg/mod/gorm.io/[email protected]/tools/gentool/gentool.go:207 +0x238

chase-isabelle-roostify avatar Jul 29 '22 20:07 chase-isabelle-roostify

This may actually be caused by a column named 0.

[edit] yep, there's a column named 0, but i guess, since postgres allows this, it should be accounted for, somehow.

chase-isabelle-roostify avatar Jul 29 '22 20:07 chase-isabelle-roostify

seems there's a similar error with other tables

gentool --db postgres --dsn 'host=localhost user=user password=password dbname=db/development port=5432 sslmode=disable' --tables 'users'

2022/07/29 22:34:11 /path/to/go/pkg/mod/gorm.io/driver/[email protected]/migrator.go:369 SLOW SQL >= 200ms
[211.973ms] [rows:-] SELECT c.column_name, c.is_nullable = 'YES', c.udt_name, c.character_maximum_length, c.numeric_precision, c.numeric_precision_radix, c.numeric_scale, c.datetime_precision, 8 * typlen, c.column_default, pd.description FROM information_schema.columns AS c JOIN pg_type AS pgt ON c.udt_name = pgt.typname LEFT JOIN pg_catalog.pg_description as pd ON pd.objsubid = c.ordinal_position AND pd.objoid = (SELECT oid FROM pg_catalog.pg_class WHERE relname = c.table_name AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = c.table_schema)) where table_catalog = 'db/development' AND table_schema = CURRENT_SCHEMA() AND table_name = 'users'

2022/07/29 22:34:11 /path/to/go/pkg/mod/gorm.io/driver/[email protected]/migrator.go:439 SLOW SQL >= 200ms
[214.748ms] [rows:-] SELECT c.column_name, constraint_type FROM information_schema.table_constraints tc JOIN information_schema.constraint_column_usage AS ccu USING (constraint_schema, constraint_name) JOIN information_schema.columns AS c ON c.table_schema = tc.constraint_schema AND tc.table_name = c.table_name AND ccu.column_name = c.column_name WHERE constraint_type IN ('PRIMARY KEY', 'UNIQUE') AND c.table_catalog = 'db/development' AND c.table_schema = CURRENT_SCHEMA() AND c.table_name = 'users'
2022/07/29 22:34:11 got 96 columns from table <users>
2022/07/29 22:34:11 Start generating code.
2022/07/29 22:34:11 generate model file(table <users> -> {model.User}): /path/to/Repositories/model/users.gen.go

2022/07/29 22:34:11 /path/to/go/pkg/mod/gorm.io/[email protected]/generator.go:529
[warn] parse model pkg path fail: err: exit status 1: stderr: go: go.mod file not found in current directory or any parent directory; see 'go help modules'
Format fail: 6 /path/to/Repositories/project/users.gen.go:6:15: expected ';', found '-'
1 // Code generated by gorm.io/gen. DO NOT EDIT.
2 // Code generated by gorm.io/gen. DO NOT EDIT.
3 // Code generated by gorm.io/gen. DO NOT EDIT.
4
5 package project
6
7 import(
8 	"context"
9 "database/sql"
10 "strings"
11
2022/07/29 22:34:11 generate query file: /path/to/Repositories/project/users.gen.go

2022/07/29 22:34:11 /path/to/go/pkg/mod/gorm.io/[email protected]/generator.go:259
[error] generate query code fail: cannot format file: /path/to/Repositories/project/users.gen.go:6:15: expected ';', found '-'
panic: generate query code fail

goroutine 1 [running]:
gorm.io/gen.(*Generator).Execute(0x1400043c380)
	/path/to/go/pkg/mod/gorm.io/[email protected]/generator.go:260 +0x13c
main.main()
	/path/to/go/pkg/mod/gorm.io/[email protected]/tools/gentool/gentool.go:207 +0x238

chase-isabelle-roostify avatar Jul 30 '22 02:07 chase-isabelle-roostify

even though the tool produces this error, it seems like the generated code is actually fine. go fmt ../model/users.gen.go produces no errors, and a quick inspection of the code looks ok.

chase-isabelle-roostify avatar Jul 31 '22 16:07 chase-isabelle-roostify

for some reason, this bug exists in the binary; however, when running via go run tools/gentool/gentool.go it does not.

chase-isabelle-roostify avatar Aug 02 '22 13:08 chase-isabelle-roostify

@chase-isabelle-roostify gen.FieldRename try this model option

But I prefer you to change the field name

cc @cha0ran

qqxhb avatar Aug 10 '22 10:08 qqxhb

yea, it's definitely an edge case and a bad field name; but, i'm keeping the ticket open since it is technically a valid field name. if i have time i will see if i can apply a patch and submit a pr. thanks, @qqxhb .

chase-isabelle-roostify avatar Aug 10 '22 15:08 chase-isabelle-roostify

I got the same error: image

But when I provided the folder path and file name like this: -outPath ./generatedtructs -outFile "generatedStructs.go"

The error got resolved for me.

amirejaz75 avatar Apr 12 '23 07:04 amirejaz75

I have encountered a similar error: "[error] generate model struct fail: cannot format file: /path/tableXXX.gen.go:48:5: expected '}', found 1688 (and 1 more errors) panic: generate model struct fail." It appears that the tool does not support field names with numbers, but this is allowed in MySQL. I hope to find a solution for this issue.

cha0ran avatar May 19 '23 08:05 cha0ran

Oh, I think I understand now. In Go's naming conventions, identifiers cannot start with a number. So, this is probably an unsolvable problem. 🫠

cha0ran avatar May 19 '23 09:05 cha0ran

But I generated it using a binary file, so I can't use the gen.FieldRename method, right? @qqxhb

cha0ran avatar May 19 '23 09:05 cha0ran

I am thinking that perhaps when generating the struct, we can add an "N" or any other letter in front of field names that start with a number. This could potentially resolve the errors encountered during automatic generation.

cha0ran avatar May 19 '23 09:05 cha0ran

But I generated it using a binary file, so I can't use the gen.FieldRename method, right? @qqxhb

Yes. The binary is not very flexible.

qqxhb avatar May 19 '23 09:05 qqxhb