mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-03 20:47:35 +03:00
reduced overhead of mapping models to ledis, I don't think we will need nested structs
This commit is contained in:
parent
ae5206612c
commit
08f035d99b
3 changed files with 37 additions and 12 deletions
|
@ -6,7 +6,7 @@ type BaseRepository struct {
|
|||
|
||||
|
||||
func (r *BaseRepository) saveOrUpdate(id string, rec interface{}) error {
|
||||
return hmset(r.key + "_id_" + id, rec)
|
||||
return saveStruct(r.key + "_id_" + id, rec)
|
||||
}
|
||||
|
||||
func (r *BaseRepository) Dump() {
|
||||
|
|
|
@ -28,8 +28,8 @@ func db() *ledis.DB {
|
|||
return _dbInstance
|
||||
}
|
||||
|
||||
func hmset(key string, data interface{}) error {
|
||||
h, err := utils.Flatten(data)
|
||||
func saveStruct(key string, data interface{}) error {
|
||||
h, err := utils.ToMap(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -43,6 +43,18 @@ func hmset(key string, data interface{}) error {
|
|||
return db().HMset([]byte(key), fvList...)
|
||||
}
|
||||
|
||||
func readStruct(key string) (interface{}, error) {
|
||||
fvs, _ := db().HGetAll([]byte(key))
|
||||
var m = make(map[string]interface{}, len(fvs))
|
||||
for _, fv := range fvs {
|
||||
var v interface{}
|
||||
json.Unmarshal(fv.Value, &v)
|
||||
m[string(fv.Field)] = v
|
||||
}
|
||||
|
||||
return utils.ToStruct(m)
|
||||
}
|
||||
|
||||
func hset(key, field, value string) error {
|
||||
_, err := db().HSet([]byte(key), []byte(field), []byte(value))
|
||||
return err
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
|
||||
const delimiter = "."
|
||||
|
||||
func explodeList(l []interface{}, parent string, delimiter string) (map[string]interface{}, error) {
|
||||
func flattenList(l []interface{}, parent string, delimiter string) (map[string]interface{}, error) {
|
||||
var err error
|
||||
var key string
|
||||
j := make(map[string]interface{})
|
||||
|
@ -32,7 +32,7 @@ func explodeList(l []interface{}, parent string, delimiter string) (map[string]i
|
|||
j[key] = v
|
||||
case []interface{}:
|
||||
out := make(map[string]interface{})
|
||||
out, err = explodeList(v, key, delimiter)
|
||||
out, err = flattenList(v, key, delimiter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ func explodeList(l []interface{}, parent string, delimiter string) (map[string]i
|
|||
}
|
||||
case map[string]interface{}:
|
||||
out := make(map[string]interface{})
|
||||
out, err = explodeMap(v, key, delimiter)
|
||||
out, err = flattenMap(v, key, delimiter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ func explodeList(l []interface{}, parent string, delimiter string) (map[string]i
|
|||
return j, nil
|
||||
}
|
||||
|
||||
func explodeMap(m map[string]interface{}, parent string, delimiter string) (map[string]interface{}, error) {
|
||||
func flattenMap(m map[string]interface{}, parent string, delimiter string) (map[string]interface{}, error) {
|
||||
var err error
|
||||
j := make(map[string]interface{})
|
||||
for k, i := range m {
|
||||
|
@ -75,7 +75,7 @@ func explodeMap(m map[string]interface{}, parent string, delimiter string) (map[
|
|||
j[k] = v
|
||||
case []interface{}:
|
||||
out := make(map[string]interface{})
|
||||
out, err = explodeList(v, k, delimiter)
|
||||
out, err = flattenList(v, k, delimiter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -84,7 +84,7 @@ func explodeMap(m map[string]interface{}, parent string, delimiter string) (map[
|
|||
}
|
||||
case map[string]interface{}:
|
||||
out := make(map[string]interface{})
|
||||
out, err = explodeMap(v, k, delimiter)
|
||||
out, err = flattenMap(v, k, delimiter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -101,14 +101,14 @@ func explodeMap(m map[string]interface{}, parent string, delimiter string) (map[
|
|||
func FlattenMap(input map[string]interface{}) (map[string]interface{}, error) {
|
||||
var flattened map[string]interface{}
|
||||
var err error
|
||||
flattened, err = explodeMap(input, "", delimiter)
|
||||
flattened, err = flattenMap(input, "", delimiter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return flattened, nil
|
||||
}
|
||||
|
||||
func marshal(rec interface{}) (map[string]interface{}, error) {
|
||||
func ToMap(rec interface{}) (map[string]interface{}, error) {
|
||||
// Convert to JSON...
|
||||
b, err := json.Marshal(rec);
|
||||
if err != nil {
|
||||
|
@ -121,8 +121,21 @@ func marshal(rec interface{}) (map[string]interface{}, error) {
|
|||
return m, err
|
||||
}
|
||||
|
||||
func ToStruct(m map[string]interface{}) (interface{}, error) {
|
||||
// Convert to JSON...
|
||||
b, err := json.Marshal(m);
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// ... then convert to map
|
||||
var rec interface{}
|
||||
err = json.Unmarshal(b, &rec)
|
||||
return rec, err
|
||||
}
|
||||
|
||||
func Flatten(input interface{}) (map[string]interface{}, error) {
|
||||
m, err := marshal(input)
|
||||
m, err := ToMap(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue