如何在Go Gin中修剪查询和JSON的空白空间?

huangapple go评论85阅读模式
英文:

How to trim white space for query and json in go gin?

问题

我有一个类似这样的结构体:

type Data struct {
	Foo string `json:"foo" binding:"required"` 
}

我使用ShouldBind将查询或JSON体绑定到该结构体:

data := Data{}
err := ctx.ShouldBind(&data)

我想知道如何最佳实践地去除字符串字段的空格?

{"foo": " bar "}转换为{"foo": "bar"}

  • 我尝试使用自定义的字符串类型,并添加自定义的UnmarshalJSON函数,但如果是查询的话,它对ctx.ShouldBind无效。
type Data struct {
	Foo TrimSpaceString `json:"foo" binding:"required"` 
}

type TrimSpaceString string

func (t *TrimSpaceString) UnmarshalJSON(data []byte) error {
	data = bytes.Trim(data, "\"")
	data = bytes.Trim(data, " ")
	*t = TrimSpaceString(strings.TrimSpace(string(data)))
	return nil
}
  • 我还尝试使用conform并为结构体添加标签。但是我必须在绑定后添加conform.Strings(&data),这不太方便。
type Data struct {
    Foo TrimSpaceString `json:"foo" binding:"required" conform:"trim"` 
}
err := ctx.ShouldBind(&data)
conform.Strings(&data)
  • 我应该自定义一个Binding并在Binding内部去除字符串的空格吗?
英文:

I have a struct like this

type Data struct {
	Foo string `json:"foo" binding:"required"` 
}

And I use ShouldBind to bind query or json body to the struct.

data := Data{}
err := ctx.ShouldBind(&data)

I was wondering what is the best practice to trim white space for the string field?

transform {"foo": "   bar   "} to struct {"foo": "bar"}
  • I have tried using custom string type, and add custom UnmarshalJSON function, but it won't work for ctx.shouldBind if it is query.
type Data struct {
	Foo TrimSpaceString `json:"foo" binding:"required"` 
}

type TrimSpaceString string

func (t *TrimSpaceString) UnmarshalJSON(data []byte) error {
	data = bytes.Trim(data, "\"")
	data = bytes.Trim(data, " ")
	*t = TrimSpaceString(strings.TrimSpace(string(data)))
	return nil
}
  • I also tried to use conform and add tag for struct. But I have to add conform.Strings(data) after bind it and it is not convinence.
type Data struct {
    Foo TrimSpaceString `json:"foo" binding:"required" conform:"trim"` 
}
err := ctx.ShouldBind(&data)
conform.Strings(&data)
  • Should I custom a Binding and trim string inside Binding?

答案1

得分: 1

我通过修改UnmarshalJSON函数使其正常工作。我没有什么可以解释的,因为我也是新手,但我会尝试解释一下。不同的是,我们通过json.Unmarshal将字节data转换为字符串。然后,我们修剪字符串上的空格,并将其附加到字段或原始字符串上。

type trim string

func (t *trim) UnmarshalJSON(data []byte) error {
    var s string
    if err := json.Unmarshal(data, &s); err != nil {
        return err
    }
    *t = trim(strings.TrimSpace(s))
    fmt.Printf("这些是字符串:%s\n", *t)
    return nil
}

如果你想自己尝试,这是playground链接:
https://go.dev/play/p/BuxvMQibVsn

英文:

I got this working by modifying UnmarshalJSON function a bit. I don't have anything to explain as I'm also new, but will try, what's different is that we are converting byte data into a string by json.Unmarshal.Then we trim the space on string and appends it to the field or the original string

type trim string

func (t *trim) UnmarshalJSON(data []byte) error {
	var s string
	if err := json.Unmarshal(data, &s); err != nil {
		return err
	}
	*t = trim(strings.TrimSpace(s))
	fmt.Printf("these are the strings: '%s'\n", *t)
	return nil
}

Here is the playground if you want to try it yourself
https://go.dev/play/p/BuxvMQibVsn

huangapple
  • 本文由 发表于 2021年12月30日 20:17:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/70531574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定