Skip to content

Commit

Permalink
Apply generic for query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
minhduc140583 committed Jun 22, 2024
1 parent 63ab82c commit 6ec3c09
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions firestore/query/query_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@ import (
"strings"
)

type Builder struct {
type Builder[T any, F any] struct {
ModelType reflect.Type
}

func NewBuilder(resultModelType reflect.Type) *Builder {
return &Builder{ModelType: resultModelType}
func UseQuery[T any, F any]() func(F) ([]f.Query, []string) {
bu := NewBuilder[T, F]()
return bu.BuildQuery
}
func (b *Builder) BuildQuery(sm interface{}) ([]f.Query, []string) {

func NewBuilder[T any, F any]() *Builder[T, F] {
var t T
resultModelType := reflect.TypeOf(t)
if resultModelType.Kind() == reflect.Ptr {
resultModelType = resultModelType.Elem()
}
return &Builder[T, F]{ModelType: resultModelType}
}
func (b *Builder[T, F]) BuildQuery(sm F) ([]f.Query, []string) {
return BuildQueryByType(sm, b.ModelType)
}

Expand Down

0 comments on commit 6ec3c09

Please sign in to comment.