Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于IDL中optional与默认值的使用 是我姿势不对? #1008

Closed
kongmHUB opened this issue Nov 27, 2023 · 1 comment
Closed

关于IDL中optional与默认值的使用 是我姿势不对? #1008

kongmHUB opened this issue Nov 27, 2023 · 1 comment
Assignees
Labels
question Further information is requested

Comments

@kongmHUB
Copy link

Describe the Question

如下,可选参数加入校验逻辑后,如果req中没有sampleid的输入,
校验逻辑会报错,是我使用的姿势不对?

Reproducible Code

idl/sample.thrift

namespace go v1.sample

struct SampleReq {
    1: optional string sampleId (api.body="sampleid",api.vd="len($) > 0; msg:sprintf('invalid d: %v',$)") ; // 添加 api 注解为方便进行参数绑定
}

struct SampleResp {
    1: string RespBody;
}

service SampleService {
    SampleResp SampleMethod(1: SampleReq request) (api.post="/v1/sample");
}

Expected behavior

期望的处理流程应该是sampleid无输入时能skip掉校验。

Screenshots

snapshot

Hertz version:

github.com/cloudwego/hertz v0.7.1

Environment:

GO111MODULE="on"
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/xxx/Library/Caches/go-build"
GOENV="/Users/xxx/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/xxx/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/xxx/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/opt/homebrew/opt/go/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/opt/go/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.20.5"
GCCGO="gccgo"
AR="ar"
CC="cc"
CXX="c++"
CGO_ENABLED="1"
GOMOD="/Users/xxx/go/src/test/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/ss/mfhwtj4d2zd2v_d8m__nx1700000gn/T/go-build625561015=/tmp/go-build -gno-record-gcc-switches -fno-common"

Additional context

上面这个问题还可以在vd中加入“api.vd="$ == nil ||” 克服一下

可选+默认值,好像就没办法了。
按下面代码设置默认值后

# idl/sample.thrift
namespace go v1.sample

struct SampleReq {
    1: optional string sampleId = "test" (api.body="sampleid",api.vd="$ == nil || len($) > 0; msg:sprintf('invalid d: %v',$)") ; // 添加 api 注解为方便进行参数绑定
}

struct SampleResp {
    1: string RespBody;
}

service SampleService {
    SampleResp SampleMethod(1: SampleReq request) (api.post="/v1/sample");
}

生成的sample.go(部分)如下

// Code generated by thriftgo (0.3.2). DO NOT EDIT.

package sample

import (
	"context"
	"fmt"
	"github.com/apache/thrift/lib/go/thrift"
)

type SampleReq struct {
	// 添加 api 注解为方便进行参数绑定
	SampleId string `thrift:"sampleId,1,optional" form:"sampleid" json:"sampleid,omitempty" vd:"$ == nil || len($) > 0; msg:sprintf('invalid d: %v',$)"`
}

func NewSampleReq() *SampleReq {
	return &SampleReq{

		SampleId: "test",
	}
}

var SampleReq_SampleId_DEFAULT string = "test"

func (p *SampleReq) GetSampleId() (v string) {
	if !p.IsSetSampleId() {
		return SampleReq_SampleId_DEFAULT
	}
	return p.SampleId
}

var fieldIDToName_SampleReq = map[int16]string{
	1: "sampleId",
}

func (p *SampleReq) IsSetSampleId() bool {
	return p.SampleId != SampleReq_SampleId_DEFAULT
}
......

看上去像是判断这个参数是否有用户输入,
参数没有设置时,req.GetSampleId == nil,IsSetSampleId()是true

@li-jin-gou
Copy link
Member

vd 支持 or 操作 https://github.com/bytedance/go-tagexpr/tree/master/validator optional没作用 cc @FGYFFFF

@li-jin-gou li-jin-gou added the question Further information is requested label Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

No branches or pull requests

3 participants