forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix zeromicro#1205 * move builder into stores * remove xrom * Remove unused code * Remove unused code * refactor builderx to builder Co-authored-by: anqiansong <anqiansong@bytedance.com>
- Loading branch information
Showing
9 changed files
with
101 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package builder | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
const dbTag = "db" | ||
|
||
// RawFieldNames converts golang struct field into slice string. | ||
func RawFieldNames(in interface{}, postgresSql ...bool) []string { | ||
out := make([]string, 0) | ||
v := reflect.ValueOf(in) | ||
if v.Kind() == reflect.Ptr { | ||
v = v.Elem() | ||
} | ||
|
||
var pg bool | ||
if len(postgresSql) > 0 { | ||
pg = postgresSql[0] | ||
} | ||
|
||
// we only accept structs | ||
if v.Kind() != reflect.Struct { | ||
panic(fmt.Errorf("ToMap only accepts structs; got %T", v)) | ||
} | ||
|
||
typ := v.Type() | ||
for i := 0; i < v.NumField(); i++ { | ||
// gets us a StructField | ||
fi := typ.Field(i) | ||
if tagv := fi.Tag.Get(dbTag); tagv != "" { | ||
if pg { | ||
out = append(out, tagv) | ||
} else { | ||
out = append(out, fmt.Sprintf("`%s`", tagv)) | ||
} | ||
} else { | ||
if pg { | ||
out = append(out, fi.Name) | ||
} else { | ||
out = append(out, fmt.Sprintf("`%s`", fi.Name)) | ||
} | ||
} | ||
} | ||
|
||
return out | ||
} | ||
|
||
// PostgreSqlJoin concatenates the given elements into a string. | ||
func PostgreSqlJoin(elems []string) string { | ||
b := new(strings.Builder) | ||
for index, e := range elems { | ||
b.WriteString(fmt.Sprintf("%s = $%d, ", e, index+2)) | ||
} | ||
|
||
if b.Len() == 0 { | ||
return b.String() | ||
} | ||
|
||
return b.String()[0 : b.Len()-2] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package builder | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type mockedUser struct { | ||
ID string `db:"id" json:"id,omitempty"` | ||
UserName string `db:"user_name" json:"userName,omitempty"` | ||
Sex int `db:"sex" json:"sex,omitempty"` | ||
UUID string `db:"uuid" uuid:"uuid,omitempty"` | ||
Age int `db:"age" json:"age"` | ||
} | ||
|
||
func TestFieldNames(t *testing.T) { | ||
t.Run("new", func(t *testing.T) { | ||
var u mockedUser | ||
out := RawFieldNames(&u) | ||
expected := []string{"`id`", "`user_name`", "`sex`", "`uuid`", "`age`"} | ||
assert.Equal(t, expected, out) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,136 +1,20 @@ | ||
package builderx | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/go-xorm/builder" | ||
"github.com/tal-tech/go-zero/core/stores/builder" | ||
) | ||
|
||
const dbTag = "db" | ||
|
||
// NewEq wraps builder.Eq. | ||
func NewEq(in interface{}) builder.Eq { | ||
return builder.Eq(ToMap(in)) | ||
} | ||
|
||
// NewGt wraps builder.Gt. | ||
func NewGt(in interface{}) builder.Gt { | ||
return builder.Gt(ToMap(in)) | ||
} | ||
|
||
// ToMap converts interface into map. | ||
func ToMap(in interface{}) map[string]interface{} { | ||
out := make(map[string]interface{}) | ||
v := reflect.ValueOf(in) | ||
if v.Kind() == reflect.Ptr { | ||
v = v.Elem() | ||
} | ||
|
||
// we only accept structs | ||
if v.Kind() != reflect.Struct { | ||
panic(fmt.Errorf("ToMap only accepts structs; got %T", v)) | ||
} | ||
|
||
typ := v.Type() | ||
for i := 0; i < v.NumField(); i++ { | ||
// gets us a StructField | ||
fi := typ.Field(i) | ||
if tagv := fi.Tag.Get(dbTag); tagv != "" { | ||
// set key of map to value in struct field | ||
val := v.Field(i) | ||
zero := reflect.Zero(val.Type()).Interface() | ||
current := val.Interface() | ||
|
||
if reflect.DeepEqual(current, zero) { | ||
continue | ||
} | ||
out[tagv] = current | ||
} | ||
} | ||
|
||
return out | ||
} | ||
|
||
// FieldNames returns field names from given in. | ||
// deprecated: use RawFieldNames instead automatically while model generating after goctl version v1.1.0 | ||
// Deprecated: Use github.com/tal-tech/go-zero/core/stores/builder.RawFieldNames instead. | ||
func FieldNames(in interface{}) []string { | ||
out := make([]string, 0) | ||
v := reflect.ValueOf(in) | ||
if v.Kind() == reflect.Ptr { | ||
v = v.Elem() | ||
} | ||
|
||
// we only accept structs | ||
if v.Kind() != reflect.Struct { | ||
panic(fmt.Errorf("ToMap only accepts structs; got %T", v)) | ||
} | ||
|
||
typ := v.Type() | ||
for i := 0; i < v.NumField(); i++ { | ||
// gets us a StructField | ||
fi := typ.Field(i) | ||
if tagv := fi.Tag.Get(dbTag); tagv != "" { | ||
out = append(out, tagv) | ||
} else { | ||
out = append(out, fi.Name) | ||
} | ||
} | ||
|
||
return out | ||
return builder.RawFieldNames(in) | ||
} | ||
|
||
// RawFieldNames converts golang struct field into slice string. | ||
// Deprecated: Use github.com/tal-tech/go-zero/core/stores/builder.RawFieldNames instead. | ||
func RawFieldNames(in interface{}, postgresSql ...bool) []string { | ||
out := make([]string, 0) | ||
v := reflect.ValueOf(in) | ||
if v.Kind() == reflect.Ptr { | ||
v = v.Elem() | ||
} | ||
|
||
var pg bool | ||
if len(postgresSql) > 0 { | ||
pg = postgresSql[0] | ||
} | ||
|
||
// we only accept structs | ||
if v.Kind() != reflect.Struct { | ||
panic(fmt.Errorf("ToMap only accepts structs; got %T", v)) | ||
} | ||
|
||
typ := v.Type() | ||
for i := 0; i < v.NumField(); i++ { | ||
// gets us a StructField | ||
fi := typ.Field(i) | ||
if tagv := fi.Tag.Get(dbTag); tagv != "" { | ||
if pg { | ||
out = append(out, tagv) | ||
} else { | ||
out = append(out, fmt.Sprintf("`%s`", tagv)) | ||
} | ||
} else { | ||
if pg { | ||
out = append(out, fi.Name) | ||
} else { | ||
out = append(out, fmt.Sprintf("`%s`", fi.Name)) | ||
} | ||
} | ||
} | ||
|
||
return out | ||
return builder.RawFieldNames(in, postgresSql...) | ||
} | ||
|
||
// PostgreSqlJoin concatenates the given elements into a string. | ||
// Deprecated: Use github.com/tal-tech/go-zero/core/stores/builderx.PostgreSqlJoin instead. | ||
func PostgreSqlJoin(elems []string) string { | ||
b := new(strings.Builder) | ||
for index, e := range elems { | ||
b.WriteString(fmt.Sprintf("%s = $%d, ", e, index+2)) | ||
} | ||
|
||
if b.Len() == 0 { | ||
return b.String() | ||
} | ||
|
||
return b.String()[0 : b.Len()-2] | ||
return builder.PostgreSqlJoin(elems) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.