Passed
Pull Request — master (#2516)
by Tolga
04:08
created

internal/storage/memory/bundle_reader.go   A

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 24
dl 0
loc 39
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A memory.*BundleReader.Read 0 15 3
A memory.NewBundleReader 0 3 1
1
package memory
2
3
import (
4
	"context"
5
	"errors"
6
7
	"github.com/Permify/permify/internal/storage"
8
	"github.com/Permify/permify/internal/storage/memory/constants"
9
10
	db "github.com/Permify/permify/pkg/database/memory"
11
	base "github.com/Permify/permify/pkg/pb/base/v1"
12
)
13
14
// BundleReader -
15
type BundleReader struct {
16
	database *db.Memory
17
}
18
19
func NewBundleReader(database *db.Memory) *BundleReader {
20
	return &BundleReader{
21
		database: database,
22
	}
23
}
24
25
func (b *BundleReader) Read(ctx context.Context, tenantID, name string) (bundle *base.DataBundle, err error) {
26
	txn := b.database.DB.Txn(false)
27
	defer txn.Abort()
28
	var raw interface{}
29
	raw, err = txn.First(constants.BundlesTable, "id", tenantID, name)
30
	if err != nil {
31
		return bundle, errors.New(base.ErrorCode_ERROR_CODE_EXECUTION.String())
32
	}
33
34
	bun, ok := raw.(storage.Bundle)
35
	if ok {
36
		return bun.DataBundle, err
37
	}
38
39
	return nil, errors.New(base.ErrorCode_ERROR_CODE_BUNDLE_NOT_FOUND.String())
40
}
41