Iris: [QUESTION] Add a default value function to the url tag of query or post?

Created on 18 Jan 2021  ·  3Comments  ·  Source: kataras/iris

mvc code

func (tb *VoiceRecord) GetRecords(c iris.Context, stru struct {
    CallID     string `url:"phone"`    // 手机号
    ComDir     int    `url:"dir"`      // 方向 | 0.全部 1.呼入 2.呼出
    CaseUserID string `url:"on"`       // 座席(用户名)
    StartYear  int    `url:"sy"`       // 开始年份
    EndYear    int    `url:"ey"`       // 结束年份
    OrderBy    string `url:"order_by;default:1"` // 排序值,可以是字符串或整型
    LastId     int    `url:"lid"`      // 上一次返回的数据中,最后一条记录的ID
}) string {
    // Add a default value for url tag,
    // When the request does not pass the order_by parameter, the stru.OrderBy value is 1。
    fmt.Println(stru.OrderBy) // 1
    return ""
}

I can't find any reference for this feature, and this feature should be useful, it might be better if it can be integrated into iris。

question

Most helpful comment

No worries @hjzCy you are fine and definitely not "stupid"! Iris is a strong framework, give it time, read its examples search for github issues and e.t.c and you will learn a lot. Don't hesitate to open a new post when you feel it, we are here to provide you with more information or fix an issue of yours.

All 3 comments

Hello @hjzCy, the url struct binder does not support default values like that and for a good reason, this layer is for validation. We have a feature for these cases as well, it's nice that you are trying to push new ideas into Iris for the past weeks but Iris have you covered 🤗

When you need that type of logic behind a request input, e.g. set default values, the right way to do that is to register a request-scoped dependency for that type. We have the Context.ReadQuery(pointer) which you can use to bind a struct value, that value can have default values. Here is how you could do that:

package main

import (
    "fmt"

    "github.com/kataras/iris/v12"
    "github.com/kataras/iris/v12/mvc"
)

func main() {
    app := iris.New()

    mvcApp := mvc.New(app)
    {
        mvcApp.Register(paramsDependency)

        mvcApp.Handle(new(controller))
    }

    app.Listen(":8080")
}

type params struct {
    CallID     string `url:"phone"`
    ComDir     int    `url:"dir"`
    CaseUserID string `url:"on"`
    StartYear  int    `url:"sy"`
    EndYear    int    `url:"ey"`
    OrderBy    string `url:"order_by"`
    Offset     int    `url:"offset"`
}

// As we've read in the previous examples, the paramsDependency
// describes a request-scoped dependency.
// It should accept the iris context (or any previously registered or builtin dependency)
// and it should return the value which will be binded to the
// controller's methods (or fields) - see `GetRecords`.
var paramsDependency = func(ctx iris.Context) params {
    p := params{
        OrderBy: "ASC", // default value.
    }
    // Bind the URL values to the "p":
    ctx.ReadQuery(&p)
    // Or bind a specific URL value by giving a default value:
    // p.OrderBy = ctx.URLParamDefault("order_by", "ASC")
    //
    // OR make checks for default values after ReadXXX,
    // e.g. if p.OrderBy == "" {...}

    /* More options to read a request:
    // Bind the JSON request body to the "p":
    ctx.ReadJSON(&p)
    // Bind the Form to the "p":
    ctx.ReadForm(&p)
    // Bind any, based on the client's content-type header:
    ctx.ReadBody(&p)
    // Bind the http requests to a struct value:
    ctx.ReadHeader(&h)
    */

    return p
}

type controller struct{}

func (c *controller) GetRecords(stru params) string {
    return fmt.Sprintf("%#+v\n", stru)
}

http://localhost:8080/records?phone=random&order_by=DESC
# main.params{CallID:"random", ComDir:0, CaseUserID:"", StartYear:0, EndYear:0, OrderBy:"DESC", Offset:0}
http://localhost:8080/records?phone=random
# main.params{CallID:"random", ComDir:0, CaseUserID:"", StartYear:0, EndYear:0, OrderBy:"ASC", Offset:0}
http://localhost:8080/records?phone=random&order_by=
# main.params{CallID:"random", ComDir:0, CaseUserID:"", StartYear:0, EndYear:0, OrderBy:"ASC", Offset:0}

Okay, so that's it, sorry, I'm too stupid。
Thank you for your patience。
I like iris very much and hope it gets stronger!

No worries @hjzCy you are fine and definitely not "stupid"! Iris is a strong framework, give it time, read its examples search for github issues and e.t.c and you will learn a lot. Don't hesitate to open a new post when you feel it, we are here to provide you with more information or fix an issue of yours.

Was this page helpful?
0 / 5 - 0 ratings