Skip to content

Commit

Permalink
Merge pull request #10 from rabee-inc/feature/go119
Browse files Browse the repository at this point in the history
Go119に対応
  • Loading branch information
aikizoku authored Feb 24, 2023
2 parents a97690b + 244e6b2 commit 0e7303f
Show file tree
Hide file tree
Showing 56 changed files with 579 additions and 1,379 deletions.
39 changes: 17 additions & 22 deletions bigquery/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,33 @@ import (
"time"

"cloud.google.com/go/bigquery"
"github.com/rabee-inc/go-pkg/log"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"

"github.com/rabee-inc/go-pkg/log"
)

// Client ... BigQueryのクライアント
type Client struct {
client *bigquery.Client
}

// List ... クエリを実行し、データを取得する
func (c *Client) List(ctx context.Context, query string, limit int, cursor string, dsts interface{}) (string, error) {
func NewClient(projectID string) *Client {
ctx := context.Background()
gOpt := option.WithGRPCDialOption(grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: 1 * time.Second,
Timeout: 5 * time.Second,
PermitWithoutStream: true,
}))
client, err := bigquery.NewClient(ctx, projectID, gOpt)
if err != nil {
panic(err)
}
return &Client{client}
}

// クエリを実行し、データを取得する
func (c *Client) List(ctx context.Context, query string, limit int, cursor string, dsts any) (string, error) {
q := c.client.Query(query)
it, err := q.Read(ctx)
if err != nil {
Expand Down Expand Up @@ -58,20 +70,3 @@ func (c *Client) List(ctx context.Context, query string, limit int, cursor strin
}
return token, nil
}

// NewClient ... クライアントを作成する
func NewClient(projectID string) *Client {
ctx := context.Background()
gOpt := option.WithGRPCDialOption(grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: 1 * time.Second,
Timeout: 5 * time.Second,
PermitWithoutStream: true,
}))
client, err := bigquery.NewClient(ctx, projectID, gOpt)
if err != nil {
panic(err)
}
return &Client{
client: client,
}
}
2 changes: 1 addition & 1 deletion bytesutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bytesutil

import "unsafe"

// ToStr ... バイト列を文字列に変換する
// バイト列を文字列に変換する
func ToStr(bytes []byte) string {
return *(*string)(unsafe.Pointer(&bytes))
}
1 change: 0 additions & 1 deletion cloudfirestore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"google.golang.org/grpc/keepalive"
)

