cvc/src/loreal.com/dit/cmd/api-tests-for-coupon-service/base/lightutils.go

106 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package base
import (
// "bytes"
"encoding/json"
// "errors"
"fmt"
"log"
"math/rand"
// "mime/multipart"
"net/http"
"strings"
"time"
"github.com/google/uuid"
)
var r *rand.Rand = rand.New(rand.NewSource(time.Now().Unix()))
// IsBlankString 判断是否为空的IDID不能都是空白.
func IsBlankString(str string) bool {
return len(strings.TrimSpace(str)) == 0
}
// IsEmptyString 判断是否为空的字符串.
func IsEmptyString(str string) bool {
return len(str) == 0
}
// IsValidUUID
func IsValidUUID(u string) bool {
_, err := uuid.Parse(u)
return err == nil
}
// SetResponseHeader 一个快捷设置status code 和content type的方法
func SetResponseHeader(w http.ResponseWriter, statusCode int, contentType string) {
w.Header().Set("Content-Type", contentType)
w.WriteHeader(statusCode)
}
// WriteErrorResponse 一个快捷设置包含错误body的response
func WriteErrorResponse(w http.ResponseWriter, statusCode int, contentType string, a interface{}) {
SetResponseHeader(w, statusCode, contentType)
switch vv := a.(type) {
case error:
{
fmt.Fprintf(w, vv.Error())
}
case map[string][]error:
{
jsonBytes, err := json.Marshal(vv)
if nil != err {
log.Println(err)
fmt.Fprintf(w, err.Error())
}
var str = string(jsonBytes)
fmt.Fprintf(w, str)
}
case []error:
{
jsonBytes, err := json.Marshal(vv)
if nil != err {
log.Println(err)
fmt.Fprintf(w, err.Error())
}
var str = string(jsonBytes)
fmt.Fprintf(w, str)
}
}
}
// trimURIPrefix 将一个uri拆分为若干node根据ndoe取得一些动态参数。
func TrimURIPrefix(uri string, stopTag string) []string {
params := strings.Split(strings.TrimPrefix(strings.TrimSuffix(uri, "/"), "/"), "/")
last := len(params) - 1
for i := last; i >= 0; i-- {
if params[i] == stopTag {
return params[i+1:]
}
}
return params
}
//outputJSON - output json for http response
func outputJSON(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json;charset=utf-8")
enc := json.NewEncoder(w)
if err := enc.Encode(data); err != nil {
log.Println("[ERR] - [outputJSON] JSON encode error:", err)
http.Error(w, "500", http.StatusInternalServerError)
return
}
}
// RandString 生成随机字符串
func RandString(len int) string {
bytes := make([]byte, len)
for i := 0; i < len; i++ {
b := r.Intn(26) + 65
bytes[i] = byte(b)
}
return string(bytes)
}