format code (#386)
This commit is contained in:
@@ -23,10 +23,12 @@ func ToMap(in interface{}) map[string]interface{} {
|
||||
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
|
||||
@@ -43,6 +45,7 @@ func ToMap(in interface{}) map[string]interface{} {
|
||||
out[tagv] = current
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -53,10 +56,12 @@ func FieldNames(in interface{}) []string {
|
||||
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
|
||||
@@ -67,6 +72,7 @@ func FieldNames(in interface{}) []string {
|
||||
out = append(out, fi.Name)
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -76,10 +82,12 @@ func RawFieldNames(in interface{}) []string {
|
||||
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
|
||||
@@ -90,5 +98,6 @@ func RawFieldNames(in interface{}) []string {
|
||||
out = append(out, fmt.Sprintf(`"%s"`, fi.Name))
|
||||
}
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -8,34 +8,32 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type (
|
||||
User struct {
|
||||
// 自增id
|
||||
Id string `db:"id" json:"id,omitempty"`
|
||||
// 姓名
|
||||
UserName string `db:"user_name" json:"userName,omitempty"`
|
||||
// 1男,2女
|
||||
Sex int `db:"sex" json:"sex,omitempty"`
|
||||
type mockedUser struct {
|
||||
// 自增id
|
||||
Id string `db:"id" json:"id,omitempty"`
|
||||
// 姓名
|
||||
UserName string `db:"user_name" json:"userName,omitempty"`
|
||||
// 1男,2女
|
||||
Sex int `db:"sex" json:"sex,omitempty"`
|
||||
Uuid string `db:"uuid" uuid:"uuid,omitempty"`
|
||||
Age int `db:"age" json:"age"`
|
||||
}
|
||||
|
||||
Uuid string `db:"uuid" uuid:"uuid,omitempty"`
|
||||
|
||||
Age int `db:"age" json:"age"`
|
||||
}
|
||||
var (
|
||||
userFieldsWithRawStringQuote = RawFieldNames(mockedUser{})
|
||||
userFieldsWithoutRawStringQuote = FieldNames(mockedUser{})
|
||||
)
|
||||
|
||||
var userFieldsWithRawStringQuote = RawFieldNames(User{})
|
||||
var userFieldsWithoutRawStringQuote = FieldNames(User{})
|
||||
|
||||
func TestFieldNames(t *testing.T) {
|
||||
t.Run("old", func(t *testing.T) {
|
||||
var u User
|
||||
var u mockedUser
|
||||
out := FieldNames(&u)
|
||||
expected := []string{"id", "user_name", "sex", "uuid", "age"}
|
||||
assert.Equal(t, expected, out)
|
||||
})
|
||||
|
||||
t.Run("new", func(t *testing.T) {
|
||||
var u User
|
||||
var u mockedUser
|
||||
out := RawFieldNames(&u)
|
||||
expected := []string{"`id`", "`user_name`", "`sex`", "`uuid`", "`age`"}
|
||||
assert.Equal(t, expected, out)
|
||||
@@ -43,7 +41,7 @@ func TestFieldNames(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestNewEq(t *testing.T) {
|
||||
u := &User{
|
||||
u := &mockedUser{
|
||||
Id: "123456",
|
||||
UserName: "wahaha",
|
||||
}
|
||||
@@ -55,7 +53,7 @@ func TestNewEq(t *testing.T) {
|
||||
|
||||
// @see https://github.com/go-xorm/builder
|
||||
func TestBuilderSql(t *testing.T) {
|
||||
u := &User{
|
||||
u := &mockedUser{
|
||||
Id: "123123",
|
||||
}
|
||||
fields := RawFieldNames(u)
|
||||
@@ -96,7 +94,7 @@ func TestBuildSqlDefaultValue(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestBuilderSqlIn(t *testing.T) {
|
||||
u := &User{
|
||||
u := &mockedUser{
|
||||
Age: 18,
|
||||
}
|
||||
gtU := NewGt(u)
|
||||
|
||||
Reference in New Issue
Block a user