From 7a2420905c4d4b883c5b1899fddeaf7d3ee31582 Mon Sep 17 00:00:00 2001 From: Paul Nicolas Date: Wed, 25 Sep 2024 15:22:04 +0200 Subject: [PATCH] fix(bunpaginate): fix missing unmarshal method for options --- bun/bunpaginate/pagination_query_options.go | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/bun/bunpaginate/pagination_query_options.go b/bun/bunpaginate/pagination_query_options.go index 2d6bc528..5461e647 100644 --- a/bun/bunpaginate/pagination_query_options.go +++ b/bun/bunpaginate/pagination_query_options.go @@ -1,6 +1,8 @@ package bunpaginate import ( + "encoding/json" + "github.com/formancehq/go-libs/query" ) @@ -10,6 +12,29 @@ type PaginatedQueryOptions[T any] struct { Options T `json:"options"` } +func (pqo *PaginatedQueryOptions[T]) UnmarshalJSON(data []byte) error { + type base struct { + PageSize uint64 `json:"pageSize"` + Options T `json:"options"` + } + var b base + if err := json.Unmarshal(data, &b); err != nil { + return err + } + pqo.PageSize = b.PageSize + pqo.Options = b.Options + var dataMap map[string]json.RawMessage + if err := json.Unmarshal(data, &dataMap); err != nil { + return err + } + qb, err := query.ParseJSON(string(dataMap["qb"])) + if err != nil { + return err + } + pqo.QueryBuilder = qb + return nil +} + func (opts PaginatedQueryOptions[T]) WithQueryBuilder(qb query.Builder) PaginatedQueryOptions[T] { opts.QueryBuilder = qb