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 7646e53..b951c70 100644 --- a/cloudfirestore/typed_batch_getter.go +++ b/cloudfirestore/typed_batch_getter.go @@ -1,17 +1,5 @@ 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) -}