Skip to content

Commit

Permalink
fix(generator): consider model type casing (#1144)
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen committed Jan 2, 2024
1 parent ae36e53 commit 673f53d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions generator/templates/_header.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions generator/templates/models.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
}
Expand All @@ -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 -}}
Expand Down
6 changes: 3 additions & 3 deletions generator/templates/query.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}{}
}
Expand All @@ -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 }}
Expand Down Expand Up @@ -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 }}",
Expand Down
3 changes: 3 additions & 0 deletions generator/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions helpers/gocase/gocase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -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"},
Expand Down
14 changes: 14 additions & 0 deletions test/features/composite/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
}

0 comments on commit 673f53d

Please sign in to comment.