From 673f53d97dcf2833dfeb96351985e3c5dd9d3a20 Mon Sep 17 00:00:00 2001 From: Luca Steeb Date: Tue, 2 Jan 2024 23:44:49 +0700 Subject: [PATCH] fix(generator): consider model type casing (#1144) --- generator/templates/_header.gotpl | 5 +++++ generator/templates/models.gotpl | 4 ++-- generator/templates/query.gotpl | 6 +++--- generator/types/types_test.go | 3 +++ helpers/gocase/gocase_test.go | 2 ++ test/features/composite/schema.prisma | 14 ++++++++++++++ 6 files changed, 29 insertions(+), 5 deletions(-) diff --git a/generator/templates/_header.gotpl b/generator/templates/_header.gotpl index 95a2ba16..72c06a42 100644 --- a/generator/templates/_header.gotpl +++ b/generator/templates/_header.gotpl @@ -34,6 +34,11 @@ const RFC3339Milli = types.RFC3339Milli type BatchResult = types.BatchResult +type Boolean = bool +type String = string +type Int = int +type Float = float64 + type DateTime = types.DateTime type JSON = types.JSON type Bytes = types.Bytes diff --git a/generator/templates/models.gotpl b/generator/templates/models.gotpl index 573dec15..cc641ab1 100644 --- a/generator/templates/models.gotpl +++ b/generator/templates/models.gotpl @@ -37,7 +37,7 @@ type Relations{{ $model.Name.GoCase }} struct { {{ range $field := $model.Fields }} {{- if $field.Kind.IsRelation }} - {{ $field.Name.GoCase }} {{ if $field.IsList }}[]{{ else }}*{{ end }}{{ $field.Type.Value }}Model {{ $field.Name.Tag false }} + {{ $field.Name.GoCase }} {{ if $field.IsList }}[]{{ else }}*{{ end }}{{ $field.Type.GoCase }}Model {{ $field.Name.Tag false }} {{- end -}} {{ end }} } @@ -46,7 +46,7 @@ {{- range $field := $model.Fields }} {{- if or (not $field.IsRequired) ($field.Kind.IsRelation) }} func (r {{ $model.Name.GoCase }}Model) {{ $field.Name.GoCase }}() ( - {{- if $field.IsList }}value []{{ else }}value{{ end }} {{ if and $field.Kind.IsRelation (not $field.IsList) }}*{{ end }}{{ $field.Type.Value }}{{ if $field.Kind.IsRelation }}Model{{ end -}} + {{- if $field.IsList }}value []{{ else }}value{{ end }} {{ if and $field.Kind.IsRelation (not $field.IsList) }}*{{ end }}{{ $field.Type.GoCase }}{{ if $field.Kind.IsRelation }}Model{{ end -}} {{- if or (not $field.Kind.IsRelation) (and (not $field.IsList) (not $field.IsRequired)) -}} , ok bool {{- end -}} diff --git a/generator/templates/query.gotpl b/generator/templates/query.gotpl index 0ad1e5bb..47b1d279 100644 --- a/generator/templates/query.gotpl +++ b/generator/templates/query.gotpl @@ -265,7 +265,7 @@ } // Set the optional value of {{ $field.Name.GoCase }} dynamically - func (r {{ $struct }}) SetIfPresent(value *{{ if $field.IsList }}[]{{ else }}{{ end }}{{ $field.Type.Value }}) {{ $setReturnStruct }} { + func (r {{ $struct }}) SetIfPresent(value *{{ if $field.IsList }}[]{{ else }}{{ end }}{{ $field.Type.GoCase }}) {{ $setReturnStruct }} { if value == nil { return {{ $setReturnStruct }}{} } @@ -276,7 +276,7 @@ {{ if and (not $field.IsRequired) (not $field.IsList) (not $field.Prisma) }} // Set the optional value of {{ $field.Name.GoCase }} dynamically - func (r {{ $struct }}) SetOptional(value *{{ $field.Type.Value }}) {{ $setReturnStruct }} { + func (r {{ $struct }}) SetOptional(value *{{ $field.Type.GoCase }}) {{ $setReturnStruct }} { if value == nil { {{/* nil value of type */}} var v *{{ $field.Type.Value }} @@ -367,7 +367,7 @@ } {{ if and (not $field.IsRequired) (not $field.Prisma) }} - func (r {{ $struct }}) EqualsOptional(value *{{ $field.Type.Value }}) {{ $returnStruct }} { + func (r {{ $struct }}) EqualsOptional(value *{{ $field.Type.GoCase }}) {{ $returnStruct }} { return {{ $returnStruct }}{ data: builder.Field{ Name: "{{ $field.Name }}", diff --git a/generator/types/types_test.go b/generator/types/types_test.go index 1bfb01ba..70117bec 100644 --- a/generator/types/types_test.go +++ b/generator/types/types_test.go @@ -18,6 +18,9 @@ func TestString_GoCase(t *testing.T) { }, { have: "anotherIdStuffSomethingId", want: "AnotherIDStuffSomethingID", + }, { + have: "APISession", + want: "APISession", }} for _, tt := range tests { t.Run(fmt.Sprintf("%s -> %s", tt.have, tt.want), func(t *testing.T) { diff --git a/helpers/gocase/gocase_test.go b/helpers/gocase/gocase_test.go index 1e90e092..cef8b594 100644 --- a/helpers/gocase/gocase_test.go +++ b/helpers/gocase/gocase_test.go @@ -40,6 +40,7 @@ func TestConverter_ToLower(t *testing.T) { {conv: dc, have: "anotherIdStuffSomethingId", want: "anotherIDStuffSomethingID"}, {conv: dc, have: "anotherIdStuffSomethingId", want: "anotherIDStuffSomethingID"}, {conv: dc, have: "another_id_stuff_something_id", want: "anotherIDStuffSomethingID"}, + // {conv: dc, have: "APISession", want: "apiSession"}, // {conv: cc, have: "JsonFile", want: "jsonFile"}, // {conv: cc, have: "CsvFile", want: "csvFile"}, @@ -90,6 +91,7 @@ func TestConverter_ToUpper(t *testing.T) { {conv: dc, have: "anotherIdStuffSomethingId", want: "AnotherIDStuffSomethingID"}, {conv: dc, have: "anotherIdStuffSomethingId", want: "AnotherIDStuffSomethingID"}, {conv: dc, have: "another_id_stuff_something_id", want: "AnotherIDStuffSomethingID"}, + {conv: dc, have: "APISession", want: "APISession"}, {conv: cc, have: "JsonFile", want: "JSONFile"}, {conv: cc, have: "CsvFile", want: "CSVFile"}, diff --git a/test/features/composite/schema.prisma b/test/features/composite/schema.prisma index 08077873..5885ec4a 100644 --- a/test/features/composite/schema.prisma +++ b/test/features/composite/schema.prisma @@ -18,8 +18,22 @@ model User { somethingId String? anotherIdStuff String? + apiStuff APIStuff? @relation(fields: [somethingId], references: [id]) + @@id([firstName, lastName]) @@unique([firstName, middleName, lastName]) @@unique([somethingId, anotherIdStuff]) @@unique([anotherIdStuff, somethingId], name: "anotherIDStuffSomethingID") } + +// TODO put this somewhere else +model APIStuff { + id String @id @default(uuid()) + + somethingId String? + anotherID String? + idSome String? + IPSome String? + + users User[] +}