Skip to content

Commit

Permalink
Merge pull request #22 from rabee-inc/feature/update-typed-batch-getter
Browse files Browse the repository at this point in the history
TypedBatchGetterをConvertibleBatchGetterと同じ仕様にする
  • Loading branch information
aikizoku authored May 29, 2023
2 parents bc64caa + d0b0846 commit d828689
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 60 deletions.
7 changes: 6 additions & 1 deletion cloudfirestore/convertible_batch_getter.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
14 changes: 1 addition & 13 deletions cloudfirestore/typed_batch_getter.go
Original file line number Diff line number Diff line change
@@ -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]
}
71 changes: 25 additions & 46 deletions cloudfirestore/typed_batch_getter_impl.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
}

0 comments on commit d828689

Please sign in to comment.