Skip to content

Commit

Permalink
Add pointer, change float to float32, add Get interface and change Ge…
Browse files Browse the repository at this point in the history
…t string.
  • Loading branch information
verzth committed Jan 14, 2022
1 parent 296cdf0 commit 67f895c
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 51 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ func SomeHandler(w http.ResponseWriter, r *http.Request) {
// Check whether 'name' exist and filled
}

name := req.Get("name") // Get name value as string
// Add Ptr to get the Pointer
someVr := req.Get("key") // Get key value as interface
name := req.GetString("name") // Get name value as string
id := req.GetUint64("id") // Get id value as uint64
id := req.GetUint32("id") // Get id value as uint32
id := req.GetUint("id") // Get id value as uint
id := req.GetInt64("id") // Get id value as int64
id := req.GetInt32("id") // Get id value as int32
id := req.GetInt("id") // Get id value as int
price := req.GetFloat64("price") // Get price value as float64
price := req.GetFloat("price") // Get price value as float32
price := req.GetFloat32("price") // Get price value as float32
status := req.GetBool("active") // Get active value as bool
birthdate, err := req.GetTime("birthdate") // Get birthdate value as *time.Time with Error handler
birthdate := req.GetTimeNE("birthdate") // Get birthdate value as *time.Time with No Error
Expand Down
4 changes: 2 additions & 2 deletions demo/Demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func index(w http.ResponseWriter, r *http.Request) {
fmt.Println(vn.(map[string]interface{})["id"].([]interface{})[0])*/

if req.Filled("test"){
fmt.Println(req.Get("test"))
fmt.Println(req.GetString("test"))
}else if req.Has("test"){
fmt.Println("Detected")
}else{
Expand All @@ -47,5 +47,5 @@ func index(w http.ResponseWriter, r *http.Request) {
fmt.Println("Not detected")
}

res.ReplySuccess("0000000", "SSSSSS", "Success")
_ = res.ReplySuccess("0000000", "SSSSSS", "Success")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module git.teknoku.digital/teknoku/jumper
go 1.13

require (
git.teknoku.digital/teknoku/go-utils v0.3.0 // indirect
git.teknoku.digital/teknoku/go-utils v0.3.0
github.com/gorilla/handlers v1.4.2
github.com/gorilla/mux v1.7.4
)
208 changes: 165 additions & 43 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func PlugRequest(r *http.Request, w http.ResponseWriter) *Request {
return req
}

// TouchRequest touch request with rewrite to reader, so handler can reuse the reader.
func TouchRequest(r *http.Request, w http.ResponseWriter) *Request {
req := &Request{
r: *r,
Expand Down Expand Up @@ -262,18 +263,6 @@ func (r *Request) Header(key string) string {
return r.header.Get(key)
}

func (r *Request) GetAll() map[string] interface{} {
return r.params
}

func (r *Request) Get(key string) string {
val := reflect.ValueOf(r.params[key])
if r.params[key] != nil || (val.IsValid() && val.Kind() == reflect.Slice && val.Len() > 0){
return fmt.Sprintf("%v", r.params[key])
}
return ""
}

func (r *Request) Append(key string, val string) {
r.params[key] = val
}
Expand Down Expand Up @@ -353,98 +342,230 @@ func (r *Request) GetFiles(key string) ([]*File, error) {
return nil, errors.New("no such file")
}

func (r *Request) GetUint64(key string) uint64 {
func (r *Request) GetAll() map[string] interface{} {
return r.params
}


func (r *Request) GetPtr(key string) *interface{} {
val := reflect.ValueOf(r.params[key])
if r.params[key] != nil || (val.IsValid() && val.Kind() == reflect.Interface){
v := r.params[key]
return &v
}
return nil
}

func (r *Request) Get(key string) interface{} {
v := r.GetPtr(key)
if v!=nil {
return v
}else{
return nil
}
}

func (r *Request) GetStringPtr(key string) *string {
val := reflect.ValueOf(r.params[key])
if r.params[key] != nil || (val.IsValid() && val.Kind() == reflect.Slice && val.Len() > 0){
v := fmt.Sprintf("%v", r.params[key])
return &v
}
return nil
}

func (r *Request) GetString(key string) string {
v := r.GetStringPtr(key)
if v!=nil {
return *v
}else{
return ""
}
}

func (r *Request) GetUint64Ptr(key string) *uint64 {
if r.params[key] != nil {
var v uint64
switch r.params[key].(type) {
case float64: return uint64(r.params[key].(float64))
case int: return uint64(r.params[key].(int))
case float64: v = uint64(r.params[key].(float64))
case int: v = uint64(r.params[key].(int))
case string:
i64, _ := strconv.ParseUint(r.params[key].(string), 10, 32)
return i64
v, _ = strconv.ParseUint(r.params[key].(string), 10, 32)
case bool: {
if r.params[key].(bool) {
return 1
v = 1
}else{
return 0
v = 0
}
}
}
return &v
}
return nil
}

func (r *Request) GetUint64(key string) uint64 {
v := r.GetUint64Ptr(key)
if v != nil {
return *v
}else{
return 0
}
}

func (r *Request) GetUint32Ptr(key string) *uint32 {
v := r.GetUint64Ptr(key)
if v != nil {
val := uint32(*v)
return &val
} else {
return nil
}
return 0
}

func (r *Request) GetUint32(key string) uint32 {
return uint32(r.GetUint64(key))
}

func (r *Request) GetUintPtr(key string) *uint {
v := r.GetUint64Ptr(key)
if v != nil {
val := uint(*v)
return &val
} else {
return nil
}
}

func (r *Request) GetUint(key string) uint {
return uint(r.GetUint64(key))
}

func (r *Request) GetInt64(key string) int64 {
func (r *Request) GetInt64Ptr(key string) *int64 {
if r.params[key] != nil {
var v int64
switch r.params[key].(type) {
case float64: return int64(r.params[key].(float64))
case int: return int64(r.params[key].(int))
case float64: v = int64(r.params[key].(float64))
case int: v = int64(r.params[key].(int))
case string:
i64, _ := strconv.ParseInt(r.params[key].(string), 10, 32)
return i64
v, _ = strconv.ParseInt(r.params[key].(string), 10, 32)
case bool: {
if r.params[key].(bool) {
return 1
v = 1
}else{
return 0
v = 0
}
}
}
return &v
}
return nil
}

func (r *Request) GetInt64(key string) int64 {
v := r.GetInt64Ptr(key)
if v != nil {
return *v
}else{
return 0
}
}

func (r *Request) GetInt32Ptr(key string) *int32 {
v := r.GetUint64Ptr(key)
if v != nil {
val := int32(*v)
return &val
} else {
return nil
}
return 0
}

func (r *Request) GetInt32(key string) int32 {
return int32(r.GetInt64(key))
}

func (r *Request) GetIntPtr(key string) *int {
v := r.GetUint64Ptr(key)
if v != nil {
val := int(*v)
return &val
} else {
return nil
}
}

func (r *Request) GetInt(key string) int {
return int(r.GetInt64(key))
}

func (r *Request) GetFloat64(key string) float64 {
func (r *Request) GetFloat64Ptr(key string) *float64 {
if r.params[key] != nil {
var v float64
switch r.params[key].(type) {
case float64: return r.params[key].(float64)
case int: return float64(r.params[key].(int))
case float64: v = r.params[key].(float64)
case int: v = float64(r.params[key].(int))
case string:
i64, _ := strconv.ParseFloat(r.params[key].(string), 10)
return i64
v, _ = strconv.ParseFloat(r.params[key].(string), 10)
case bool: {
if r.params[key].(bool) {
return 1
v = 1
}else{
return 0
v = 0
}
}
}
return &v
}
return nil
}

func (r *Request) GetFloat64(key string) float64 {
v := r.GetFloat64Ptr(key)
if v != nil {
return *v
}else{
return 0
}
}

func (r *Request) GetFloat32Ptr(key string) *float32 {
v := r.GetFloat64Ptr(key)
if v != nil {
val := float32(*v)
return &val
} else {
return nil
}
return 0
}

func (r *Request) GetFloat(key string) float32 {
return float32(r.GetFloat64(key))
}

func (r *Request) GetBool(key string) bool {
func (r *Request) GetBoolPtr(key string) *bool {
if r.params[key] != nil {
var v bool
switch r.params[key].(type) {
case float64: return r.params[key].(float64) > 0
case int: return float64(r.params[key].(int)) > 0
case float64: v = r.params[key].(float64) > 0
case int: v = float64(r.params[key].(int)) > 0
case string:
i64, _ := strconv.ParseFloat(r.params[key].(string), 10)
return i64 > 0
case bool: return r.params[key].(bool)
v = i64 > 0
case bool: v = r.params[key].(bool)
}
return &v
}
return nil
}

func (r *Request) GetBool(key string) bool {
v := r.GetBoolPtr(key)
if v != nil {
return *v
}else{
return false
}
return false
}

func (r *Request) GetTime(key string) (*time.Time,error) {
Expand Down Expand Up @@ -534,8 +655,9 @@ func (r *Request) Filled(keys... string) (found bool) {
val := reflect.ValueOf(r.params[key])
if val.IsValid() {
switch val.Kind() {
case reflect.String: found = found && strings.TrimSpace(r.Get(key)) != ""
case reflect.String: found = found && strings.TrimSpace(r.GetString(key)) != ""
case reflect.Slice: found = found && val.Len() > 0
case reflect.Array: found = found && val.Len() > 0
}
}else{
found = false
Expand Down
6 changes: 3 additions & 3 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ func (r *Response) SetHttpCode(code int) *Response {
return r
}

/*
'data' arguments only used on index 0
*/
// Reply 'data' arguments only used on index 0 */
func (r *Response) Reply(status int, number string, code string, message string, data... interface{}) error {
r.w.Header().Set("Content-Type", "application/json")

Expand All @@ -48,10 +46,12 @@ func (r *Response) Reply(status int, number string, code string, message string,
return json.NewEncoder(r.w).Encode(r)
}

// ReplyFailed 'data' arguments only used on index 0 */
func (r *Response) ReplyFailed(number string, code string, message string, data... interface{}) error {
return r.Reply(0, number, code, message, data...)
}

// ReplySuccess 'data' arguments only used on index 0 */
func (r *Response) ReplySuccess(number string, code string, message string, data... interface{}) error {
return r.Reply(1, number, code, message, data...)
}

0 comments on commit 67f895c

Please sign in to comment.