diff --git a/repositories/base_repository.go b/repositories/base_repository.go index 7bcb2099f..b275504f1 100644 --- a/repositories/base_repository.go +++ b/repositories/base_repository.go @@ -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() { diff --git a/repositories/ledis_database.go b/repositories/ledis_database.go index 8fcf95cf7..2beaf6d08 100644 --- a/repositories/ledis_database.go +++ b/repositories/ledis_database.go @@ -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 diff --git a/utils/flat.go b/utils/mapping.go similarity index 75% rename from utils/flat.go rename to utils/mapping.go index 129919dfa..3a6949f9a 100644 --- a/utils/flat.go +++ b/utils/mapping.go @@ -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 }