From 64695309bcb0a2c0947bccbd5c322a2c4b8c7f6e Mon Sep 17 00:00:00 2001 From: simiraaaa Date: Mon, 29 May 2023 13:13:34 +0900 Subject: [PATCH 1/2] =?UTF-8?q?TypedBatchGetter=E3=82=92ConvertibleBatchGe?= =?UTF-8?q?tter=E3=81=A8=E5=90=8C=E3=81=98=E4=BB=95=E6=A7=98=E3=81=AB?= =?UTF-8?q?=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cloudfirestore/typed_batch_getter.go | 8 +-- cloudfirestore/typed_batch_getter_impl.go | 71 ++++++++--------------- 2 files changed, 26 insertions(+), 53 deletions(-) diff --git a/cloudfirestore/typed_batch_getter.go b/cloudfirestore/typed_batch_getter.go index 7646e53..a2d0b22 100644 --- a/cloudfirestore/typed_batch_getter.go +++ b/cloudfirestore/typed_batch_getter.go @@ -1,17 +1,11 @@ package cloudfirestore import ( - "context" - "cloud.google.com/go/firestore" ) type FuncGetDoc func(ids ...string) *firestore.DocumentRef type TypedBatchGetter[T any] interface { - Add(ids ...string) - Delete(ids ...string) - GetMap() map[string]*T - Get(ids ...string) *T - Commit(ctx context.Context) error + ConvertibleBatchGetter[T, T] } diff --git a/cloudfirestore/typed_batch_getter_impl.go b/cloudfirestore/typed_batch_getter_impl.go index 0713d39..fe2ab4f 100644 --- a/cloudfirestore/typed_batch_getter_impl.go +++ b/cloudfirestore/typed_batch_getter_impl.go @@ -1,19 +1,32 @@ package cloudfirestore -import "context" +func NewTypedBatchGetter[T any]( + bg BatchGetter, + getDoc FuncGetDoc, + getID FuncGetID[T], + convert FuncConvert[T, T], +) TypedBatchGetter[T] { + if convert == nil { + convert = func(t *T) *T { return t } + } + cbg := &convertibleBatchGetter[T, T]{ + bg: bg, + dstMap: map[string]*T{}, + items: []*convertibleBatchGetterItem[*T]{}, + getDoc: getDoc, + getID: getID, + convert: convert, + } -type typedBatchGetter[T any] struct { - bg BatchGetter - docMap map[string]string - getDoc FuncGetDoc -} + bg.OnCommit(func() { + cbg.convertAll() + }) -func NewTypedBatchGetter[T any](bg BatchGetter, getDoc FuncGetDoc) TypedBatchGetter[T] { - return &typedBatchGetter[T]{ - bg: bg, - docMap: map[string]string{}, - getDoc: getDoc, - } + bg.OnEnd(func() { + cbg.convertAll() + }) + + return cbg } type FuncGetModel[E, M any] func(e *E) (id string, m *M) @@ -32,37 +45,3 @@ func GetModelMapByBatchGetter[E, M any]( } return ms } - -func (tbg *typedBatchGetter[T]) Add(ids ...string) { - docRef := tbg.getDoc(ids...) - tbg.docMap[docRef.Path] = docRef.ID - data := new(T) - tbg.bg.Add(docRef, data) -} - -func (tbg *typedBatchGetter[T]) Delete(ids ...string) { - docRef := tbg.getDoc(ids...) - delete(tbg.docMap, docRef.Path) - tbg.bg.Delete(docRef) -} - -func (tbg *typedBatchGetter[T]) GetMap() map[string]*T { - m := map[string]*T{} - for k, id := range tbg.docMap { - d := tbg.bg.Get(k) - if d != nil { - m[id] = d.(*T) - } - } - return m -} - -func (tbg *typedBatchGetter[T]) Get(ids ...string) *T { - docRef := tbg.getDoc(ids...) - data := tbg.bg.Get(docRef.Path) - return data.(*T) -} - -func (tbg *typedBatchGetter[T]) Commit(ctx context.Context) error { - return tbg.bg.Commit(ctx) -} From d0b08462a22cc9b9f717423dc31f0dde8863083c Mon Sep 17 00:00:00 2001 From: simiraaaa Date: Mon, 29 May 2023 13:15:06 +0900 Subject: [PATCH 2/2] fix --- cloudfirestore/convertible_batch_getter.go | 7 ++++++- cloudfirestore/typed_batch_getter.go | 6 ------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/cloudfirestore/convertible_batch_getter.go b/cloudfirestore/convertible_batch_getter.go index 2edeabc..0d62351 100644 --- a/cloudfirestore/convertible_batch_getter.go +++ b/cloudfirestore/convertible_batch_getter.go @@ -1,7 +1,12 @@ package cloudfirestore -import "context" +import ( + "context" + "cloud.google.com/go/firestore" +) + +type FuncGetDoc func(ids ...string) *firestore.DocumentRef type FuncGetID[D any] func(*D) string type FuncConvert[S, D any] func(*S) *D type ConvertibleBatchGetterItem[D any] interface { diff --git a/cloudfirestore/typed_batch_getter.go b/cloudfirestore/typed_batch_getter.go index a2d0b22..b951c70 100644 --- a/cloudfirestore/typed_batch_getter.go +++ b/cloudfirestore/typed_batch_getter.go @@ -1,11 +1,5 @@ package cloudfirestore -import ( - "cloud.google.com/go/firestore" -) - -type FuncGetDoc func(ids ...string) *firestore.DocumentRef - type TypedBatchGetter[T any] interface { ConvertibleBatchGetter[T, T] }