You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-library/params/params.go

58 lines
882 B

package params
import (
"encoding/json"
"net/url"
"strconv"
)
type Params map[string]interface{}
func NewParams() Params {
P := make(Params)
return P
}
func NewParamsWithType(params ...Params) Params {
p := make(Params)
for _, v := range params {
p.SetParams(v)
}
return p
}
func (p Params) Set(key string, value interface{}) {
p[key] = value
}
func (p Params) SetParams(params Params) {
for key, value := range params {
p[key] = value
}
}
func (p Params) GetQuery() string {
u := url.Values{}
for k, v := range p {
u.Set(k, GetParamsString(v))
}
return u.Encode()
}
func GetParamsString(i interface{}) string {
switch v := i.(type) {
case string:
return v
case []byte:
return string(v)
case int:
return strconv.Itoa(v)
case bool:
return strconv.FormatBool(v)
default:
bytes, _ := json.Marshal(v)
return string(bytes)
}
return ""
}