Best Got code snippet using utils.ToInterfaces
mysql_test_assist.go
Source:mysql_test_assist.go
...46 builder.Append(&weTestTab1.Weight)47 builder.Append(&weTestTab1.CreatedTime)48 builder.Append(&weTestTab1.ModifiedTime)49 builder.Append(&weTestTab1.IsDeleted)50 err := row.Scan(builder.ToInterfaces()...)51 if err != nil {52 return err53 }54 return nil55}56func (weTestTab1 *WeTestTab1) RowsToStruct(rows *db.Rows) error {57 var weTestTab1s [] WeTestTab158 builder := tsgutils.NewInterfaceBuilder()59 for rows.Next() {60 builder.Clear()61 builder.Append(&weTestTab1.Id)62 builder.Append(&weTestTab1.Name)63 builder.Append(&weTestTab1.Gender)64 builder.Append(&weTestTab1.Birthday)65 builder.Append(&weTestTab1.Stature)66 builder.Append(&weTestTab1.Weight)67 builder.Append(&weTestTab1.CreatedTime)68 builder.Append(&weTestTab1.ModifiedTime)69 builder.Append(&weTestTab1.IsDeleted)70 err := rows.Scan(builder.ToInterfaces()...)71 if err != nil {72 return err73 }74 weTestTab1s = append(weTestTab1s, *weTestTab1)75 }76 if rows != nil {77 defer rows.Close()78 }79 weTestTab1.WeTestTab1s = weTestTab1s80 return nil81}82func (weTestTab1 *WeTestTab1) Insert(client *DBClient, idSet bool) (int64, error) {83 structParam := *weTestTab184 sql := tsgutils.NewStringBuilder()85 qSql := tsgutils.NewStringBuilder()86 params := tsgutils.NewInterfaceBuilder()87 sql.Append("INSERT INTO ")88 sql.Append("we_test_tab1")89 sql.Append(" (")90 ks := reflect.TypeOf(structParam)91 vs := reflect.ValueOf(structParam)92 for i, ksLen := 0, ks.NumField()-1; i < ksLen; i++ {93 col := ks.Field(i).Tag.Get("column")94 v := vs.Field(i).Interface()95 if col == "id" && !idSet {96 continue97 }98 sql.Append("`").Append(col).Append("`,")99 qSql.Append("?,")100 params.Append(v)101 }102 sql.RemoveLast()103 qSql.RemoveLast()104 sql.Append(") VALUES (").Append(qSql.ToString()).Append(");")105 defer client.CloseConn()106 return client.Exec(sql.ToString(), params.ToInterfaces()...)107}108func (weTestTab1 *WeTestTab1) UpdateWeTestTab1ById(client *DBClient) (int64, error) {109 structParam := *weTestTab1110 sql := tsgutils.NewStringBuilder()111 params := tsgutils.NewInterfaceBuilder()112 sql.Append("UPDATE ")113 sql.Append("we_test_tab1")114 sql.Append(" SET ")115 ks := reflect.TypeOf(structParam)116 vs := reflect.ValueOf(structParam)117 var id interface{}118 for i, ksLen := 0, ks.NumField()-1; i < ksLen; i++ {119 col := ks.Field(i).Tag.Get("column")120 v := vs.Field(i).Interface()121 if col == "id" {122 id = v123 continue124 }125 sql.Append(col).Append("=").Append("?,")126 params.Append(v)127 }128 sql.RemoveLast()129 params.Append(id)130 sql.Append(" WHERE id = ?;")131 defer client.CloseConn()132 return client.Exec(sql.ToString(), params.ToInterfaces()...)133}134func (weTestTab1 *WeTestTab1) DeleteWeTestTab1ById(client *DBClient) (int64, error) {135 structParam := weTestTab1136 sql := tsgutils.NewStringBuilder()137 sql.Append("DELETE FROM ")138 sql.Append("we_test_tab1")139 sql.Append(" WHERE id = ?;")140 defer client.CloseConn()141 return client.Exec(sql.ToString(), structParam.Id)142}143func (weTestTab1 *WeTestTab1) BatchInsert(client *DBClient, idSet, returnIds bool) ([]int64, error) {144 structParam := *weTestTab1145 list := structParam.WeTestTab1s146 var result []int64147 listLen := len(list)148 if listLen == 0 {149 return result, errors.New("no data needs to be inserted")150 }151 sql := tsgutils.NewStringBuilder()152 oneQSql := tsgutils.NewStringBuilder()153 batchQSql := tsgutils.NewStringBuilder()154 ks := reflect.TypeOf(structParam)155 fieldsNum := ks.NumField() - 1156 sql.Append("INSERT INTO ")157 sql.Append("we_test_tab1")158 sql.Append(" (")159 for i := 0; i < fieldsNum; i++ {160 iCol := ks.Field(i).Tag.Get("column")161 if iCol == "id" && !idSet {162 continue163 }164 sql.Append("`").Append(iCol).Append("`,")165 }166 sql.RemoveLast().Append(") VALUES ")167 batchInsertColsLen := tsgutils.InterfaceToInt(tsgutils.IIIInterfaceOperator(idSet, fieldsNum, fieldsNum-1))168 oneQSql.Append("(")169 for j := 0; j < batchInsertColsLen; j++ {170 oneQSql.Append("?,")171 }172 oneQSql.RemoveLast().Append(")")173 if !returnIds {174 for j := 0; j < listLen; j++ {175 batchQSql.Append(oneQSql.ToString()).Append(",")176 }177 batchQSql.RemoveLast()178 batchSql := tsgutils.NewStringBuilder().Append(sql.ToString()).Append(batchQSql.ToString()).Append(";").ToString()179 batchParams := tsgutils.NewInterfaceBuilder()180 for k := range list {181 item := list[k]182 kItem := reflect.ValueOf(item)183 for l := 0; l < fieldsNum; l++ {184 lCol := ks.Field(l).Tag.Get("column")185 if lCol == "id" && !idSet {186 continue187 }188 batchParams.Append(kItem.Field(l).Interface())189 }190 }191 id, err := client.Exec(batchSql, batchParams.ToInterfaces()...)192 if err != nil {193 return result, err194 }195 result = append(result, id)196 } else {197 oneSql := tsgutils.NewStringBuilder().Append(sql.ToString()).Append(oneQSql.ToString()).Append(";").ToString()198 oneParams := tsgutils.NewInterfaceBuilder()199 tx, err := client.TxBegin()200 if err != nil {201 return result, err202 }203 for m := range list {204 oneParams.Clear()205 item := list[m]206 mItem := reflect.ValueOf(item)207 for n := 0; n < fieldsNum; n++ {208 nCol := ks.Field(n).Tag.Get("column")209 if nCol == "id" && !idSet {210 continue211 }212 oneParams.Append(mItem.Field(n).Interface())213 }214 id, err := client.TxExec(tx, oneSql, oneParams.ToInterfaces()...)215 if err != nil {216 client.TxRollback(tx)217 var resultTxRollback []int64218 return resultTxRollback, err219 }220 result = append(result, id)221 }222 if !client.TxCommit(tx) {223 return result, errors.New("batch insert (returnIds=true) tx commit failed")224 }225 }226 defer client.CloseConn()227 return result, nil228}229/*230 test table2231*/232type WeTestTab2 struct {233 Id int64 `column:"id"` // The primary key id234 UserId int64 `column:"user_id"` // The user id235 AreaCode int64 `column:"area_code"` // The user area code236 Phone int64 `column:"phone"` // The user phone237 Email string `column:"email"` // The user email238 Postcode int64 `column:"postcode"` // The user postcode239 AdministrationCode int64 `column:"administration_code"` // The user administration code240 Address string `column:"address"` // The user address241 CreatedTime time.Time `column:"created_time"` // created time242 ModifiedTime time.Time `column:"modified_time"` // modified time243 IsDeleted int64 `column:"is_deleted"` // Logic to delete(0:normal 1:deleted)244 WeTestTab2s [] WeTestTab2 // This value is used for batch queries and inserts.245}246func (weTestTab2 *WeTestTab2) RowToStruct(row *db.Row) error {247 builder := tsgutils.NewInterfaceBuilder()248 builder.Append(&weTestTab2.Id)249 builder.Append(&weTestTab2.UserId)250 builder.Append(&weTestTab2.AreaCode)251 builder.Append(&weTestTab2.Phone)252 builder.Append(&weTestTab2.Email)253 builder.Append(&weTestTab2.Postcode)254 builder.Append(&weTestTab2.AdministrationCode)255 builder.Append(&weTestTab2.Address)256 builder.Append(&weTestTab2.CreatedTime)257 builder.Append(&weTestTab2.ModifiedTime)258 builder.Append(&weTestTab2.IsDeleted)259 err := row.Scan(builder.ToInterfaces()...)260 if err != nil {261 return err262 }263 return nil264}265func (weTestTab2 *WeTestTab2) RowsToStruct(rows *db.Rows) error {266 var weTestTab2s [] WeTestTab2267 builder := tsgutils.NewInterfaceBuilder()268 for rows.Next() {269 builder.Clear()270 builder.Append(&weTestTab2.Id)271 builder.Append(&weTestTab2.UserId)272 builder.Append(&weTestTab2.AreaCode)273 builder.Append(&weTestTab2.Phone)274 builder.Append(&weTestTab2.Email)275 builder.Append(&weTestTab2.Postcode)276 builder.Append(&weTestTab2.AdministrationCode)277 builder.Append(&weTestTab2.Address)278 builder.Append(&weTestTab2.CreatedTime)279 builder.Append(&weTestTab2.ModifiedTime)280 builder.Append(&weTestTab2.IsDeleted)281 err := rows.Scan(builder.ToInterfaces()...)282 if err != nil {283 return err284 }285 weTestTab2s = append(weTestTab2s, *weTestTab2)286 }287 if rows != nil {288 defer rows.Close()289 }290 weTestTab2.WeTestTab2s = weTestTab2s291 return nil292}293func (weTestTab2 *WeTestTab2) Insert(client *DBClient, idSet bool) (int64, error) {294 structParam := *weTestTab2295 sql := tsgutils.NewStringBuilder()296 qSql := tsgutils.NewStringBuilder()297 params := tsgutils.NewInterfaceBuilder()298 sql.Append("INSERT INTO ")299 sql.Append("we_test_tab2")300 sql.Append(" (")301 ks := reflect.TypeOf(structParam)302 vs := reflect.ValueOf(structParam)303 for i, ksLen := 0, ks.NumField()-1; i < ksLen; i++ {304 col := ks.Field(i).Tag.Get("column")305 v := vs.Field(i).Interface()306 if col == "id" && !idSet {307 continue308 }309 sql.Append("`").Append(col).Append("`,")310 qSql.Append("?,")311 params.Append(v)312 }313 sql.RemoveLast()314 qSql.RemoveLast()315 sql.Append(") VALUES (").Append(qSql.ToString()).Append(");")316 defer client.CloseConn()317 return client.Exec(sql.ToString(), params.ToInterfaces()...)318}319func (weTestTab2 *WeTestTab2) UpdateWeTestTab2ById(client *DBClient) (int64, error) {320 structParam := *weTestTab2321 sql := tsgutils.NewStringBuilder()322 params := tsgutils.NewInterfaceBuilder()323 sql.Append("UPDATE ")324 sql.Append("we_test_tab2")325 sql.Append(" SET ")326 ks := reflect.TypeOf(structParam)327 vs := reflect.ValueOf(structParam)328 var id interface{}329 for i, ksLen := 0, ks.NumField()-1; i < ksLen; i++ {330 col := ks.Field(i).Tag.Get("column")331 v := vs.Field(i).Interface()332 if col == "id" {333 id = v334 continue335 }336 sql.Append(col).Append("=").Append("?,")337 params.Append(v)338 }339 sql.RemoveLast()340 params.Append(id)341 sql.Append(" WHERE id = ?;")342 defer client.CloseConn()343 return client.Exec(sql.ToString(), params.ToInterfaces()...)344}345func (weTestTab2 *WeTestTab2) DeleteWeTestTab2ById(client *DBClient) (int64, error) {346 structParam := weTestTab2347 sql := tsgutils.NewStringBuilder()348 sql.Append("DELETE FROM ")349 sql.Append("we_test_tab2")350 sql.Append(" WHERE id = ?;")351 defer client.CloseConn()352 return client.Exec(sql.ToString(), structParam.Id)353}354func (weTestTab2 *WeTestTab2) BatchInsert(client *DBClient, idSet, returnIds bool) ([]int64, error) {355 structParam := *weTestTab2356 list := structParam.WeTestTab2s357 var result []int64358 listLen := len(list)359 if listLen == 0 {360 return result, errors.New("no data needs to be inserted")361 }362 sql := tsgutils.NewStringBuilder()363 oneQSql := tsgutils.NewStringBuilder()364 batchQSql := tsgutils.NewStringBuilder()365 ks := reflect.TypeOf(structParam)366 fieldsNum := ks.NumField() - 1367 sql.Append("INSERT INTO ")368 sql.Append("we_test_tab2")369 sql.Append(" (")370 for i := 0; i < fieldsNum; i++ {371 iCol := ks.Field(i).Tag.Get("column")372 if iCol == "id" && !idSet {373 continue374 }375 sql.Append("`").Append(iCol).Append("`,")376 }377 sql.RemoveLast().Append(") VALUES ")378 batchInsertColsLen := tsgutils.InterfaceToInt(tsgutils.IIIInterfaceOperator(idSet, fieldsNum, fieldsNum-1))379 oneQSql.Append("(")380 for j := 0; j < batchInsertColsLen; j++ {381 oneQSql.Append("?,")382 }383 oneQSql.RemoveLast().Append(")")384 if !returnIds {385 for j := 0; j < listLen; j++ {386 batchQSql.Append(oneQSql.ToString()).Append(",")387 }388 batchQSql.RemoveLast()389 batchSql := tsgutils.NewStringBuilder().Append(sql.ToString()).Append(batchQSql.ToString()).Append(";").ToString()390 batchParams := tsgutils.NewInterfaceBuilder()391 for k := range list {392 item := list[k]393 kItem := reflect.ValueOf(item)394 for l := 0; l < fieldsNum; l++ {395 lCol := ks.Field(l).Tag.Get("column")396 if lCol == "id" && !idSet {397 continue398 }399 batchParams.Append(kItem.Field(l).Interface())400 }401 }402 id, err := client.Exec(batchSql, batchParams.ToInterfaces()...)403 if err != nil {404 return result, err405 }406 result = append(result, id)407 } else {408 oneSql := tsgutils.NewStringBuilder().Append(sql.ToString()).Append(oneQSql.ToString()).Append(";").ToString()409 oneParams := tsgutils.NewInterfaceBuilder()410 tx, err := client.TxBegin()411 if err != nil {412 return result, err413 }414 for m := range list {415 oneParams.Clear()416 item := list[m]417 mItem := reflect.ValueOf(item)418 for n := 0; n < fieldsNum; n++ {419 nCol := ks.Field(n).Tag.Get("column")420 if nCol == "id" && !idSet {421 continue422 }423 oneParams.Append(mItem.Field(n).Interface())424 }425 id, err := client.TxExec(tx, oneSql, oneParams.ToInterfaces()...)426 if err != nil {427 client.TxRollback(tx)428 var resultTxRollback []int64429 return resultTxRollback, err430 }431 result = append(result, id)432 }433 if !client.TxCommit(tx) {434 return result, errors.New("batch insert (returnIds=true) tx commit failed")435 }436 }437 defer client.CloseConn()438 return result, nil439}...
interface_builder.go
Source:interface_builder.go
...12 var i1 int = 12313 var f1 float64 = 123.4014 builder1 := NewInterfaceBuilder()15 builder1.Append(s1).Append(i1).Append(f1)16 fmt.Println(builder1.ToInterfaces())17 builder2 := builder1.Clear()18 builder2.Appends(s1, i1, f1)19 fmt.Println(builder2.ToInterfaces())20 print:21 [Abc 123 123.4]22 [Abc 123 123.4]23 If you operate the database:24 var users []User25 builder := tsgutils.NewInterfaceBuilder()26 for rows.Next() {27 builder.Clear()28 builder.Append(&user.Host).Append(&user.User)29 builder.Append(&user.AuthenticationString)30 err := rows.Scan(builder.ToInterfaces()...)31 tsgutils.CheckAndPrintError("MySQL query rows scan error", err)32 users = append(users, *user)33 }34 */35type InterfaceBuilder struct {36 interfaces []interface{}37}38func NewInterfaceBuilder() *InterfaceBuilder {39 var interfaces InterfaceBuilder40 return &interfaces41}42func (builder *InterfaceBuilder) Append(arg interface{}) *InterfaceBuilder {43 builder.interfaces = append(builder.interfaces, arg)44 return builder45}46func (builder *InterfaceBuilder) Appends(args ...interface{}) *InterfaceBuilder {47 for i := range args {48 builder.interfaces = append(builder.interfaces, args[i])49 }50 return builder51}52func (builder *InterfaceBuilder) Clear() *InterfaceBuilder {53 var interfaces []interface{}54 builder.interfaces = interfaces55 return builder56}57func (builder *InterfaceBuilder) ToInterfaces() []interface{} {58 return builder.interfaces59}...
utils.go
Source:utils.go
...33func MethodType(target interface{}, method string) reflect.Type {34 targetVal := reflect.ValueOf(target)35 return targetVal.MethodByName(method).Type()36}37// ToInterfaces convertor38func ToInterfaces(vs []reflect.Value) []interface{} {39 out := []interface{}{}40 for _, v := range vs {41 out = append(out, v.Interface())42 }43 return out44}45// ToValues convertor46func ToValues(vs []interface{}) []reflect.Value {47 out := []reflect.Value{}48 for _, v := range vs {49 out = append(out, reflect.ValueOf(v))50 }51 return out52}...
ToInterfaces
Using AI Code Generation
1func main() {2 arr := []int{1, 2, 3, 4}3 arr1 := utils.ToInterfaces(arr)4 fmt.Println(arr1)5}6func main() {7 arr := []int{1, 2, 3, 4}8 arr1 := utils.ToInterfaces(arr)9 fmt.Println(arr1)10}11func main() {12 arr := []int{1, 2, 3, 4}13 arr1 := utils.ToInterfaces(arr)14 fmt.Println(arr1)15}16func main() {17 arr := []int{1, 2, 3, 4}18 arr1 := utils.ToInterfaces(arr)19 fmt.Println(arr1)20}21func main() {22 arr := []int{1, 2, 3, 4}23 arr1 := utils.ToInterfaces(arr)24 fmt.Println(arr1)25}26func main() {27 arr := []int{1, 2, 3, 4}28 arr1 := utils.ToInterfaces(arr)29 fmt.Println(arr1)30}31func main() {32 arr := []int{1, 2, 3, 4}33 arr1 := utils.ToInterfaces(arr)34 fmt.Println(arr1)35}36func main() {37 arr := []int{1, 2, 3, 4}38 arr1 := utils.ToInterfaces(arr)39 fmt.Println(arr1)40}41func main() {42 arr := []int{1, 2, 3, 4}43 arr1 := utils.ToInterfaces(arr)44 fmt.Println(arr1)45}46func main()
ToInterfaces
Using AI Code Generation
1func main() {2 a := []int{1, 2, 3, 4, 5}3 b := []string{"a", "b", "c", "d", "e"}4 c := []float64{1.1, 2.2, 3.3, 4.4, 5.5}5 d := []int{1, 2, 3, 4, 5}6 e := []string{"a", "b", "c", "d", "e"}7 f := []float64{1.1, 2.2, 3.3, 4.4, 5.5}8 g := []int{1, 2, 3, 4, 5}9 h := []string{"a", "b", "c", "d", "e"}10 i := []float64{1.1, 2.2, 3.3, 4.4, 5.5}11 j := []int{1, 2, 3, 4, 5}12 k := []string{"a", "b", "c", "d", "e"}13 l := []float64{1.1, 2.2, 3.3, 4.4, 5.5}14 m := []int{1, 2, 3, 4, 5}15 n := []string{"a", "b", "c", "d", "e"}16 o := []float64{1.1, 2.2, 3.3, 4.4, 5.5}17 p := []int{1, 2, 3, 4, 5}18 q := []string{"a", "b", "
ToInterfaces
Using AI Code Generation
1import (2func main() {3 var a = []string{"a", "b", "c"}4 var b = utils.ToInterfaces(a)5 fmt.Println(b)6}7import (8func main() {9 var a = []interface{}{"a", "b", "c"}10 var b = utils.ToStrings(a)11 fmt.Println(b)12}13func ToInterfaces(a []string) []interface{} {14 var b = make([]interface{}, len(a))15 for i, v := range a {16 }17}18func ToStrings(a []interface{}) []string {19 var b = make([]string, len(a))20 for i, v := range a {21 b[i] = v.(string)22 }23}24import (25func TestToInterfaces(t *testing.T) {26 var a = []string{"a", "b", "c"}27 var b = ToInterfaces(a)28 if len(b) != 3 {29 t.Errorf("Expected length to be 3 but got %d", len(b))30 }31}32func TestToStrings(t *testing.T) {33 var a = []interface{}{"a", "b", "c"}34 var b = ToStrings(a)35 if len(b) != 3 {36 t.Errorf("Expected length to be 3 but got %d", len(b))37 }38}39import (40func TestToInterfaces(t *testing.T) {41 var a = []string{"a", "b", "c"}42 var b = ToInterfaces(a)43 if len(b) != 3 {44 t.Errorf("Expected length to be 3 but got %d", len(b))45 }46}47func TestToStrings(t *testing.T) {48 var a = []interface{}{"a", "b", "c"}
ToInterfaces
Using AI Code Generation
1import (2func main() {3 fmt.Println(utils.ToInterfaces([]string{"a", "b", "c"}))4}5import (6func main() {7 fmt.Println(utils.ToStrings([]interface{}{"a", "b", "c"}))8}
ToInterfaces
Using AI Code Generation
1import (2func main() {3 slice := []int{1, 2, 3, 4, 5}4 sliceInterface := bvc.ToInterfaces(slice)5 fmt.Println(sliceInterface)6}7import (8func main() {9 slice := []interface{}{1, 2, 3, 4, 5}10 sliceInt := bvc.ToInts(slice)11 fmt.Println(sliceInt)12}13import (14func main() {15 slice := []interface{}{"1", "2", "3", "4", "5"}16 sliceString := bvc.ToStrings(slice)17 fmt.Println(sliceString)18}19import (20func main() {21 slice := []interface{}{1.1, 2.2, 3.3, 4.4, 5.5}22 sliceFloat64 := bvc.ToFloat64s(slice)23 fmt.Println(sliceFloat64)24}25import (26func main() {27 slice := []interface{}{1.1, 2.2, 3.3, 4.4, 5.5}
ToInterfaces
Using AI Code Generation
1import (2func main() {3 var sli = []int{1, 2, 3, 4}4 var sli1 = utils.ToInterfaces(sli)5 fmt.Println(reflect.TypeOf(sli1))6}7[]interface {}8func ToInterfaces(slice interface{}) []interface{} {9 s := reflect.ValueOf(slice)10 if s.Kind() != reflect.Slice {11 panic("InterfaceSlice() given a non-slice type")12 }13 ret := make([]interface{}, s.Len())14 for i := 0; i < s.Len(); i++ {15 ret[i] = s.Index(i).Interface()16 }17}
ToInterfaces
Using AI Code Generation
1import (2func main() {3 var x interface{} = []string{"a", "b", "c"}4 fmt.Println(utils.ToInterfaces(x))5}6[interface {}{"a", "b", "c"}]
ToInterfaces
Using AI Code Generation
1import (2func main() {3 var x interface{} = 14 var y interface{} = "hello"5 var z interface{} = 1.236 result := utils.ToInterfaces(x, y, z)7 fmt.Println(result)8}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!