mirror of
https://git.sr.ht/~ashkeel/strimertul
synced 2024-09-20 02:00:49 +00:00
26 lines
463 B
Go
26 lines
463 B
Go
|
package utils
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
|
||
|
"github.com/dgraph-io/badger/v3"
|
||
|
)
|
||
|
|
||
|
func GetJSONTx(t *badger.Txn, key string, dst interface{}) error {
|
||
|
item, err := t.Get([]byte(key))
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
byt, err := item.ValueCopy(nil)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return json.Unmarshal(byt, dst)
|
||
|
}
|
||
|
|
||
|
func DBGetJSON(db *badger.DB, key string, dst interface{}) error {
|
||
|
return db.View(func(t *badger.Txn) error {
|
||
|
return GetJSONTx(t, key, dst)
|
||
|
})
|
||
|
}
|