github.com/aaabigfish/gopkg@v1.1.0/ginx/binding.go (about)

     1  package ginx
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/gin-gonic/gin"
     7  )
     8  
     9  func Query(c *gin.Context, key string) string {
    10  	if val := c.Param(key); val != "" {
    11  		return val
    12  	}
    13  
    14  	return c.Query(key)
    15  }
    16  
    17  // GetPage returns paging parameter.
    18  func GetPage(c *gin.Context) int {
    19  	ret, _ := strconv.Atoi(c.Query("page"))
    20  	if 1 > ret {
    21  		ret = 1
    22  	}
    23  
    24  	return ret
    25  }
    26  
    27  // GetPage returns paging parameter.
    28  func GetPageSize(c *gin.Context) int {
    29  	ret, _ := strconv.Atoi(c.Query("size"))
    30  	if 1 > ret {
    31  		ret = 10
    32  	}
    33  
    34  	return ret
    35  }
    36  
    37  // GetString returns the input value by key string or the default value while it's present and input is blank
    38  func GetString(c *gin.Context, key string, def ...string) string {
    39  	if v := Query(c, key); v != "" {
    40  		return v
    41  	}
    42  	if len(def) > 0 {
    43  		return def[0]
    44  	}
    45  	return ""
    46  }
    47  
    48  // GetInt returns input as an int or the default value while it's present and input is blank
    49  func GetInt(c *gin.Context, key string, def ...int) (i int) {
    50  	strv := Query(c, key)
    51  	if len(strv) == 0 && len(def) > 0 {
    52  		i = def[0]
    53  		return
    54  	}
    55  	i, _ = strconv.Atoi(strv)
    56  	return
    57  }
    58  
    59  // GetInt8 return input as an int8 or the default value while it's present and input is blank
    60  func GetInt8(c *gin.Context, key string, def ...int8) (i8 int8) {
    61  	strv := Query(c, key)
    62  	if len(strv) == 0 && len(def) > 0 {
    63  		i8 = def[0]
    64  		return
    65  	}
    66  	i64, _ := strconv.ParseInt(strv, 10, 8)
    67  	i8 = int8(i64)
    68  	return
    69  }
    70  
    71  // GetUint8 return input as an uint8 or the default value while it's present and input is blank
    72  func GetUint8(c *gin.Context, key string, def ...uint8) (u8 uint8) {
    73  	strv := Query(c, key)
    74  	if len(strv) == 0 && len(def) > 0 {
    75  		u8 = def[0]
    76  		return
    77  	}
    78  	u64, _ := strconv.ParseUint(strv, 10, 8)
    79  	u8 = uint8(u64)
    80  	return
    81  }
    82  
    83  // GetInt16 returns input as an int16 or the default value while it's present and input is blank
    84  func GetInt16(c *gin.Context, key string, def ...int16) (i16 int16) {
    85  	strv := Query(c, key)
    86  	if len(strv) == 0 && len(def) > 0 {
    87  		i16 = def[0]
    88  		return
    89  	}
    90  	i64, _ := strconv.ParseInt(strv, 10, 16)
    91  	i16 = int16(i64)
    92  	return
    93  }
    94  
    95  // GetUint16 returns input as an uint16 or the default value while it's present and input is blank
    96  func GetUint16(c *gin.Context, key string, def ...uint16) (u16 uint16) {
    97  	strv := Query(c, key)
    98  	if len(strv) == 0 && len(def) > 0 {
    99  		u16 = def[0]
   100  		return
   101  	}
   102  	u64, _ := strconv.ParseUint(strv, 10, 16)
   103  	u16 = uint16(u64)
   104  	return
   105  }
   106  
   107  // GetInt32 returns input as an int32 or the default value while it's present and input is blank
   108  func GetInt32(c *gin.Context, key string, def ...int32) (i32 int32) {
   109  	strv := Query(c, key)
   110  	if len(strv) == 0 && len(def) > 0 {
   111  		i32 = def[0]
   112  		return
   113  	}
   114  	i64, _ := strconv.ParseInt(strv, 10, 32)
   115  	i32 = int32(i64)
   116  	return
   117  }
   118  
   119  // GetUint32 returns input as an uint32 or the default value while it's present and input is blank
   120  func GetUint32(c *gin.Context, key string, def ...uint32) (u32 uint32) {
   121  	strv := Query(c, key)
   122  	if len(strv) == 0 && len(def) > 0 {
   123  		u32 = def[0]
   124  		return
   125  	}
   126  	u64, _ := strconv.ParseUint(strv, 10, 32)
   127  	u32 = uint32(u64)
   128  	return
   129  }
   130  
   131  // GetInt64 returns input value as int64 or the default value while it's present and input is blank.
   132  func GetInt64(c *gin.Context, key string, def ...int64) (i64 int64) {
   133  	strv := Query(c, key)
   134  	if len(strv) == 0 && len(def) > 0 {
   135  		i64 = def[0]
   136  		return
   137  	}
   138  	i64, _ = strconv.ParseInt(strv, 10, 64)
   139  	return
   140  }
   141  
   142  // GetUint64 returns input value as uint64 or the default value while it's present and input is blank.
   143  func GetUint64(c *gin.Context, key string, def ...uint64) (u64 uint64) {
   144  	strv := Query(c, key)
   145  	if len(strv) == 0 && len(def) > 0 {
   146  		u64 = def[0]
   147  		return
   148  	}
   149  	u64, _ = strconv.ParseUint(strv, 10, 64)
   150  	return
   151  }
   152  
   153  // GetBool returns input value as bool or the default value while it's present and input is blank.
   154  func GetBool(c *gin.Context, key string, def ...bool) (b bool) {
   155  	strv := Query(c, key)
   156  	if len(strv) == 0 && len(def) > 0 {
   157  		b = def[0]
   158  		return
   159  	}
   160  	b, _ = strconv.ParseBool(strv)
   161  	return
   162  }
   163  
   164  // GetFloat returns input value as float64 or the default value while it's present and input is blank.
   165  func GetFloat(c *gin.Context, key string, def ...float64) (f64 float64) {
   166  	strv := Query(c, key)
   167  	if len(strv) == 0 && len(def) > 0 {
   168  		f64 = def[0]
   169  		return
   170  	}
   171  
   172  	f64, _ = strconv.ParseFloat(strv, 64)
   173  	return
   174  }
   175  
   176  func GetIntE(c *gin.Context, key string, def ...int) (int, error) {
   177  	strv := Query(c, key)
   178  	if len(strv) == 0 && len(def) > 0 {
   179  		return def[0], nil
   180  	}
   181  	return strconv.Atoi(strv)
   182  }
   183  
   184  func GetInt8E(c *gin.Context, key string, def ...int8) (int8, error) {
   185  	strv := Query(c, key)
   186  	if len(strv) == 0 && len(def) > 0 {
   187  		return def[0], nil
   188  	}
   189  	i64, err := strconv.ParseInt(strv, 10, 8)
   190  	return int8(i64), err
   191  }
   192  
   193  func GetUint8E(c *gin.Context, key string, def ...uint8) (uint8, error) {
   194  	strv := Query(c, key)
   195  	if len(strv) == 0 && len(def) > 0 {
   196  		return def[0], nil
   197  	}
   198  	u64, err := strconv.ParseUint(strv, 10, 8)
   199  	return uint8(u64), err
   200  }
   201  
   202  func GetInt16E(c *gin.Context, key string, def ...int16) (int16, error) {
   203  	strv := Query(c, key)
   204  	if len(strv) == 0 && len(def) > 0 {
   205  		return def[0], nil
   206  	}
   207  	i64, err := strconv.ParseInt(strv, 10, 16)
   208  	return int16(i64), err
   209  }
   210  
   211  func GetUint16E(c *gin.Context, key string, def ...uint16) (uint16, error) {
   212  	strv := Query(c, key)
   213  	if len(strv) == 0 && len(def) > 0 {
   214  		return def[0], nil
   215  	}
   216  	u64, err := strconv.ParseUint(strv, 10, 16)
   217  	return uint16(u64), err
   218  }
   219  
   220  func GetInt32E(c *gin.Context, key string, def ...int32) (int32, error) {
   221  	strv := Query(c, key)
   222  	if len(strv) == 0 && len(def) > 0 {
   223  		return def[0], nil
   224  	}
   225  	i64, err := strconv.ParseInt(strv, 10, 32)
   226  	return int32(i64), err
   227  }
   228  
   229  func GetUint32E(c *gin.Context, key string, def ...uint32) (uint32, error) {
   230  	strv := Query(c, key)
   231  	if len(strv) == 0 && len(def) > 0 {
   232  		return def[0], nil
   233  	}
   234  	u64, err := strconv.ParseUint(strv, 10, 32)
   235  	return uint32(u64), err
   236  }
   237  
   238  func GetInt64E(c *gin.Context, key string, def ...int64) (int64, error) {
   239  	strv := Query(c, key)
   240  	if len(strv) == 0 && len(def) > 0 {
   241  		return def[0], nil
   242  	}
   243  	return strconv.ParseInt(strv, 10, 64)
   244  }
   245  
   246  func GetUint64E(c *gin.Context, key string, def ...uint64) (uint64, error) {
   247  	strv := Query(c, key)
   248  	if len(strv) == 0 && len(def) > 0 {
   249  		return def[0], nil
   250  	}
   251  	return strconv.ParseUint(strv, 10, 64)
   252  }
   253  
   254  func GetBoolE(c *gin.Context, key string, def ...bool) (bool, error) {
   255  	strv := Query(c, key)
   256  	if len(strv) == 0 && len(def) > 0 {
   257  		return def[0], nil
   258  	}
   259  	return strconv.ParseBool(strv)
   260  }
   261  
   262  func GetFloatE(c *gin.Context, key string, def ...float64) (float64, error) {
   263  	strv := Query(c, key)
   264  	if len(strv) == 0 && len(def) > 0 {
   265  		return def[0], nil
   266  	}
   267  	return strconv.ParseFloat(strv, 64)
   268  }