Skip to content

Commit

Permalink
Merge pull request MabinGo#2 from mengfanliang/develop
Browse files Browse the repository at this point in the history
Develop ,This week PR merged
  • Loading branch information
fivestarsky committed Jun 25, 2015
2 parents 974bea1 + 249c471 commit 11a0f90
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 43 deletions.
76 changes: 58 additions & 18 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"time"

"gopkg.in/redis.v3"
"strings"
"time"
)

const (
Expand Down Expand Up @@ -52,26 +52,66 @@ func GeneralDBKey(key string) string {
return hex.EncodeToString(h.Sum(nil))
}

func SaveObject(object interface{}, key string) error {
if json, err := json.Marshal(object); err != nil {
return err
func GetUUID(ObjectType, Object string) (UUID string, err error) {

index := ""

switch strings.TrimSpace(ObjectType) {

case "user":
index = GLOBAL_USER_INDEX
case "repository":
index = GLOBAL_REPOSITORY_INDEX
case "organization":
index = GLOBAL_ORGANIZATION_INDEX
case "team":
index = GLOBAL_TEAM_INDEX
case "image":
index = GLOBAL_IMAGE_INDEX
case "tarsum":
index = GLOBAL_TARSUM_INDEX
case "tag":
index = GLOBAL_TAG_INDEX
case "compose":
index = GLOBAL_COMPOSE_INDEX
case "admin":
index = GLOBAL_ADMIN_INDEX
case "log":
index = GLOBAL_LOG_INDEX
default:

}

if UUID, err = Client.HGet(index, Object).Result(); err != nil {
return "", err
} else {
if err := Client.Set(string(json), key, 0).Err(); err != nil {
return err
} else {
return nil
}
return UUID, nil
}
}

func LoadObject(object interface{}, key string) error {
if value, err := Client.Get(key).Result(); err != nil {
func Save(obj interface{}, key string) (err error) {

result, err := json.Marshal(&obj)
if err != nil {
return err
} else {
if err := json.Unmarshal([]byte(value), object); err != nil {
return err
} else {
return nil
}
}

if _, err := Client.Set(key, string(result), 0).Result(); err != nil {
return err
}

return nil
}

func Get(obj interface{}, key string) (err error) {
result, err := Client.Get(key).Result()
if err != nil {
return err
}

if err = json.Unmarshal([]byte(result), &obj); err != nil {
return err
}

return nil
}
30 changes: 5 additions & 25 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/base64"
"fmt"
"os"
"strconv"
"strings"
)

Expand Down Expand Up @@ -50,33 +49,14 @@ func DecodeBasicAuth(authorization string) (username string, password string, er
return username, password, nil
}

func StringToBool(value string) bool {
if boolean, _ := strconv.ParseBool(value); boolean == true {
return true
} else if boolean == false {
return false
} else {
return false
}
}
func IsDirExists(path string) bool {
fi, err := os.Stat(path)

func StringToInt64(value string) int64 {
retval, err := strconv.ParseInt(value, 0, 64)
if err != nil {
fmt.Println("err:=", err)
return 0
}
return retval
}

func BoolToString(boolean bool) string {
if boolean == true {
return "true"
return os.IsExist(err)
} else {
return "false"
return fi.IsDir()
}
}

func Int64ToString(i int64) string {
return strconv.FormatInt(i, 10)
panic("not reached")
}

0 comments on commit 11a0f90

Please sign in to comment.