package base import ( "reflect" "strings" "unicode" "git.fsdpf.net/go/contracts/res_type" "git.fsdpf.net/go/db" "git.fsdpf.net/go/req" ) type QueryField struct { ResField typ req.RouteParamType `db:"dataType"` alias string `db:"alias"` isExpr bool `db:"isExpr"` isOmitempty bool ignored bool } func (this QueryField) Alias() string { return this.alias } func (this QueryField) Ignored() bool { return this.ignored } func (this QueryField) IsExpr() bool { return this.isExpr } func (this QueryField) IsOmitempty() bool { return this.isOmitempty } func (this QueryField) Type() req.RouteParamType { return this.typ } func (this *QueryField) SetOptions(options byte) req.QueryField { if options&req.IsExpr != 0 { this.isExpr = true } if options&req.IsOmitempty != 0 { this.isOmitempty = true } if options&req.Ignored != 0 { this.ignored = true } return this } func (this QueryField) GetCodeOrAlias() string { if this.Alias() != "" { return this.Alias() } return this.Code } func (this *QueryField) ToSql() db.Expression { if this.Alias() != "" { if this.IsExpr() { if this.GetCode() == "" { return db.Raw("null as `" + this.Alias() + "`") } return db.Raw("(" + this.GetCode() + ") as `" + this.Alias() + "`") } return db.Raw("`" + this.GetCodeResource() + "`.`" + this.GetCode() + "` as `" + this.Alias() + "`") } return db.Raw("`" + this.GetCodeResource() + "`.`" + this.GetCode() + "`") } func (this *QueryField) ToStructField(tags ...string) reflect.StructField { var typ reflect.Type fCode := this.GetCodeOrAlias() // 字段规则 if !unicode.IsLetter(rune(fCode[0])) { panic("struct field name invalid. " + fCode) } fTag := `db:"` + fCode if this.ignored { fTag = fTag + `" json:"-"` } else if this.isOmitempty { fTag = fTag + `" json:"` + fCode + `,omitempty"` } else { fTag = fTag + `" json:"` + fCode + `"` } if len(tags) > 0 { fTag = strings.Join(tags, " ") + " " + fTag } switch this.typ { case req.ReqString: typ = reflect.TypeOf(string("")) case req.ReqNumber: typ = reflect.TypeOf(res_type.ResFieldByNumber(0)) case req.ReqInteger: typ = reflect.TypeOf(res_type.ResFieldByInteger(0)) case req.ReqFloat: typ = reflect.TypeOf(res_type.ResFieldByFloat(0)) case req.ReqBool: typ = reflect.TypeOf(true) case req.ReqArray: typ = reflect.TypeOf([]any{}) case req.ReqJson: typ = reflect.TypeOf(map[string]any{}) } return reflect.StructField{ Name: strings.ToUpper(fCode[:1]) + fCode[1:], Tag: reflect.StructTag(fTag), Type: typ, } }