Best K6 code snippet using common.FieldName
CommonData.go
Source:CommonData.go
...139 Values [][]string `json:"values"` // æ°æ®è¡¨140 ValueBuf [][]byte `json:"-"` // æ¯ä¸ªvalue对åºçjsonå符串(ç¸å½äºé¢å
æå
好çåæ¡æ°æ®çjsonStr)141 // è¾
å©æ°æ®142 DataCount int `json:"-"` // æ°æ®æ»æ¡æ°143 FieldNameMap map[string]int `json:"-"` // æ°æ®å称å°æ°æ®ä¸æ çæ å°144 FieldCount int `json:"-"` // ä¸æ¡æ°æ®çæ°æ®ä¸ªæ°145}146func NewCommonJsonData() *CommonJsonData {147 this := new(CommonJsonData)148 // this.FieldNameMap = new(map[string]int)149 this.sep = "|"150 return this151}152func NewCommonJsonDataFromFile(filename string) *CommonJsonData {153 this := NewCommonJsonData()154 err := this.DecodeJsonFile(filename)155 if err != nil {156 ShowError(err)157 return nil158 }159 return this160}161// ä»jsonæ ¼å¼ä¸è¯»åç»ä¸æ°æ®162func (this *CommonJsonData) DecodeJsonFile(filename string) error {163 var e error164 f, e := os.Open(filename)165 if e != nil {166 return e167 }168 defer f.Close()169 var buf, jsonBuffer []byte170 bufLen := 1024171 buf = make([]byte, bufLen) // read buf172 var n, count int173 count = 0174 for {175 n, e = f.Read(buf)176 if e != nil || n <= 0 {177 break178 }179 if jsonBuffer == nil { // é¦æ¬¡è¯»å180 if n < bufLen {181 jsonBuffer = buf[0:n]182 break183 }184 jsonBuffer = make([]byte, bufLen<<2)185 }186 jsonBuffer = append(jsonBuffer[0:count], buf[:n]...)187 count += n188 }189 // check uft8 bom190 if CheckUTF8_BOM(jsonBuffer) {191 jsonBuffer = jsonBuffer[UTF8_BOM_LEN:]192 }193 e = json.Unmarshal(jsonBuffer, this)194 if e != nil {195 return e196 }197 this.FileName = filename198 this.JsonStr = string(jsonBuffer)199 // field æ»æ°200 if this.Fields != nil {201 // field æ´ç202 this.FieldCount = len(this.Fields)203 this.FieldNameMap = make(map[string]int, this.FieldCount)204 for i := 0; i < this.FieldCount; i++ {205 this.FieldNameMap[this.Fields[i]] = i206 }207 } else {208 this.FieldCount = 0209 }210 // æ°æ®æ¡æ°211 if this.Values != nil {212 this.DataCount = len(this.Values)213 } else {214 this.DataCount = 0215 }216 return nil217}218func (this *CommonJsonData) getValue(index int, fieldName string) string {219 var (220 fieldIndex int221 ok bool222 )223 if index < 0 || index > this.DataCount {224 ShowError("CommonJsonData.getValue() invalid index , fileName=", Color(CL_YELLOW, this.FileName),225 ", index=", index, ", DataCount=", this.DataCount)226 return ""227 }228 fieldIndex, ok = this.FieldNameMap[fieldName]229 if !ok {230 ShowError("CommonJsonData.getValue() invalid fieldName , fileName=", Color(CL_YELLOW, this.FileName),231 " ,fieldName=", fieldName)232 return ""233 }234 return this.Values[index][fieldIndex]235}236// ä»æ°æ®éä¸è§£æåºä¸ä¸ª string237func (this *CommonJsonData) ParseString(index int, fieldName string) string {238 return this.getValue(index, fieldName)239}240// ä»æ°æ®éä¸è§£æåºä¸ä¸ªint64241func (this *CommonJsonData) ParseInt64(index int, fieldName string) int64 {242 var (...
common_import.go
Source:common_import.go
1package rules2import (3 "fmt"4 "github.com/nbkit/mdf/db"5 "strings"6 "github.com/nbkit/mdf/framework/files"7 "github.com/nbkit/mdf/framework/md"8 "github.com/nbkit/mdf/utils"9)10type commonImport struct {11 register *md.MDRule12}13func newCommonImport() *commonImport {14 return &commonImport{15 register: &md.MDRule{Action: "import", Code: "import", Widget: "common", Sequence: 50},16 }17}18func (s *commonImport) Register() *md.MDRule {19 return s.register20}21func (s *commonImport) Exec(flow *utils.FlowContext) {22 logData := &md.MDLog{EntID: flow.EntID(), UserID: flow.UserID(), NodeType: flow.Request.Widget, NodeID: flow.Request.Widget, DataID: flow.Request.Entity}23 md.LogSv().CreateLog(logData.Clone().SetMsg("导å
¥å¼å§======begin======"))24 defer func() {25 md.LogSv().CreateLog(logData.Clone().SetMsg("导å
¥ç»æ"))26 }()27 if flow.Request.Data == nil {28 err := flow.Error("没æè¦å¯¼å
¥çæ°æ®")29 md.LogSv().CreateLog(logData.Clone().SetMsg(err))30 return31 }32 data := flow.Request.Data33 if items, ok := data.([]files.ImportData); ok {34 for _, data := range items {35 s.importMapData(flow, data)36 }37 } else if items, ok := data.(files.ImportData); ok {38 s.importMapData(flow, items)39 }40}41func (s *commonImport) importMapData(flow *utils.FlowContext, data files.ImportData) {42 log := md.LogSv()43 logData := &md.MDLog{EntID: flow.EntID(), UserID: flow.UserID(), NodeType: flow.Request.Widget, NodeID: flow.Request.Widget, DataID: flow.Request.Entity}44 log.CreateLog(logData.Clone().SetMsg(fmt.Sprintf("æ¥æ¶å°éè¦å¯¼å
¥çæ°æ®-%sï¼%væ¡", flow.Request.Entity, len(data.Data))))45 entity := md.MDSv().GetEntity(data.EntityCode)46 if entity == nil {47 entity = md.MDSv().GetEntity(flow.Request.Entity)48 }49 if entity == nil {50 log.CreateLog(logData.Clone().SetMsg(fmt.Sprintf("没æé
置导å
¥å®ä½ï¼è¯·ç¡®è®¤æ¯å¦éè¦å¯¼å
¥,%v", data.SheetName)))51 return52 }53 dbDatas := make([]map[string]interface{}, 0)54 quotedMap := make(map[string]string)55 for _, item := range data.Data {56 dbItem := make(map[string]interface{})57 if v, ok := item[utils.STATE_FIELD]; ok && (v == utils.STATE_TEMP || v == utils.STATE_NORMAL || v == utils.STATE_IGNORED) {58 continue59 }60 for kk, kv := range item {61 field := entity.GetField(kk)62 if field == nil || kv == "" {63 continue64 }65 fieldName := ""66 if field.TypeType == utils.TYPE_ENTITY {67 fieldName = field.DbName + "_id"68 qreq := flow.Copy()69 qreq.Request.Entity = field.TypeID70 qreq.Request.Q = kv71 qreq.Request.Data = item72 if md.MDSv().TakeDataByQ(flow); flow.Error() != nil {73 log.CreateLog(logData.Clone().SetMsg(fmt.Sprintf("æ°æ®[%s]=[%s],æ¥è¯¢å¤±è´¥ï¼%v", qreq.Request.Entity, qreq.Request.Q, flow.Error())))74 } else if v := flow.Response.Get("id"); v != nil {75 dbItem[fieldName] = v76 quotedMap[fieldName] = fieldName77 } else {78 log.CreateLog(logData.Clone().SetMsg(fmt.Sprintf("å
³è对象[%s],æ¾ä¸å°[%s]对åºæ°æ®!", qreq.Request.Entity, qreq.Request.Q)))79 }80 } else if field.TypeType == utils.TYPE_ENUM {81 fieldName = field.DbName + "_id"82 if vv := md.MDSv().GetEnum(field.Limit, kv); vv != nil {83 dbItem[fieldName] = vv.ID84 quotedMap[fieldName] = fieldName85 } else {86 log.CreateLog(logData.Clone().SetMsg(fmt.Sprintf("å
³èæ举[%s],æ¾ä¸å°[%s]对åºæ°æ®!", field.Limit, kv)))87 }88 } else if field.TypeType == utils.TYPE_SIMPLE {89 fieldName = field.DbName90 if field.TypeID == utils.FIELD_TYPE_BOOL {91 dbItem[fieldName] = utils.ToSBool(files.GetCellValue(kk, item))92 quotedMap[fieldName] = fieldName93 } else if field.TypeID == utils.FIELD_TYPE_DATETIME || field.TypeID == utils.FIELD_TYPE_DATE {94 dbItem[fieldName] = files.GetCellValue(kk, item)95 quotedMap[fieldName] = fieldName96 } else if field.TypeID == utils.FIELD_TYPE_DECIMAL || field.TypeID == utils.FIELD_TYPE_INT {97 dbItem[fieldName] = files.GetCellValue(kk, item)98 quotedMap[fieldName] = fieldName99 } else {100 dbItem[fieldName] = kv101 quotedMap[fieldName] = fieldName102 }103 }104 }105 if field := entity.GetField("ID"); field != nil {106 fieldName := field.DbName107 if _, ok := dbItem[fieldName]; !ok {108 dbItem[fieldName] = utils.GUID()109 }110 quotedMap[fieldName] = fieldName111 }112 if field := entity.GetField("EntID"); field != nil && field.DbName != "" {113 fieldName := field.DbName114 if _, ok := dbItem[fieldName]; !ok {115 dbItem[fieldName] = flow.EntID()116 }117 quotedMap[fieldName] = fieldName118 }119 if field := entity.GetField("CreatedBy"); field != nil && field.DbName != "" {120 fieldName := field.DbName121 if _, ok := dbItem[fieldName]; !ok {122 dbItem[fieldName] = flow.UserID()123 }124 quotedMap[fieldName] = fieldName125 }126 if field := entity.GetField("CreatedAt"); field != nil && field.DbName != "" {127 fieldName := field.DbName128 dbItem[fieldName] = utils.TimeNow()129 quotedMap[fieldName] = fieldName130 }131 if field := entity.GetField("UpdatedAt"); field != nil && field.DbName != "" {132 fieldName := field.DbName133 dbItem[fieldName] = utils.TimeNow()134 quotedMap[fieldName] = fieldName135 }136 if len(dbItem) > 0 {137 dbDatas = append(dbDatas, dbItem)138 }139 }140 quoted := make([]string, 0, len(quotedMap))141 for fk, _ := range quotedMap {142 quoted = append(quoted, fk)143 }144 placeholdersArr := make([]string, 0, len(quotedMap))145 valueVars := make([]interface{}, 0)146 var itemCount uint = 0147 var MaxBatchs uint = 100148 for _, data := range dbDatas {149 itemCount = itemCount + 1150 placeholders := make([]string, 0, len(quoted))151 for _, f := range quoted {152 placeholders = append(placeholders, "?")153 valueVars = append(valueVars, data[f])154 }155 placeholdersArr = append(placeholdersArr, "("+strings.Join(placeholders, ", ")+")")156 if itemCount >= MaxBatchs {157 if err := s.batchInsertSave(entity, quoted, placeholdersArr, valueVars...); err != nil {158 log.CreateLog(logData.Clone().SetMsg(fmt.Sprintf("æ°æ®åºåå¨[%v]æ¡è®°å½åºéäº:%s!", itemCount, err.Error())))159 flow.Error(err)160 return161 }162 itemCount = 0163 placeholdersArr = make([]string, 0, len(quotedMap))164 valueVars = make([]interface{}, 0)165 }166 }167 if itemCount > 0 {168 if err := s.batchInsertSave(entity, quoted, placeholdersArr, valueVars...); err != nil {169 log.CreateLog(logData.Clone().SetMsg(fmt.Sprintf("æ°æ®åºåå¨[%v]æ¡è®°å½åºéäº:%s!", itemCount, err.Error())))170 flow.Error(err)171 return172 }173 }174}175func (s *commonImport) batchInsertSave(entity *md.MDEntity, quoted []string, placeholders []string, valueVars ...interface{}) error {176 var sql = fmt.Sprintf("INSERT INTO %s (%s) VALUES %s", db.Default().Dialect().Quote(entity.TableName), strings.Join(quoted, ", "), strings.Join(placeholders, ", "))177 if err := db.Default().Exec(sql, valueVars...).Error; err != nil {178 return err179 }180 return nil181}...
selector.go
Source:selector.go
1package instruction2import "github.com/lwpyr/goscript/common"3func SelectorLhs(constructor common.Constructor, fieldName string, oneOfs []string) common.Instruction {4 if len(oneOfs) > 0 {5 return func(m *common.Memory, stk *common.Stack) {6 ptr := stk.Top().(*interface{})7 if *ptr == nil {8 *ptr = constructor()9 }10 for _, name := range oneOfs {11 MessageReset(*ptr, name)12 }13 ptr = MessageGetPtr(*ptr, fieldName)14 stk.Set(0, ptr)15 stk.Pc++16 }17 } else {18 return func(m *common.Memory, stk *common.Stack) {19 ptr := stk.Top().(*interface{})20 if *ptr == nil {21 *ptr = constructor()22 }23 ptr = MessageGetPtr(*ptr, fieldName)24 stk.Set(0, ptr)25 stk.Pc++26 }27 }28}29func Selector(fieldName string) common.Instruction {30 return func(m *common.Memory, stk *common.Stack) {31 if stk.Top() != nil {32 stk.Set(0, MessageGet(stk.Top(), fieldName))33 }34 stk.Pc++35 }36}37func SelectorEnum(fieldName string) common.Instruction {38 return func(m *common.Memory, stk *common.Stack) {39 if stk.Top() != nil {40 stk.Set(0, stk.Top().(map[string]int32)[fieldName])41 }42 stk.Pc++43 }44}...
FieldName
Using AI Code Generation
1import (2func main() {3 fmt.Println(common.FieldName())4}5func FieldName() string {6}
FieldName
Using AI Code Generation
1import (2func main() {3 fmt.Println(golcommon.FieldName())4}5import (6func main() {7 fmt.Println(golcommon.FieldName())8}9func FieldName() string {10}11import (12func TestFieldName(t *testing.T) {13 if FieldName() != "FieldName" {14 t.Error("FieldName() does not return FieldName")15 }16}17import (18func main() {19 fmt.Println(golcommon.FieldName())20}21import (22func main() {23 fmt.Println(golcommon.FieldName())24}25As you can see, the package is not imported again, because it
FieldName
Using AI Code Generation
1func main() {2 fmt.Println(common.FieldName())3}4func main() {5 fmt.Println(common.FieldName())6}7func main() {8 fmt.Println(common.FieldName())9}10func main() {11 fmt.Println(common.FieldName())12}13func main() {14 fmt.Println(common.FieldName())15}16func main() {17 fmt.Println(common.FieldName())18}19func main() {20 fmt.Println(common.FieldName())21}22func main() {23 fmt.Println(common.FieldName())24}25func main() {26 fmt.Println(common.FieldName())27}28func main() {29 fmt.Println(common.FieldName())30}31func main() {32 fmt.Println(common.FieldName())33}34func main() {35 fmt.Println(common.FieldName())36}37func main() {38 fmt.Println(common.FieldName())39}
FieldName
Using AI Code Generation
1import (2func main() {3 fmt.Println(common.FieldName())4}5import (6func main() {7 fmt.Println(common.FieldName())8}9import (10func main() {11 fmt.Println(common.FieldName())12}13import (14func main() {15 fmt.Println(common.FieldName())16}
FieldName
Using AI Code Generation
1import (2func main() {3 fmt.Println(c.FieldName())4}5type Common struct {6}7func (c Common) FieldName() string {8}9import (10func main() {11 fmt.Println(c.FieldName())12}13type Common struct {14}15func (c Common) FieldName() string {16}17import (18func main() {19 fmt.Println(c.FieldName())20}21type Common struct {22}23func (c Common) FieldName() string {24}25import (26func main() {27 fmt.Println(c.FieldName())28}
FieldName
Using AI Code Generation
1import (2func main() {3 fmt.Println(common.FieldName())4}5import (6func FieldName() string {7 var s struct {8 }9 return reflect.TypeOf(s).Field(0).Name10}11I have a package that defines a struct with a field, and a method that returns the name of that field. I want to use that method in a different package, but I don't want to copy the entire struct definition into the other package. I would like to be able to just import the first package and use the method. The method uses reflect, so it can't be a constant.Is there any way to do this without copying the struct definition?Here's a simple example of what I'm trying to do:
FieldName
Using AI Code Generation
1import (2func main() {3 fmt.Println(stringutil.FieldName())4}5import (6func main() {7 fmt.Println(stringutil.FieldName())8}9import (10func main() {11 fmt.Println(common.FieldName())12}13import (14func main() {15 fmt.Println(stringutil.FieldName())16}17import (18func main() {19 fmt.Println(common.FieldName())20}
FieldName
Using AI Code Generation
1import "fmt"2import "github.com/GoLangTraining/common"3func main() {4 fmt.Println(common.FieldName)5}6import "fmt"7import "github.com/GoLangTraining/common"8func main() {9 fmt.Println(common.FieldName)10}11import "fmt"12import "github.com/GoLangTraining/common"13func main() {14 fmt.Println(common.FunctionName())15}16import "fmt"17import "github.com/GoLangTraining/common"18func main() {19 fmt.Println(common.FunctionName())20}21import "fmt"22import "github.com/GoLangTraining/common"23func main() {24 fmt.Println(common.FunctionName())25}26import "fmt"27import "github.com/GoLangTraining/common"28func main() {29 fmt.Println(common.FunctionName())30}31import "fmt"32import "github.com/GoLangTraining/common"33func main() {34 fmt.Println(common.FunctionName())35}
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!!