// NewClient ... Firestoreのクライアントを取得する
func NewClient(projectID string) *firestore.Client {
ctx := context.Background()
gOpt := option.WithGRPCDialOption(grpc.WithKeepaliveParams(keepalive.ClientParameters{
Expand Down
12 changes: 6 additions & 6 deletions cloudfirestore/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type contextKey string

const (
ctxTxKey contextKey = "firestore:tx"
ctxBtKey contextKey = "firestore:bt"
ctxBwKey contextKey = "firestore:bw"
)

func getContextTransaction(ctx context.Context) *firestore.Transaction {
Expand All @@ -24,13 +24,13 @@ func setContextTransaction(ctx context.Context, tx *firestore.Transaction) conte
return context.WithValue(ctx, ctxTxKey, tx)
}

func getContextWriteBatch(ctx context.Context) *firestore.WriteBatch {
if bt, ok := ctx.Value(ctxBtKey).(*firestore.WriteBatch); ok {
return bt
func getContextBulkWriter(ctx context.Context) *firestore.BulkWriter {
if bw, ok := ctx.Value(ctxBwKey).(*firestore.BulkWriter); ok {
return bw
}
return nil
}

func setContextWriteBatch(ctx context.Context, bt *firestore.WriteBatch) context.Context {
return context.WithValue(ctx, ctxBtKey, bt)
func setContextBulkWriter(ctx context.Context, bt *firestore.BulkWriter) context.Context {
return context.WithValue(ctx, ctxBwKey, bt)
}
97 changes: 55 additions & 42 deletions cloudfirestore/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ import (
"regexp"

"cloud.google.com/go/firestore"
"google.golang.org/api/iterator"

"github.com/rabee-inc/go-pkg/log"
"github.com/rabee-inc/go-pkg/sliceutil"
"github.com/rabee-inc/go-pkg/stringutil"
"google.golang.org/api/iterator"
)

var reInvalidDocID = regexp.MustCompile(`\.|/`)

// ValidateDocumentID ... 正常な DocumentID かチェック
// 正常な DocumentID かチェック
func ValidateDocumentID(str string) bool {
return !reInvalidDocID.MatchString(str)
}

// ValidateCollectionRef ... 正常な Path かチェック
// 正常な Path かチェック
func ValidateCollectionRef(colRef *firestore.CollectionRef) bool {
var docRef *firestore.DocumentRef
for colRef != nil || docRef != nil {
Expand All @@ -43,15 +42,15 @@ func ValidateCollectionRef(colRef *firestore.CollectionRef) bool {
return true
}

// ValidateDocumentRef ... 正常な Path かチェック
// 正常な Path かチェック
func ValidateDocumentRef(docRef *firestore.DocumentRef) bool {
if !ValidateDocumentID(docRef.ID) {
return false
}
return ValidateCollectionRef(docRef.Parent)
}

// GenerateDocumentRef ... ドキュメント参照を作成する
// ドキュメント参照を作成する
func GenerateDocumentRef(cFirestore *firestore.Client, docRefs []*DocRef) *firestore.DocumentRef {
var dst *firestore.DocumentRef
for i, docRef := range docRefs {
Expand All @@ -71,26 +70,24 @@ func RunTransaction(ctx context.Context, cFirestore *firestore.Client, fn func(c
}, opts...)
}

func RunWriteBatch(ctx context.Context, cFirestore *firestore.Client) context.Context {
bt := cFirestore.Batch()
return setContextWriteBatch(ctx, bt)
func RunBulkWriter(ctx context.Context, cFirestore *firestore.Client) context.Context {
bw := cFirestore.BulkWriter(ctx)
return setContextBulkWriter(ctx, bw)
}

func CommitWriteBatch(ctx context.Context) (context.Context, error) {
if bt := getContextWriteBatch(ctx); bt != nil {
ctx = setContextWriteBatch(ctx, nil)
if _, err := bt.Commit(ctx); err != nil {
return ctx, err
}
func CommitBulkWriter(ctx context.Context) (context.Context, error) {
if bw := getContextBulkWriter(ctx); bw != nil {
ctx = setContextBulkWriter(ctx, nil)
bw.Flush()
} else {
err := log.Errore(ctx, "no running write batch")
err := log.Errore(ctx, "no running bulk writer")
return ctx, err
}
return ctx, nil
}

// Get ... 単体取得する(tx対応)
func Get(ctx context.Context, docRef *firestore.DocumentRef, dst interface{}) (bool, error) {
// 単体取得する(tx対応)
func Get(ctx context.Context, docRef *firestore.DocumentRef, dst any) (bool, error) {
if docRef == nil || docRef.ID == "" || !ValidateDocumentID(docRef.ID) {
return false, nil
}
Expand Down Expand Up @@ -118,8 +115,8 @@ func Get(ctx context.Context, docRef *firestore.DocumentRef, dst interface{}) (b
return true, nil
}

// GetMulti ... 複数取得する(tx対応)
func GetMulti(ctx context.Context, cFirestore *firestore.Client, docRefs []*firestore.DocumentRef, dsts interface{}) error {
// 複数取得する(tx対応)
func GetMulti(ctx context.Context, cFirestore *firestore.Client, docRefs []*firestore.DocumentRef, dsts any) error {
docRefs = sliceutil.StreamOf(docRefs).
Filter(func(docRef *firestore.DocumentRef) bool {
return docRef != nil && docRef.ID != "" && ValidateDocumentID(docRef.ID)
Expand Down Expand Up @@ -159,8 +156,8 @@ func GetMulti(ctx context.Context, cFirestore *firestore.Client, docRefs []*fire
return nil
}

// GetByQuery ... クエリで単体取得する(tx対応)
func GetByQuery(ctx context.Context, query firestore.Query, dst interface{}) (bool, error) {
// クエリで単体取得する(tx対応)
func GetByQuery(ctx context.Context, query firestore.Query, dst any) (bool, error) {
query = query.Limit(1)
var it *firestore.DocumentIterator
if tx := getContextTransaction(ctx); tx != nil {
Expand All @@ -187,8 +184,8 @@ func GetByQuery(ctx context.Context, query firestore.Query, dst interface{}) (bo
return true, nil
}

// ListByQuery ... クエリで複数取得する(tx対応)
func ListByQuery(ctx context.Context, query firestore.Query, dsts interface{}) error {
// クエリで複数取得する(tx対応)
func ListByQuery(ctx context.Context, query firestore.Query, dsts any) error {
var it *firestore.DocumentIterator
if tx := getContextTransaction(ctx); tx != nil {
it = tx.Documents(query)
Expand Down Expand Up @@ -221,8 +218,8 @@ func ListByQuery(ctx context.Context, query firestore.Query, dsts interface{}) e
return nil
}

// ListByQueryCursor ... クエリで複数取得する(ページング)
func ListByQueryCursor(ctx context.Context, query firestore.Query, limit int, cursor *firestore.DocumentSnapshot, dsts interface{}) (*firestore.DocumentSnapshot, error) {
// クエリで複数取得する(ページング)
func ListByQueryCursor(ctx context.Context, query firestore.Query, limit int, cursor *firestore.DocumentSnapshot, dsts any) (*firestore.DocumentSnapshot, error) {
if cursor != nil {
query = query.StartAfter(cursor)
}
Expand Down Expand Up @@ -264,8 +261,8 @@ func ListByQueryCursor(ctx context.Context, query firestore.Query, limit int, cu
return nil, nil
}

// Create ... 作成する(tx, bt対応)
func Create(ctx context.Context, colRef *firestore.CollectionRef, src interface{}) error {
// 作成する(tx, bw対応)
func Create(ctx context.Context, colRef *firestore.CollectionRef, src any) error {
// 不正なIDがないかチェック
if !ValidateCollectionRef(colRef) {
return errors.New("Invalid Collection Path: " + colRef.Path)
Expand All @@ -280,10 +277,14 @@ func Create(ctx context.Context, colRef *firestore.CollectionRef, src interface{
log.Warning(ctx, err)
return err
}
} else if bt := getContextWriteBatch(ctx); bt != nil {
} else if bw := getContextBulkWriter(ctx); bw != nil {
id := stringutil.UniqueID()
docRef = colRef.Doc(id)
bt.Create(docRef, src)
_, err := bw.Create(docRef, src)
if err != nil {
log.Warning(ctx, err)
return err
}
} else {
var err error
docRef, _, err = colRef.Add(ctx, src)
Expand All @@ -296,8 +297,8 @@ func Create(ctx context.Context, colRef *firestore.CollectionRef, src interface{
return nil
}

// Update ... 更新する(tx, bt対応)
func Update(ctx context.Context, docRef *firestore.DocumentRef, kv map[string]interface{}) error {
// 更新する(tx, bw対応)
func Update(ctx context.Context, docRef *firestore.DocumentRef, kv map[string]any) error {
srcs := []firestore.Update{}
for k, v := range kv {
src := firestore.Update{Path: k, Value: v}
Expand All @@ -309,8 +310,12 @@ func Update(ctx context.Context, docRef *firestore.DocumentRef, kv map[string]in
log.Warning(ctx, err)
return err
}
} else if bt := getContextWriteBatch(ctx); bt != nil {
_ = bt.Update(docRef, srcs)
} else if bw := getContextBulkWriter(ctx); bw != nil {
_, err := bw.Update(docRef, srcs)
if err != nil {
log.Warning(ctx, err)
return err
}
} else {
_, err := docRef.Update(ctx, srcs)
if err != nil {
Expand All @@ -321,8 +326,8 @@ func Update(ctx context.Context, docRef *firestore.DocumentRef, kv map[string]in
return nil
}

// Set ... 上書きする(tx, bt対応)
func Set(ctx context.Context, docRef *firestore.DocumentRef, src interface{}) error {
// 上書きする(tx, bw対応)
func Set(ctx context.Context, docRef *firestore.DocumentRef, src any) error {
// 不正なIDがないかチェック
if !ValidateDocumentRef(docRef) {
return errors.New("Invalid Document Path: " + docRef.Path)
Expand All @@ -334,8 +339,12 @@ func Set(ctx context.Context, docRef *firestore.DocumentRef, src interface{}) er
log.Warning(ctx, err)
return err
}
} else if bt := getContextWriteBatch(ctx); bt != nil {
_ = bt.Set(docRef, src)
} else if bw := getContextBulkWriter(ctx); bw != nil {
_, err := bw.Set(docRef, src)
if err != nil {
log.Warning(ctx, err)
return err
}
} else {
_, err := docRef.Set(ctx, src)
if err != nil {
Expand All @@ -347,16 +356,20 @@ func Set(ctx context.Context, docRef *firestore.DocumentRef, src interface{}) er
return nil
}

// Delete ... 削除する(tx, bt対応)
// 削除する(tx, bw対応)
func Delete(ctx context.Context, docRef *firestore.DocumentRef) error {
if tx := getContextTransaction(ctx); tx != nil {
err := tx.Delete(docRef)
if err != nil {
log.Warning(ctx, err)
return err
}
} else if bt := getContextWriteBatch(ctx); bt != nil {
_ = bt.Delete(docRef)
} else if bw := getContextBulkWriter(ctx); bw != nil {
_, err := bw.Delete(docRef)
if err != nil {
log.Warning(ctx, err)
return err
}
} else {
_, err := docRef.Delete(ctx)
if err != nil {
Expand All @@ -367,7 +380,7 @@ func Delete(ctx context.Context, docRef *firestore.DocumentRef) error {
return nil
}

// AddStartWith ... 前方一致クエリを追加する
// 前方一致クエリを追加する
func AddStartWith(q firestore.Query, key string, word string) firestore.Query {
return q.OrderBy(key, firestore.Asc).
StartAt(word).
Expand Down
6 changes: 3 additions & 3 deletions cloudfirestore/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
"cloud.google.com/go/firestore"
)

// DocRef ... 個別のドキュメント参照
// 個別のドキュメント参照
type DocRef struct {
CollectionName string `json:"collection_name"`
DocID string `json:"doc_id"`
}

// SummaryDocRef ... 最小のドキュメント参照(フロントに返せる)
// 最小のドキュメント参照(フロントに返せる)
type SummaryDocRef struct {
ID string `json:"id"`
Path string `json:"path"`
}

// GenerateSummaryDocRef ... 最小のドキュメント参照を取得する
// 最小のドキュメント参照を取得する
func GenerateSummaryDocRef(docRef *firestore.DocumentRef, rootCollectionPath string) *SummaryDocRef {
if docRef == nil {
return nil
Expand Down
4 changes: 2 additions & 2 deletions cloudfirestore/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"cloud.google.com/go/firestore"
)

func setDocByDst(dst interface{}, ref *firestore.DocumentRef) {
func setDocByDst(dst any, ref *firestore.DocumentRef) {
rv := reflect.Indirect(reflect.ValueOf(dst))
rt := rv.Type()
if rt.Kind() == reflect.Struct {
Expand Down Expand Up @@ -42,7 +42,7 @@ func setDocByDsts(rv reflect.Value, rt reflect.Type, ref *firestore.DocumentRef)
}
}

func setEmptyBySlice(dst interface{}) {
func setEmptyBySlice(dst any) {
rv := reflect.Indirect(reflect.ValueOf(dst))
rt := rv.Type()
if rt.Kind() == reflect.Struct {
Expand Down
Loading

0 comments on commit 0e7303f

Please sign in to comment.