geo-smart-system/model/tile38/data.go

51 lines
1.3 KiB
Go
Raw Permalink Normal View History

2019-10-20 03:34:17 +00:00
package tile38
2019-10-19 16:38:38 +00:00
import (
"encoding/json"
"github.com/go-redis/redis"
"github.com/tidwall/gjson"
"strconv"
)
2020-01-04 00:44:42 +00:00
type Data struct {
Object []Object `json:"data"`
2019-10-19 16:38:38 +00:00
}
2020-01-04 00:44:42 +00:00
func FromScan(client *redis.Client, name string) (Data, error) {
var tile38Data Data
2019-10-19 16:38:38 +00:00
var err error
v, err := client.Do("SCAN", name).Result()
if err == nil {
data, err := json.Marshal(v)
if err == nil {
jsonConverted := `{"data":` + string(data) + `}`
lengthData := gjson.Get(jsonConverted, "data.1.#")
if lengthData.Int() != 0 {
var x int64 = 0
for x = 0; x < lengthData.Int(); x++ {
name := "data.1." + strconv.FormatInt(int64(x), 10)
idName := name + ".0"
contentName := name + ".1"
id := gjson.Get(jsonConverted, idName)
content := gjson.Get(jsonConverted, contentName)
2020-01-04 00:44:42 +00:00
var tile38Object Object
var tile38SubObject SubObject
2019-10-19 16:38:38 +00:00
err = json.Unmarshal([]byte(content.String()), &tile38SubObject)
if err == nil {
tile38Object.Id = id.String()
tile38Object.Object = tile38SubObject
tile38Data.Object = append(tile38Data.Object, tile38Object)
}
}
}
}
}
return tile38Data, err
}
2020-01-04 00:44:42 +00:00
func (tile38Data Data) ToJsonString() (string, error) {
2019-10-19 16:38:38 +00:00
var err error
data, err := json.Marshal(tile38Data)
return string(data), err
}