From d0a8a0b7dc985df3cfc42f841b92964469394b49 Mon Sep 17 00:00:00 2001 From: Luca Steeb Date: Tue, 26 Oct 2021 16:46:18 +0700 Subject: [PATCH] fix(ast): introduce list read filters (#651) --- generator/ast/transform/models.go | 2 +- generator/ast/transform/readfilters.go | 21 +++++++++++++++------ generator/templates/query.gotpl | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/generator/ast/transform/models.go b/generator/ast/transform/models.go index 95716a1b..4c6a30d1 100644 --- a/generator/ast/transform/models.go +++ b/generator/ast/transform/models.go @@ -11,7 +11,7 @@ type Model struct { Indexes []Index `json:"indexes"` // TODO remove this and apply all required data directly to model - OldModel dmmf.Model + OldModel dmmf.Model `json:"-"` } type Field struct { diff --git a/generator/ast/transform/readfilters.go b/generator/ast/transform/readfilters.go index dc299985..db1577c9 100644 --- a/generator/ast/transform/readfilters.go +++ b/generator/ast/transform/readfilters.go @@ -7,6 +7,8 @@ import ( "github.com/prisma/prisma-client-go/generator/types" ) +const list = "List" + func (r *AST) readFilters() []Filter { var filters []Filter for _, scalar := range r.Scalars { @@ -21,22 +23,26 @@ func (r *AST) readFilters() []Filter { }, } - var fields []Method for _, c := range combinations { p := r.pick(c...) if p == nil { continue } + var fields []Method for _, field := range p.Fields { if method := convertField(field); method != nil { fields = append(fields, *method) } } + s := scalar + if strings.Contains(p.Name.String(), "ListFilter") { + s += list + } + filters = append(filters, Filter{ + Name: s, + Methods: fields, + }) } - filters = append(filters, Filter{ - Name: scalar, - Methods: fields, - }) } for _, enum := range r.Enums { p := r.pick( @@ -63,9 +69,12 @@ func (r *AST) readFilters() []Filter { } // ReadFilter returns a filter for a read operation by scalar -func (r *AST) ReadFilter(scalar string) *Filter { +func (r *AST) ReadFilter(scalar string, isList bool) *Filter { scalar = strings.Replace(scalar, "NullableFilter", "", 1) scalar = strings.Replace(scalar, "ReadFilter", "", 1) + if isList { + scalar += list + } for _, filter := range r.ReadFilters { if filter.Name == scalar { return &filter diff --git a/generator/templates/query.gotpl b/generator/templates/query.gotpl index bae01469..c7a1a146 100644 --- a/generator/templates/query.gotpl +++ b/generator/templates/query.gotpl @@ -441,7 +441,7 @@ {{ end }} {{/* Provide field and type-specific methods. */}} - {{ $readType := $.AST.ReadFilter $field.Type.String }} + {{ $readType := $.AST.ReadFilter $field.Type.String $field.IsList }} {{ if $readType }} {{ range $method := $readType.Methods }} {{ if ne $method.Deprecated "" }}