add default value feature for gjson/gparser; update default value type for ghttp.Request

This commit is contained in:
John
2019-05-09 14:19:27 +08:00
parent 2b865a55ac
commit fdfefbb94d
10 changed files with 280 additions and 351 deletions

View File

@ -15,7 +15,7 @@ import (
)
// Val returns the json value.
func (j *Json) Val() interface{} {
func (j *Json) Value() interface{} {
j.mu.RLock()
defer j.mu.RUnlock()
return *(j.p)
@ -27,35 +27,36 @@ func (j *Json) Val() interface{} {
//
// We can also access slice item by its index number in <pattern>,
// eg: "items.name.first", "list.10".
func (j *Json) Get(pattern...string) interface{} {
//
// It returns a default value specified by <def> if value for <pattern> is not found.
func (j *Json) Get(pattern string, def...interface{}) interface{} {
j.mu.RLock()
defer j.mu.RUnlock()
queryPattern := ""
if len(pattern) > 0 {
queryPattern = pattern[0]
}
var result *interface{}
if j.vc {
result = j.getPointerByPattern(queryPattern)
result = j.getPointerByPattern(pattern)
} else {
result = j.getPointerByPatternWithoutViolenceCheck(queryPattern)
result = j.getPointerByPatternWithoutViolenceCheck(pattern)
}
if result != nil {
return *result
}
if len(def) > 0 {
return def[0]
}
return nil
}
// GetVar returns a *gvar.Var with value by given <pattern>.
func (j *Json) GetVar(pattern...string) *gvar.Var {
return gvar.New(j.Get(pattern...), true)
func (j *Json) GetVar(pattern string, def...interface{}) *gvar.Var {
return gvar.New(j.Get(pattern, def...), true)
}
// GetMap gets the value by specified <pattern>,
// and converts it to map[string]interface{}.
func (j *Json) GetMap(pattern string) map[string]interface{} {
result := j.Get(pattern)
func (j *Json) GetMap(pattern string, def...interface{}) map[string]interface{} {
result := j.Get(pattern, def...)
if result != nil {
return gconv.Map(result)
}
@ -64,8 +65,8 @@ func (j *Json) GetMap(pattern string) map[string]interface{} {
// GetJson gets the value by specified <pattern>,
// and converts it to a Json object.
func (j *Json) GetJson(pattern string) *Json {
result := j.Get(pattern)
func (j *Json) GetJson(pattern string, def...interface{}) *Json {
result := j.Get(pattern, def...)
if result != nil {
return New(result)
}
@ -74,8 +75,8 @@ func (j *Json) GetJson(pattern string) *Json {
// GetJsons gets the value by specified <pattern>,
// and converts it to a slice of Json object.
func (j *Json) GetJsons(pattern string) []*Json {
array := j.GetArray(pattern)
func (j *Json) GetJsons(pattern string, def...interface{}) []*Json {
array := j.GetArray(pattern, def...)
if len(array) > 0 {
jsons := make([]*Json, len(array))
for i := 0; i < len(array); i++ {
@ -88,101 +89,101 @@ func (j *Json) GetJsons(pattern string) []*Json {
// GetArray gets the value by specified <pattern>,
// and converts it to a slice of []interface{}.
func (j *Json) GetArray(pattern string) []interface{} {
return gconv.Interfaces(j.Get(pattern))
func (j *Json) GetArray(pattern string, def...interface{}) []interface{} {
return gconv.Interfaces(j.Get(pattern, def...))
}
// GetString gets the value by specified <pattern>,
// and converts it to string.
func (j *Json) GetString(pattern string) string {
return gconv.String(j.Get(pattern))
func (j *Json) GetString(pattern string, def...interface{}) string {
return gconv.String(j.Get(pattern, def...))
}
// GetBool gets the value by specified <pattern>,
// and converts it to bool.
// It returns false when value is: "", 0, false, off, nil;
// or returns true instead.
func (j *Json) GetBool(pattern string) bool {
return gconv.Bool(j.Get(pattern))
func (j *Json) GetBool(pattern string, def...interface{}) bool {
return gconv.Bool(j.Get(pattern, def...))
}
func (j *Json) GetInt(pattern string) int {
return gconv.Int(j.Get(pattern))
func (j *Json) GetInt(pattern string, def...interface{}) int {
return gconv.Int(j.Get(pattern, def...))
}
func (j *Json) GetInt8(pattern string) int8 {
return gconv.Int8(j.Get(pattern))
func (j *Json) GetInt8(pattern string, def...interface{}) int8 {
return gconv.Int8(j.Get(pattern, def...))
}
func (j *Json) GetInt16(pattern string) int16 {
return gconv.Int16(j.Get(pattern))
func (j *Json) GetInt16(pattern string, def...interface{}) int16 {
return gconv.Int16(j.Get(pattern, def...))
}
func (j *Json) GetInt32(pattern string) int32 {
return gconv.Int32(j.Get(pattern))
func (j *Json) GetInt32(pattern string, def...interface{}) int32 {
return gconv.Int32(j.Get(pattern, def...))
}
func (j *Json) GetInt64(pattern string) int64 {
return gconv.Int64(j.Get(pattern))
func (j *Json) GetInt64(pattern string, def...interface{}) int64 {
return gconv.Int64(j.Get(pattern, def...))
}
func (j *Json) GetInts(pattern string) []int {
return gconv.Ints(j.Get(pattern))
func (j *Json) GetUint(pattern string, def...interface{}) uint {
return gconv.Uint(j.Get(pattern, def...))
}
func (j *Json) GetUint(pattern string) uint {
return gconv.Uint(j.Get(pattern))
func (j *Json) GetUint8(pattern string, def...interface{}) uint8 {
return gconv.Uint8(j.Get(pattern, def...))
}
func (j *Json) GetUint8(pattern string) uint8 {
return gconv.Uint8(j.Get(pattern))
func (j *Json) GetUint16(pattern string, def...interface{}) uint16 {
return gconv.Uint16(j.Get(pattern, def...))
}
func (j *Json) GetUint16(pattern string) uint16 {
return gconv.Uint16(j.Get(pattern))
func (j *Json) GetUint32(pattern string, def...interface{}) uint32 {
return gconv.Uint32(j.Get(pattern, def...))
}
func (j *Json) GetUint32(pattern string) uint32 {
return gconv.Uint32(j.Get(pattern))
func (j *Json) GetUint64(pattern string, def...interface{}) uint64 {
return gconv.Uint64(j.Get(pattern, def...))
}
func (j *Json) GetUint64(pattern string) uint64 {
return gconv.Uint64(j.Get(pattern))
func (j *Json) GetFloat32(pattern string, def...interface{}) float32 {
return gconv.Float32(j.Get(pattern, def...))
}
func (j *Json) GetFloat32(pattern string) float32 {
return gconv.Float32(j.Get(pattern))
func (j *Json) GetFloat64(pattern string, def...interface{}) float64 {
return gconv.Float64(j.Get(pattern, def...))
}
func (j *Json) GetFloat64(pattern string) float64 {
return gconv.Float64(j.Get(pattern))
func (j *Json) GetFloats(pattern string, def...interface{}) []float64 {
return gconv.Floats(j.Get(pattern, def...))
}
func (j *Json) GetFloats(pattern string) []float64 {
return gconv.Floats(j.Get(pattern))
func (j *Json) GetInts(pattern string, def...interface{}) []int {
return gconv.Ints(j.Get(pattern, def...))
}
// GetStrings gets the value by specified <pattern>,
// and converts it to a slice of []string.
func (j *Json) GetStrings(pattern string) []string {
return gconv.Strings(j.Get(pattern))
func (j *Json) GetStrings(pattern string, def...interface{}) []string {
return gconv.Strings(j.Get(pattern, def...))
}
// See GetArray.
func (j *Json) GetInterfaces(pattern string) []interface{} {
return gconv.Interfaces(j.Get(pattern))
func (j *Json) GetInterfaces(pattern string, def...interface{}) []interface{} {
return gconv.Interfaces(j.Get(pattern, def...))
}
func (j *Json) GetTime(pattern string, format ... string) time.Time {
func (j *Json) GetTime(pattern string, format... string) time.Time {
return gconv.Time(j.Get(pattern), format...)
}
func (j *Json) GetTimeDuration(pattern string) time.Duration {
return gconv.TimeDuration(j.Get(pattern))
func (j *Json) GetTimeDuration(pattern string, def...interface{}) time.Duration {
return gconv.TimeDuration(j.Get(pattern, def...))
}
func (j *Json) GetGTime(pattern string) *gtime.Time {
return gconv.GTime(j.Get(pattern))
func (j *Json) GetGTime(pattern string, format... string) *gtime.Time {
return gconv.GTime(j.Get(pattern), format...)
}
// Set sets value with specified <pattern>.
@ -236,17 +237,17 @@ func (j *Json) Append(pattern string, value interface{}) error {
// GetToVar gets the value by specified <pattern>,
// and converts it to specified golang variable <v>.
// The <v> should be a pointer type.
func (j *Json) GetToVar(pattern string, v interface{}) error {
// The <pointer> should be a pointer type.
func (j *Json) GetToVar(pattern string, pointer interface{}) error {
r := j.Get(pattern)
if r != nil {
if t, err := Encode(r); err == nil {
return DecodeTo(t, v)
return DecodeTo(t, pointer)
} else {
return err
}
} else {
v = nil
pointer = nil
}
return nil
}
@ -254,8 +255,8 @@ func (j *Json) GetToVar(pattern string, v interface{}) error {
// GetToStruct gets the value by specified <pattern>,
// and converts it to specified object <objPointer>.
// The <objPointer> should be the pointer to an object.
func (j *Json) GetToStruct(pattern string, objPointer interface{}) error {
return gconv.Struct(j.Get(pattern), objPointer)
func (j *Json) GetToStruct(pattern string, pointer interface{}) error {
return gconv.Struct(j.Get(pattern), pointer)
}
// ToMap converts current Json object to map[string]interface{}.

View File

@ -13,8 +13,8 @@ import (
)
// Val returns the value.
func (p *Parser) Val() interface{} {
return p.json.Val()
func (p *Parser) Value() interface{} {
return p.json.Value()
}
// Get returns value by specified <pattern>.
@ -23,131 +23,133 @@ func (p *Parser) Val() interface{} {
//
// We can also access slice item by its index number in <pattern>,
// eg: "items.name.first", "list.10".
func (p *Parser) Get(pattern...string) interface{} {
return p.json.Get(pattern...)
//
// It returns a default value specified by <def> if value for <pattern> is not found.
func (p *Parser) Get(pattern string, def...interface{}) interface{} {
return p.json.Get(pattern, def...)
}
// GetVar returns a *gvar.Var with value by given <pattern>.
func (p *Parser) GetVar(pattern...string) *gvar.Var {
return p.json.GetVar(pattern...)
func (p *Parser) GetVar(pattern string, def...interface{}) *gvar.Var {
return p.json.GetVar(pattern, def...)
}
// GetMap gets the value by specified <pattern>,
// and converts it to map[string]interface{}.
func (p *Parser) GetMap(pattern string) map[string]interface{} {
return p.json.GetMap(pattern)
func (p *Parser) GetMap(pattern string, def...interface{}) map[string]interface{} {
return p.json.GetMap(pattern, def...)
}
// GetArray gets the value by specified <pattern>,
// and converts it to a slice of []interface{}.
func (p *Parser) GetArray(pattern string) []interface{} {
return p.json.GetArray(pattern)
func (p *Parser) GetArray(pattern string, def...interface{}) []interface{} {
return p.json.GetArray(pattern, def...)
}
// GetString gets the value by specified <pattern>,
// and converts it to string.
func (p *Parser) GetString(pattern string) string {
return p.json.GetString(pattern)
func (p *Parser) GetString(pattern string, def...interface{}) string {
return p.json.GetString(pattern, def...)
}
// GetBool gets the value by specified <pattern>,
// and converts it to bool.
// It returns false when value is: "", 0, false, off, nil;
// or returns true instead.
func (p *Parser) GetBool(pattern string) bool {
return p.json.GetBool(pattern)
func (p *Parser) GetBool(pattern string, def...interface{}) bool {
return p.json.GetBool(pattern, def...)
}
func (p *Parser) GetInt(pattern string) int {
return p.json.GetInt(pattern)
func (p *Parser) GetInt(pattern string, def...interface{}) int {
return p.json.GetInt(pattern, def...)
}
func (p *Parser) GetInt8(pattern string) int8 {
return p.json.GetInt8(pattern)
func (p *Parser) GetInt8(pattern string, def...interface{}) int8 {
return p.json.GetInt8(pattern, def...)
}
func (p *Parser) GetInt16(pattern string) int16 {
return p.json.GetInt16(pattern)
func (p *Parser) GetInt16(pattern string, def...interface{}) int16 {
return p.json.GetInt16(pattern, def...)
}
func (p *Parser) GetInt32(pattern string) int32 {
return p.json.GetInt32(pattern)
func (p *Parser) GetInt32(pattern string, def...interface{}) int32 {
return p.json.GetInt32(pattern, def...)
}
func (p *Parser) GetInt64(pattern string) int64 {
return p.json.GetInt64(pattern)
func (p *Parser) GetInt64(pattern string, def...interface{}) int64 {
return p.json.GetInt64(pattern, def...)
}
func (p *Parser) GetInts(pattern string) []int {
return p.json.GetInts(pattern)
func (p *Parser) GetInts(pattern string, def...interface{}) []int {
return p.json.GetInts(pattern, def...)
}
func (p *Parser) GetUint(pattern string) uint {
return p.json.GetUint(pattern)
func (p *Parser) GetUint(pattern string, def...interface{}) uint {
return p.json.GetUint(pattern, def...)
}
func (p *Parser) GetUint8(pattern string) uint8 {
return p.json.GetUint8(pattern)
func (p *Parser) GetUint8(pattern string, def...interface{}) uint8 {
return p.json.GetUint8(pattern, def...)
}
func (p *Parser) GetUint16(pattern string) uint16 {
return p.json.GetUint16(pattern)
func (p *Parser) GetUint16(pattern string, def...interface{}) uint16 {
return p.json.GetUint16(pattern, def...)
}
func (p *Parser) GetUint32(pattern string) uint32 {
return p.json.GetUint32(pattern)
func (p *Parser) GetUint32(pattern string, def...interface{}) uint32 {
return p.json.GetUint32(pattern, def...)
}
func (p *Parser) GetUint64(pattern string) uint64 {
return p.json.GetUint64(pattern)
func (p *Parser) GetUint64(pattern string, def...interface{}) uint64 {
return p.json.GetUint64(pattern, def...)
}
func (p *Parser) GetFloat32(pattern string) float32 {
return p.json.GetFloat32(pattern)
func (p *Parser) GetFloat32(pattern string, def...interface{}) float32 {
return p.json.GetFloat32(pattern, def...)
}
func (p *Parser) GetFloat64(pattern string) float64 {
return p.json.GetFloat64(pattern)
func (p *Parser) GetFloat64(pattern string, def...interface{}) float64 {
return p.json.GetFloat64(pattern, def...)
}
func (p *Parser) GetFloats(pattern string) []float64 {
return p.json.GetFloats(pattern)
func (p *Parser) GetFloats(pattern string, def...interface{}) []float64 {
return p.json.GetFloats(pattern, def...)
}
// GetStrings gets the value by specified <pattern>,
// and converts it to a slice of []string.
func (p *Parser) GetStrings(pattern string) []string {
return p.json.GetStrings(pattern)
func (p *Parser) GetStrings(pattern string, def...interface{}) []string {
return p.json.GetStrings(pattern, def...)
}
func (p *Parser) GetInterfaces(pattern string) []interface{} {
return p.json.GetInterfaces(pattern)
func (p *Parser) GetInterfaces(pattern string, def...interface{}) []interface{} {
return p.json.GetInterfaces(pattern, def...)
}
func (p *Parser) GetTime(pattern string, format ... string) time.Time {
func (p *Parser) GetTime(pattern string, format...string) time.Time {
return p.json.GetTime(pattern, format...)
}
func (p *Parser) GetTimeDuration(pattern string) time.Duration {
return p.json.GetTimeDuration(pattern)
func (p *Parser) GetTimeDuration(pattern string, def...interface{}) time.Duration {
return p.json.GetTimeDuration(pattern, def...)
}
func (p *Parser) GetGTime(pattern string) *gtime.Time {
return p.json.GetGTime(pattern)
func (p *Parser) GetGTime(pattern string, format...string) *gtime.Time {
return p.json.GetGTime(pattern, format...)
}
// GetToVar gets the value by specified <pattern>,
// and converts it to specified golang variable <v>.
// The <v> should be a pointer type.
func (p *Parser) GetToVar(pattern string, v interface{}) error {
return p.json.GetToVar(pattern, v)
func (p *Parser) GetToVar(pattern string, pointer interface{}) error {
return p.json.GetToVar(pattern, pointer)
}
// GetToStruct gets the value by specified <pattern>,
// and converts it to specified object <objPointer>.
// The <objPointer> should be the pointer to an object.
func (p *Parser) GetToStruct(pattern string, objPointer interface{}) error {
return p.json.GetToStruct(pattern, objPointer)
// and converts it to specified object <pointer>.
// The <pointer> should be the pointer to a struct.
func (p *Parser) GetToStruct(pattern string, pointer interface{}) error {
return p.json.GetToStruct(pattern, pointer)
}
// Set sets value with specified <pattern>.

View File

@ -20,7 +20,7 @@ func New(value interface{}, unsafe...bool) *Parser {
}
// NewUnsafe creates a un-concurrent-safe Parser object.
func NewUnsafe (value...interface{}) *Parser {
func NewUnsafe(value...interface{}) *Parser {
if len(value) > 0 {
return &Parser{gjson.New(value[0], false)}
}
@ -29,7 +29,7 @@ func NewUnsafe (value...interface{}) *Parser {
// Load loads content from specified file <path>,
// and creates a Parser object from its content.
func Load (path string, unsafe...bool) (*Parser, error) {
func Load(path string, unsafe...bool) (*Parser, error) {
if j, e := gjson.Load(path, unsafe...); e == nil {
return &Parser{j}, nil
} else {
@ -40,7 +40,7 @@ func Load (path string, unsafe...bool) (*Parser, error) {
// LoadContent creates a Parser object from given content,
// it checks the data type of <content> automatically,
// supporting JSON, XML, YAML and TOML types of data.
func LoadContent (data []byte, unsafe...bool) (*Parser, error) {
func LoadContent(data []byte, unsafe...bool) (*Parser, error) {
if j, e := gjson.LoadContent(data, unsafe...); e == nil {
return &Parser{j}, nil
} else {

View File

@ -71,12 +71,12 @@ func (r *Request) WebSocket() (*WebSocket, error) {
// 获得指定名称的参数字符串(Router/GET/POST),同 GetRequestString
// 这是常用方法的简化别名
func (r *Request) Get(key string, def ... string) string {
func (r *Request) Get(key string, def...interface{}) string {
return r.GetRequestString(key, def...)
}
// 建议都用该参数替代参数获取
func (r *Request) GetVar(key string, def ... interface{}) gvar.VarRead {
func (r *Request) GetVar(key string, def...interface{}) gvar.VarRead {
return r.GetRequestVar(key, def...)
}
@ -110,43 +110,43 @@ func (r *Request) GetJson() *gjson.Json {
return nil
}
func (r *Request) GetString(key string, def ... string) string {
func (r *Request) GetString(key string, def...interface{}) string {
return r.GetRequestString(key, def...)
}
func (r *Request) GetInt(key string, def ... int) int {
func (r *Request) GetInt(key string, def...interface{}) int {
return r.GetRequestInt(key, def...)
}
func (r *Request) GetInts(key string, def ... []int) []int {
func (r *Request) GetInts(key string, def...interface{}) []int {
return r.GetRequestInts(key, def...)
}
func (r *Request) GetUint(key string, def ... uint) uint {
func (r *Request) GetUint(key string, def...interface{}) uint {
return r.GetRequestUint(key, def...)
}
func (r *Request) GetFloat32(key string, def ... float32) float32 {
func (r *Request) GetFloat32(key string, def...interface{}) float32 {
return r.GetRequestFloat32(key, def...)
}
func (r *Request) GetFloat64(key string, def ... float64) float64 {
func (r *Request) GetFloat64(key string, def...interface{}) float64 {
return r.GetRequestFloat64(key, def...)
}
func (r *Request) GetFloats(key string, def ... []float64) []float64 {
func (r *Request) GetFloats(key string, def...interface{}) []float64 {
return r.GetRequestFloats(key, def...)
}
func (r *Request) GetArray(key string, def ... []string) []string {
func (r *Request) GetArray(key string, def...interface{}) []string {
return r.GetRequestArray(key, def...)
}
func (r *Request) GetStrings(key string, def ... []string) []string {
func (r *Request) GetStrings(key string, def...interface{}) []string {
return r.GetRequestStrings(key, def...)
}
func (r *Request) GetInterfaces(key string, def ... []interface{}) []interface{} {
func (r *Request) GetInterfaces(key string, def...interface{}) []interface{} {
return r.GetRequestInterfaces(key, def...)
}
@ -154,9 +154,10 @@ func (r *Request) GetMap(def...map[string]string) map[string]string {
return r.GetRequestMap(def...)
}
// 将所有的request参数映射到struct属性上参数object应当为一个struct对象的指针, mapping为非必需参数自定义参数与属性的映射关系
func (r *Request) GetToStruct(object interface{}, mapping...map[string]string) {
r.GetRequestToStruct(object, mapping...)
// 将所有的request参数映射到struct属性上参数pointer应当为一个struct对象的指针,
// mapping为非必需参数自定义参数与属性的映射关系
func (r *Request) GetToStruct(pointer interface{}, mapping...map[string]string) {
r.GetRequestToStruct(pointer, mapping...)
}
// 仅退出当前逻辑执行函数, 如:服务函数、HOOK函数
@ -233,9 +234,9 @@ func (r *Request) GetReferer() string {
}
// 获得结构体对象的参数名称标签构成map返回
func (r *Request) getStructParamsTagMap(object interface{}) map[string]string {
func (r *Request) getStructParamsTagMap(pointer interface{}) map[string]string {
tagMap := make(map[string]string)
fields := structs.Fields(object)
fields := structs.Fields(pointer)
for _, field := range fields {
if tag := field.Tag("params"); tag != "" {
for _, v := range strings.Split(tag, ",") {

View File

@ -17,12 +17,15 @@ func (r *Request) SetParam(key string, value interface{}) {
}
// 获取请求流程共享变量
func (r *Request) GetParam(key string) gvar.VarRead {
func (r *Request) GetParam(key string, def...interface{}) gvar.VarRead {
if r.params != nil {
if v, ok := r.params[key]; ok {
return gvar.New(v, true)
}
}
if len(def) > 0 {
return gvar.New(def[0], true)
}
return gvar.New(nil, true)
}

View File

@ -32,121 +32,94 @@ func (r *Request) AddPost(key string, value string) {
}
// 获得post参数
func (r *Request) GetPost(key string, def...[]string) []string {
func (r *Request) GetPost(key string, def...interface{}) []string {
r.initPost()
if v, ok := r.PostForm[key]; ok {
return v
}
if len(def) > 0 {
return def[0]
return gconv.Strings(def[0])
}
return nil
}
func (r *Request) GetPostString(key string, def ... string) string {
value := r.GetPost(key)
func (r *Request) GetPostString(key string, def...interface{}) string {
value := r.GetPost(key, def...)
if value != nil && value[0] != "" {
return value[0]
}
if len(def) > 0 {
return def[0]
}
return ""
}
func (r *Request) GetPostBool(key string, def ... bool) bool {
value := r.GetPostString(key)
func (r *Request) GetPostBool(key string, def...interface{}) bool {
value := r.GetPostString(key, def...)
if value != "" {
return gconv.Bool(value)
}
if len(def) > 0 {
return def[0]
}
return false
}
func (r *Request) GetPostInt(key string, def ... int) int {
value := r.GetPostString(key)
func (r *Request) GetPostInt(key string, def...interface{}) int {
value := r.GetPostString(key, def...)
if value != "" {
return gconv.Int(value)
}
if len(def) > 0 {
return def[0]
}
return 0
}
func (r *Request) GetPostInts(key string, def ... []int) []int {
value := r.GetPost(key)
func (r *Request) GetPostInts(key string, def...interface{}) []int {
value := r.GetPost(key, def...)
if value != nil {
return gconv.Ints(value)
}
if len(def) > 0 {
return def[0]
}
return nil
}
func (r *Request) GetPostUint(key string, def ... uint) uint {
value := r.GetPostString(key)
func (r *Request) GetPostUint(key string, def...interface{}) uint {
value := r.GetPostString(key, def...)
if value != "" {
return gconv.Uint(value)
}
if len(def) > 0 {
return def[0]
}
return 0
}
func (r *Request) GetPostFloat32(key string, def ... float32) float32 {
value := r.GetPostString(key)
func (r *Request) GetPostFloat32(key string, def...interface{}) float32 {
value := r.GetPostString(key, def...)
if value != "" {
return gconv.Float32(value)
}
if len(def) > 0 {
return def[0]
}
return 0
}
func (r *Request) GetPostFloat64(key string, def ... float64) float64 {
value := r.GetPostString(key)
func (r *Request) GetPostFloat64(key string, def...interface{}) float64 {
value := r.GetPostString(key, def...)
if value != "" {
return gconv.Float64(value)
}
if len(def) > 0 {
return def[0]
}
return 0
}
func (r *Request) GetPostFloats(key string, def ... []float64) []float64 {
value := r.GetPost(key)
func (r *Request) GetPostFloats(key string, def...interface{}) []float64 {
value := r.GetPost(key, def...)
if value != nil {
return gconv.Floats(value)
}
if len(def) > 0 {
return def[0]
}
return nil
}
func (r *Request) GetPostArray(key string, def ... []string) []string {
func (r *Request) GetPostArray(key string, def...interface{}) []string {
return r.GetPost(key, def...)
}
func (r *Request) GetPostStrings(key string, def ... []string) []string {
func (r *Request) GetPostStrings(key string, def...interface{}) []string {
return r.GetPost(key, def...)
}
func (r *Request) GetPostInterfaces(key string, def ... []interface{}) []interface{} {
value := r.GetPost(key)
func (r *Request) GetPostInterfaces(key string, def...interface{}) []interface{} {
value := r.GetPost(key, def...)
if value != nil {
return gconv.Interfaces(value)
}
if len(def) > 0 {
return def[0]
}
return nil
}

View File

@ -41,126 +41,99 @@ func (r *Request) AddQuery(key string, value string) {
}
// 获得指定名称的get参数列表
func (r *Request) GetQuery(key string, def ... []string) []string {
func (r *Request) GetQuery(key string, def...interface{}) []string {
r.initGet()
if v, ok := r.queryVars[key]; ok {
return v
}
if len(def) > 0 {
return def[0]
return gconv.Strings(def[0])
}
return nil
}
func (r *Request) GetQueryString(key string, def ... string) string {
value := r.GetQuery(key)
func (r *Request) GetQueryString(key string, def...interface{}) string {
value := r.GetQuery(key, def...)
if value != nil && value[0] != "" {
return value[0]
}
if len(def) > 0 {
return def[0]
}
return ""
}
func (r *Request) GetQueryBool(key string, def ... bool) bool {
value := r.GetQueryString(key)
func (r *Request) GetQueryBool(key string, def...interface{}) bool {
value := r.GetQueryString(key, def...)
if value != "" {
return gconv.Bool(value)
}
if len(def) > 0 {
return def[0]
}
return false
}
func (r *Request) GetQueryInt(key string, def ... int) int {
value := r.GetQueryString(key)
func (r *Request) GetQueryInt(key string, def...interface{}) int {
value := r.GetQueryString(key, def...)
if value != "" {
return gconv.Int(value)
}
if len(def) > 0 {
return def[0]
}
return 0
}
func (r *Request) GetQueryInts(key string, def ... []int) []int {
value := r.GetQuery(key)
func (r *Request) GetQueryInts(key string, def...interface{}) []int {
value := r.GetQuery(key, def...)
if value != nil {
return gconv.Ints(value)
}
if len(def) > 0 {
return def[0]
}
return nil
}
func (r *Request) GetQueryUint(key string, def ... uint) uint {
value := r.GetQueryString(key)
func (r *Request) GetQueryUint(key string, def...interface{}) uint {
value := r.GetQueryString(key, def...)
if value != "" {
return gconv.Uint(value)
}
if len(def) > 0 {
return def[0]
}
return 0
}
func (r *Request) GetQueryFloat32(key string, def ... float32) float32 {
value := r.GetQueryString(key)
func (r *Request) GetQueryFloat32(key string, def...interface{}) float32 {
value := r.GetQueryString(key, def...)
if value != "" {
return gconv.Float32(value)
}
if len(def) > 0 {
return def[0]
}
return 0
}
func (r *Request) GetQueryFloat64(key string, def ... float64) float64 {
value := r.GetQueryString(key)
func (r *Request) GetQueryFloat64(key string, def...interface{}) float64 {
value := r.GetQueryString(key, def...)
if value != "" {
return gconv.Float64(value)
}
if len(def) > 0 {
return def[0]
}
return 0
}
func (r *Request) GetQueryFloats(key string, def ... []float64) []float64 {
value := r.GetQuery(key)
func (r *Request) GetQueryFloats(key string, def...interface{}) []float64 {
value := r.GetQuery(key, def...)
if value != nil {
return gconv.Floats(value)
}
if len(def) > 0 {
return def[0]
}
return nil
}
func (r *Request) GetQueryArray(key string, def ... []string) []string {
func (r *Request) GetQueryArray(key string, def...interface{}) []string {
return r.GetQuery(key, def...)
}
func (r *Request) GetQueryStrings(key string, def ... []string) []string {
func (r *Request) GetQueryStrings(key string, def...interface{}) []string {
return r.GetQuery(key, def...)
}
func (r *Request) GetQueryInterfaces(key string, def ... []interface{}) []interface{} {
value := r.GetQuery(key)
func (r *Request) GetQueryInterfaces(key string, def...interface{}) []interface{} {
value := r.GetQuery(key, def...)
if value != nil {
return gconv.Interfaces(value)
}
if len(def) > 0 {
return def[0]
}
return nil
}
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
func (r *Request) GetQueryMap(def ... map[string]string) map[string]string {
func (r *Request) GetQueryMap(def... map[string]string) map[string]string {
r.initGet()
m := make(map[string]string)
for k, v := range r.queryVars {

View File

@ -12,7 +12,7 @@ import (
)
// 获得router、post或者get提交的参数如果有同名参数那么按照router->get->post优先级进行覆盖
func (r *Request) GetRequest(key string, def ... []string) []string {
func (r *Request) GetRequest(key string, def...interface{}) []string {
v := r.GetRouterArray(key)
if v == nil {
v = r.GetQuery(key)
@ -21,126 +21,96 @@ func (r *Request) GetRequest(key string, def ... []string) []string {
v = r.GetPost(key)
}
if v == nil && len(def) > 0 {
return def[0]
return gconv.Strings(def[0])
}
return v
}
func (r *Request) GetRequestVar(key string, def ... interface{}) gvar.VarRead {
value := r.GetRequest(key)
func (r *Request) GetRequestVar(key string, def...interface{}) gvar.VarRead {
value := r.GetRequest(key, def...)
if value != nil {
return gvar.New(value[0], true)
}
if len(def) > 0 {
return gvar.New(def[0], true)
}
return gvar.New(nil, true)
}
func (r *Request) GetRequestString(key string, def ... string) string {
value := r.GetRequest(key)
func (r *Request) GetRequestString(key string, def...interface{}) string {
value := r.GetRequest(key, def...)
if value != nil && value[0] != "" {
return value[0]
}
if len(def) > 0 {
return def[0]
}
return ""
}
func (r *Request) GetRequestBool(key string, def ... bool) bool {
value := r.GetRequestString(key)
func (r *Request) GetRequestBool(key string, def...interface{}) bool {
value := r.GetRequestString(key, def...)
if value != "" {
return gconv.Bool(value)
}
if len(def) > 0 {
return def[0]
}
return false
}
func (r *Request) GetRequestInt(key string, def ... int) int {
value := r.GetRequestString(key)
func (r *Request) GetRequestInt(key string, def...interface{}) int {
value := r.GetRequestString(key, def...)
if value != "" {
return gconv.Int(value)
}
if len(def) > 0 {
return def[0]
}
return 0
}
func (r *Request) GetRequestInts(key string, def ... []int) []int {
value := r.GetRequest(key)
func (r *Request) GetRequestInts(key string, def...interface{}) []int {
value := r.GetRequest(key, def...)
if value != nil {
return gconv.Ints(value)
}
if len(def) > 0 {
return def[0]
}
return nil
}
func (r *Request) GetRequestUint(key string, def ... uint) uint {
value := r.GetRequestString(key)
func (r *Request) GetRequestUint(key string, def...interface{}) uint {
value := r.GetRequestString(key, def...)
if value != "" {
return gconv.Uint(value)
}
if len(def) > 0 {
return def[0]
}
return 0
}
func (r *Request) GetRequestFloat32(key string, def ... float32) float32 {
value := r.GetRequestString(key)
func (r *Request) GetRequestFloat32(key string, def...interface{}) float32 {
value := r.GetRequestString(key, def...)
if value != "" {
return gconv.Float32(value)
}
if len(def) > 0 {
return def[0]
}
return 0
}
func (r *Request) GetRequestFloat64(key string, def ... float64) float64 {
value := r.GetRequestString(key)
func (r *Request) GetRequestFloat64(key string, def...interface{}) float64 {
value := r.GetRequestString(key, def...)
if value != "" {
return gconv.Float64(value)
}
if len(def) > 0 {
return def[0]
}
return 0
}
func (r *Request) GetRequestFloats(key string, def ... []float64) []float64 {
value := r.GetRequest(key)
func (r *Request) GetRequestFloats(key string, def...interface{}) []float64 {
value := r.GetRequest(key, def...)
if value != nil {
return gconv.Floats(value)
}
if len(def) > 0 {
return def[0]
}
return nil
}
func (r *Request) GetRequestArray(key string, def ... []string) []string {
func (r *Request) GetRequestArray(key string, def...interface{}) []string {
return r.GetRequest(key, def...)
}
func (r *Request) GetRequestStrings(key string, def ... []string) []string {
func (r *Request) GetRequestStrings(key string, def...interface{}) []string {
return r.GetRequest(key, def...)
}
func (r *Request) GetRequestInterfaces(key string, def ... []interface{}) []interface{} {
value := r.GetRequest(key)
func (r *Request) GetRequestInterfaces(key string, def...interface{}) []interface{} {
value := r.GetRequest(key, def...)
if value != nil {
return gconv.Interfaces(value)
}
if len(def) > 0 {
return def[0]
}
return nil
}
@ -162,8 +132,8 @@ func (r *Request) GetRequestMap(def...map[string]string) map[string]string {
}
// 将所有的request参数映射到struct属性上参数object应当为一个struct对象的指针, mapping为非必需参数自定义参数与属性的映射关系
func (r *Request) GetRequestToStruct(object interface{}, mapping...map[string]string) error {
tagmap := r.getStructParamsTagMap(object)
func (r *Request) GetRequestToStruct(pointer interface{}, mapping...map[string]string) error {
tagmap := r.getStructParamsTagMap(pointer)
if len(mapping) > 0 {
for k, v := range mapping[0] {
tagmap[k] = v
@ -178,6 +148,6 @@ func (r *Request) GetRequestToStruct(object interface{}, mapping...map[string]st
params = j.ToMap()
}
}
return gconv.Struct(params, object, tagmap)
return gconv.Struct(params, pointer, tagmap)
}

View File

@ -134,15 +134,16 @@ func (c *Cookie) SetSessionId(id string) {
}
// 查询cookie
func (c *Cookie) Get(key string) string {
func (c *Cookie) Get(key string, def...string) string {
c.init()
if r, ok := c.data[key]; ok {
if r.expire >= 0 {
return r.value
} else {
return ""
}
}
if len(def) > 0 {
return def[0]
}
return ""
}

View File

@ -99,17 +99,22 @@ func (s *Session) Contains (key string) bool {
}
// 获取SESSION变量
func (s *Session) Get(key string) interface{} {
func (s *Session) Get(key string, def...interface{}) interface{} {
if len(s.id) > 0 || s.request.Cookie.GetSessionId() != "" {
s.init()
return s.data.Get(key)
if v := s.data.Get(key); v != nil {
return v
}
}
if len(def) > 0 {
return def[0]
}
return nil
}
// 获取SESSION建议都用该方法获取参数
func (s *Session) GetVar(key string) gvar.VarRead {
return gvar.NewRead(s.Get(key), true)
func (s *Session) GetVar(key string, def...interface{}) gvar.VarRead {
return gvar.NewRead(s.Get(key, def...), true)
}
// 删除session
@ -135,80 +140,80 @@ func (s *Session) UpdateExpire() {
}
}
func (s *Session) GetString(key string) string {
return gconv.String(s.Get(key))
func (s *Session) GetString(key string, def...interface{}) string {
return gconv.String(s.Get(key, def...))
}
func (s *Session) GetBool(key string) bool {
return gconv.Bool(s.Get(key))
func (s *Session) GetBool(key string, def...interface{}) bool {
return gconv.Bool(s.Get(key, def...))
}
func (s *Session) GetInt(key string) int {
return gconv.Int(s.Get(key)) }
func (s *Session) GetInt8(key string) int8 {
return gconv.Int8(s.Get(key))
func (s *Session) GetInt(key string, def...interface{}) int {
return gconv.Int(s.Get(key, def...))
}
func (s *Session) GetInt16(key string) int16 {
return gconv.Int16(s.Get(key))
func (s *Session) GetInt8(key string, def...interface{}) int8 {
return gconv.Int8(s.Get(key, def...))
}
func (s *Session) GetInt32(key string) int32 {
return gconv.Int32(s.Get(key))
func (s *Session) GetInt16(key string, def...interface{}) int16 {
return gconv.Int16(s.Get(key, def...))
}
func (s *Session) GetInt64(key string) int64 {
return gconv.Int64(s.Get(key))
func (s *Session) GetInt32(key string, def...interface{}) int32 {
return gconv.Int32(s.Get(key, def...))
}
func (s *Session) GetUint(key string) uint {
return gconv.Uint(s.Get(key))
func (s *Session) GetInt64(key string, def...interface{}) int64 {
return gconv.Int64(s.Get(key, def...))
}
func (s *Session) GetUint8(key string) uint8 {
return gconv.Uint8(s.Get(key))
func (s *Session) GetUint(key string, def...interface{}) uint {
return gconv.Uint(s.Get(key, def...))
}
func (s *Session) GetUint16(key string) uint16 {
return gconv.Uint16(s.Get(key))
func (s *Session) GetUint8(key string, def...interface{}) uint8 {
return gconv.Uint8(s.Get(key, def...))
}
func (s *Session) GetUint32(key string) uint32 {
return gconv.Uint32(s.Get(key))
func (s *Session) GetUint16(key string, def...interface{}) uint16 {
return gconv.Uint16(s.Get(key, def...))
}
func (s *Session) GetUint64(key string) uint64 {
return gconv.Uint64(s.Get(key))
func (s *Session) GetUint32(key string, def...interface{}) uint32 {
return gconv.Uint32(s.Get(key, def...))
}
func (s *Session) GetFloat32(key string) float32 {
return gconv.Float32(s.Get(key))
func (s *Session) GetUint64(key string, def...interface{}) uint64 {
return gconv.Uint64(s.Get(key, def...))
}
func (s *Session) GetFloat64(key string) float64 {
return gconv.Float64(s.Get(key))
func (s *Session) GetFloat32(key string, def...interface{}) float32 {
return gconv.Float32(s.Get(key, def...))
}
func (s *Session) GetBytes(key string) []byte {
return gconv.Bytes(s.Get(key))
func (s *Session) GetFloat64(key string, def...interface{}) float64 {
return gconv.Float64(s.Get(key, def...))
}
func (s *Session) GetInts(key string) []int {
return gconv.Ints(s.Get(key))
func (s *Session) GetBytes(key string, def...interface{}) []byte {
return gconv.Bytes(s.Get(key, def...))
}
func (s *Session) GetFloats(key string) []float64 {
return gconv.Floats(s.Get(key))
func (s *Session) GetInts(key string, def...interface{}) []int {
return gconv.Ints(s.Get(key, def...))
}
func (s *Session) GetStrings(key string) []string {
return gconv.Strings(s.Get(key))
func (s *Session) GetFloats(key string, def...interface{}) []float64 {
return gconv.Floats(s.Get(key, def...))
}
func (s *Session) GetInterfaces(key string) []interface{} {
return gconv.Interfaces(s.Get(key))
func (s *Session) GetStrings(key string, def...interface{}) []string {
return gconv.Strings(s.Get(key, def...))
}
func (s *Session) GetInterfaces(key string, def...interface{}) []interface{} {
return gconv.Interfaces(s.Get(key, def...))
}
func (s *Session) GetTime(key string, format...string) time.Time {
@ -219,13 +224,13 @@ func (s *Session) GetGTime(key string, format...string) *gtime.Time {
return gconv.GTime(s.Get(key), format...)
}
func (s *Session) GetTimeDuration(key string) time.Duration {
return gconv.TimeDuration(s.Get(key))
func (s *Session) GetTimeDuration(key string, def...interface{}) time.Duration {
return gconv.TimeDuration(s.Get(key, def...))
}
// 将变量转换为对象,注意 objPointer 参数必须为struct指针
func (s *Session) GetStruct(key string, objPointer interface{}, attrMapping...map[string]string) error {
return gconv.Struct(s.Get(key), objPointer, attrMapping...)
// 将变量转换为对象,注意 pointer 参数必须为struct指针
func (s *Session) GetStruct(key string, pointer interface{}, mapping...map[string]string) error {
return gconv.Struct(s.Get(key), pointer, mapping...)
}