Skip to content

Commit

Permalink
fix(ast): introduce list read filters (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen authored Oct 26, 2021
1 parent 21025cc commit d0a8a0b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion generator/ast/transform/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 15 additions & 6 deletions generator/ast/transform/readfilters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion generator/templates/query.gotpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 "" }}
Expand Down

0 comments on commit d0a8a0b

Please sign in to comment.