How to use getMapKeys method of lib Package

Best K6 code snippet using lib.getMapKeys

archive_test.go

Source:archive_test.go Github

copy

Full Screen

...71 require.NoError(t, afero.WriteFile(fs, path, data, 0644))72 }73 return fs74}75func getMapKeys(m map[string]afero.Fs) []string {76 keys := make([]string, 0, len(m))77 for key := range m {78 keys = append(keys, key)79 }80 return keys81}82func diffMapFilesystems(t *testing.T, first, second map[string]afero.Fs) bool {83 require.ElementsMatch(t, getMapKeys(first), getMapKeys(second),84 "fs map keys don't match %s, %s", getMapKeys(first), getMapKeys(second))85 for key, fs := range first {86 secondFs := second[key]87 diffFilesystems(t, fs, secondFs)88 }89 return true90}91func diffFilesystems(t *testing.T, first, second afero.Fs) {92 diffFilesystemsDir(t, first, second, "/")93}94func getInfoNames(infos []os.FileInfo) []string {95 var names = make([]string, len(infos))96 for i, info := range infos {97 names[i] = info.Name()98 }...

Full Screen

Full Screen

db.go

Source:db.go Github

copy

Full Screen

1package file2import (3 "errors"4 "fmt"5 "net/http"6 "sort"7 "strings"8 "sync"9 "ehang.io/nps/lib/common"10 "ehang.io/nps/lib/crypt"11 "ehang.io/nps/lib/rate"12)13type DbUtils struct {14 JsonDb *JsonDb15}16var (17 Db *DbUtils18 once sync.Once19)20//init csv from file21func GetDb() *DbUtils {22 once.Do(func() {23 jsonDb := NewJsonDb(common.GetRunPath())24 jsonDb.LoadClientFromJsonFile()25 jsonDb.LoadTaskFromJsonFile()26 jsonDb.LoadHostFromJsonFile()27 Db = &DbUtils{JsonDb: jsonDb}28 })29 return Db30}31func GetMapKeys(m sync.Map, isSort bool, sortKey, order string) (keys []int) {32 if sortKey != "" && isSort {33 return sortClientByKey(m, sortKey, order)34 }35 m.Range(func(key, value interface{}) bool {36 keys = append(keys, key.(int))37 return true38 })39 sort.Ints(keys)40 return41}42func (s *DbUtils) GetClientList(start, length int, search, sort, order string, clientId int) ([]*Client, int) {43 list := make([]*Client, 0)44 var cnt int45 keys := GetMapKeys(s.JsonDb.Clients, true, sort, order)46 for _, key := range keys {47 if value, ok := s.JsonDb.Clients.Load(key); ok {48 v := value.(*Client)49 if v.NoDisplay {50 continue51 }52 if clientId != 0 && clientId != v.Id {53 continue54 }55 if search != "" && !(v.Id == common.GetIntNoErrByStr(search) || strings.Contains(v.VerifyKey, search) || strings.Contains(v.Remark, search)) {56 continue57 }58 cnt++59 if start--; start < 0 {60 if length--; length >= 0 {61 list = append(list, v)62 }63 }64 }65 }66 return list, cnt67}68func (s *DbUtils) GetIdByVerifyKey(vKey string, addr string) (id int, err error) {69 var exist bool70 s.JsonDb.Clients.Range(func(key, value interface{}) bool {71 v := value.(*Client)72 if common.Getverifyval(v.VerifyKey) == vKey && v.Status {73 v.Addr = common.GetIpByAddr(addr)74 id = v.Id75 exist = true76 return false77 }78 return true79 })80 if exist {81 return82 }83 return 0, errors.New("not found")84}85func (s *DbUtils) NewTask(t *Tunnel) (err error) {86 s.JsonDb.Tasks.Range(func(key, value interface{}) bool {87 v := value.(*Tunnel)88 if (v.Mode == "secret" || v.Mode == "p2p") && v.Password == t.Password {89 err = errors.New(fmt.Sprintf("secret mode keys %s must be unique", t.Password))90 return false91 }92 return true93 })94 if err != nil {95 return96 }97 t.Flow = new(Flow)98 s.JsonDb.Tasks.Store(t.Id, t)99 s.JsonDb.StoreTasksToJsonFile()100 return101}102func (s *DbUtils) UpdateTask(t *Tunnel) error {103 s.JsonDb.Tasks.Store(t.Id, t)104 s.JsonDb.StoreTasksToJsonFile()105 return nil106}107func (s *DbUtils) DelTask(id int) error {108 s.JsonDb.Tasks.Delete(id)109 s.JsonDb.StoreTasksToJsonFile()110 return nil111}112//md5 password113func (s *DbUtils) GetTaskByMd5Password(p string) (t *Tunnel) {114 s.JsonDb.Tasks.Range(func(key, value interface{}) bool {115 if crypt.Md5(value.(*Tunnel).Password) == p {116 t = value.(*Tunnel)117 return false118 }119 return true120 })121 return122}123func (s *DbUtils) GetTask(id int) (t *Tunnel, err error) {124 if v, ok := s.JsonDb.Tasks.Load(id); ok {125 t = v.(*Tunnel)126 return127 }128 err = errors.New("not found")129 return130}131func (s *DbUtils) DelHost(id int) error {132 s.JsonDb.Hosts.Delete(id)133 s.JsonDb.StoreHostToJsonFile()134 return nil135}136func (s *DbUtils) IsHostExist(h *Host) bool {137 var exist bool138 s.JsonDb.Hosts.Range(func(key, value interface{}) bool {139 v := value.(*Host)140 if v.Id != h.Id && v.Host == h.Host && h.Location == v.Location && (v.Scheme == "all" || v.Scheme == h.Scheme) {141 exist = true142 return false143 }144 return true145 })146 return exist147}148func (s *DbUtils) NewHost(t *Host) error {149 if t.Location == "" {150 t.Location = "/"151 }152 if s.IsHostExist(t) {153 return errors.New("host has exist")154 }155 t.Flow = new(Flow)156 s.JsonDb.Hosts.Store(t.Id, t)157 s.JsonDb.StoreHostToJsonFile()158 return nil159}160func (s *DbUtils) GetHost(start, length int, id int, search string) ([]*Host, int) {161 list := make([]*Host, 0)162 var cnt int163 keys := GetMapKeys(s.JsonDb.Hosts, false, "", "")164 for _, key := range keys {165 if value, ok := s.JsonDb.Hosts.Load(key); ok {166 v := value.(*Host)167 if search != "" && !(v.Id == common.GetIntNoErrByStr(search) || strings.Contains(v.Host, search) || strings.Contains(v.Remark, search)) {168 continue169 }170 if id == 0 || v.Client.Id == id {171 cnt++172 if start--; start < 0 {173 if length--; length >= 0 {174 list = append(list, v)175 }176 }177 }178 }179 }180 return list, cnt181}182func (s *DbUtils) DelClient(id int) error {183 s.JsonDb.Clients.Delete(id)184 s.JsonDb.StoreClientsToJsonFile()185 return nil186}187func (s *DbUtils) NewClient(c *Client) error {188 var isNotSet bool189 if c.WebUserName != "" && !s.VerifyUserName(c.WebUserName, c.Id) {190 return errors.New("web login username duplicate, please reset")191 }192reset:193 if c.VerifyKey == "" || isNotSet {194 isNotSet = true195 c.VerifyKey = crypt.GetRandomString(16)196 }197 if c.RateLimit == 0 {198 c.Rate = rate.NewRate(int64(2 << 23))199 } else if c.Rate == nil {200 c.Rate = rate.NewRate(int64(c.RateLimit * 1024))201 }202 c.Rate.Start()203 if !s.VerifyVkey(c.VerifyKey, c.Id) {204 if isNotSet {205 goto reset206 }207 return errors.New("Vkey duplicate, please reset")208 }209 if c.Id == 0 {210 c.Id = int(s.JsonDb.GetClientId())211 }212 if c.Flow == nil {213 c.Flow = new(Flow)214 }215 s.JsonDb.Clients.Store(c.Id, c)216 s.JsonDb.StoreClientsToJsonFile()217 return nil218}219func (s *DbUtils) VerifyVkey(vkey string, id int) (res bool) {220 res = true221 s.JsonDb.Clients.Range(func(key, value interface{}) bool {222 v := value.(*Client)223 if v.VerifyKey == vkey && v.Id != id {224 res = false225 return false226 }227 return true228 })229 return res230}231func (s *DbUtils) VerifyUserName(username string, id int) (res bool) {232 res = true233 s.JsonDb.Clients.Range(func(key, value interface{}) bool {234 v := value.(*Client)235 if v.WebUserName == username && v.Id != id {236 res = false237 return false238 }239 return true240 })241 return res242}243func (s *DbUtils) UpdateClient(t *Client) error {244 s.JsonDb.Clients.Store(t.Id, t)245 if t.RateLimit == 0 {246 t.Rate = rate.NewRate(int64(2 << 23))247 t.Rate.Start()248 }249 return nil250}251func (s *DbUtils) IsPubClient(id int) bool {252 client, err := s.GetClient(id)253 if err == nil {254 return client.NoDisplay255 }256 return false257}258func (s *DbUtils) GetClient(id int) (c *Client, err error) {259 if v, ok := s.JsonDb.Clients.Load(id); ok {260 c = v.(*Client)261 return262 }263 err = errors.New("未找到客户端")264 return265}266func (s *DbUtils) GetClientIdByVkey(vkey string) (id int, err error) {267 var exist bool268 s.JsonDb.Clients.Range(func(key, value interface{}) bool {269 v := value.(*Client)270 if crypt.Md5(v.VerifyKey) == vkey {271 exist = true272 id = v.Id273 return false274 }275 return true276 })277 if exist {278 return279 }280 err = errors.New("未找到客户端")281 return282}283func (s *DbUtils) GetHostById(id int) (h *Host, err error) {284 if v, ok := s.JsonDb.Hosts.Load(id); ok {285 h = v.(*Host)286 return287 }288 err = errors.New("The host could not be parsed")289 return290}291//get key by host from x292func (s *DbUtils) GetInfoByHost(host string, r *http.Request) (h *Host, err error) {293 var hosts []*Host294 //Handling Ported Access295 host = common.GetIpByAddr(host)296 s.JsonDb.Hosts.Range(func(key, value interface{}) bool {297 v := value.(*Host)298 if v.IsClose {299 return true300 }301 //Remove http(s) http(s)://a.proxy.com302 //*.proxy.com *.a.proxy.com Do some pan-parsing303 if v.Scheme != "all" && v.Scheme != r.URL.Scheme {304 return true305 }306 tmpHost := v.Host307 if strings.Contains(tmpHost, "*") {308 tmpHost = strings.Replace(tmpHost, "*", "", -1)309 if strings.Contains(host, tmpHost) {310 hosts = append(hosts, v)311 }312 } else if v.Host == host {313 hosts = append(hosts, v)314 }315 return true316 })317 for _, v := range hosts {318 //If not set, default matches all319 if v.Location == "" {320 v.Location = "/"321 }322 if strings.Index(r.RequestURI, v.Location) == 0 {323 if h == nil || (len(v.Location) > len(h.Location)) {324 h = v325 }326 }327 }328 if h != nil {329 return330 }331 err = errors.New("The host could not be parsed")332 return333}...

Full Screen

Full Screen

parse.go

Source:parse.go Github

copy

Full Screen

1package lib2import (3 "github.com/PuerkitoBio/goquery"4 "strings"5)6type ParseUrl struct {7 Document *goquery.Document8}9func NewParse() *ParseUrl {10 return &ParseUrl{11 }12}13func (p *ParseUrl) NewDocument(url string) {14 document, err := goquery.NewDocument(url)15 if err != nil {16 Log.Warnf("parse Document failed url:%s",url)17 return18 }19 p.Document = document20}21func (p *ParseUrl) GetTitle() string {22 if p.Document == nil {23 return ""24 }25 return p.Document.Find("title").Text()26}27func (p *ParseUrl) Size() int {28 return p.Document.Size()29}30func (p *ParseUrl) GetHrefs() []string {31 var urlMap = map[string]interface{}{}32 if p.Document == nil {33 return []string{}34 }35 p.Document.Find("a").Each(func(i int, aa *goquery.Selection) {36 href, ok := aa.Attr("href")37 href = strings.TrimSpace(href)38 _, exist := urlMap[href]39 if ok && IsHtml(href) && IsCrawled(href) && exist == false {40 if strings.HasPrefix(href, "/") || strings.HasPrefix(href, "./") {41 href = SamePathUrl(href, href, 1)42 } else if strings.HasPrefix(href, "../") {43 href = SamePathUrl(href, href, 2)44 }45 urlMap[href] = true46 }47 })48 return GetMapKeys(urlMap)49}50func (p *ParseUrl) GetImages() []string {51 var urlMap = make(map[string]interface{})52 if p.Document == nil {53 return []string{}54 }55 p.Document.Find("img").Each(func(i int, aa *goquery.Selection) {56 href, ok := aa.Attr("src")57 href = strings.TrimSpace(href)58 _, exist := urlMap[href]59 if ok && exist == false && IsImage(href) && IsHtml(href){60 if strings.HasPrefix(href, "/") || strings.HasPrefix(href, "./") {61 href = SamePathUrl(href, href, 1)62 } else if strings.HasPrefix(href, "../") {63 href = SamePathUrl(href, href, 2)64 }65 urlMap[href] = true66 }67 })68 return GetMapKeys(urlMap)69}70func (p *ParseUrl) GetVideos() []string {71 var urlMap = make(map[string]interface{})72 if p.Document == nil {73 return []string{}74 }75 p.Document.Find("video").Each(func(i int, aa *goquery.Selection) {76 href, ok := aa.Attr("src")77 href = strings.TrimSpace(href)78 _, exist := urlMap[href]79 if ok && exist == false && IsImage(href) && IsHtml(href){80 if strings.HasPrefix(href, "/") || strings.HasPrefix(href, "./") {81 href = SamePathUrl(href, href, 1)82 } else if strings.HasPrefix(href, "../") {83 href = SamePathUrl(href, href, 2)84 }85 urlMap[href] = true86 }87 })88 return GetMapKeys(urlMap)89}...

Full Screen

Full Screen

getMapKeys

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 m := map[string]int{4 }5 fmt.Println(lib.GetMapKeys(m))6}7func GetMapKeys(m map[string]int) []string {8 keys := make([]string, len(m))9 for k := range m {10 }11}

Full Screen

Full Screen

getMapKeys

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 keys := lib.GetMapKeys()4 for _, key := range keys {5 fmt.Println(key)6 }7}8import (9func GetMapKeys() []string {10 m = make(map[string]string)11 return reflect.ValueOf(m).MapKeys()12}

Full Screen

Full Screen

getMapKeys

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 map1 = make(map[string]int)4 fmt.Println("map1 keys: ", lib.GetMapKeys(map1))5}6func GetMapKeys(m map[string]int) []string {7 keys := make([]string, 0, len(m))8 for k := range m {9 keys = append(keys, k)10 }11}

Full Screen

Full Screen

getMapKeys

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 m := map[string]string{"k1": "v1", "k2": "v2"}4 fmt.Println(lib.GetMapKeys(m))5}6func GetMapKeys(m map[string]string) []string {7 for k := range m {8 keys = append(keys, k)9 }10}

Full Screen

Full Screen

getMapKeys

Using AI Code Generation

copy

Full Screen

1import (2func main() {3fmt.Println(lib.GetMapKeys(map[string]string{"a": "apple", "b": "ball", "c": "cat"}))4}5func GetMapKeys(m map[string]string) []string {6for key := range m {7keys = append(keys, key)8}9}

Full Screen

Full Screen

getMapKeys

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 map1 := map[string]int {4 }5 keys := lib.GetMapKeys(map1)6 fmt.Println(keys)7}

Full Screen

Full Screen

getMapKeys

Using AI Code Generation

copy

Full Screen

1func main() {2 lib := lib.New()3 lib.GetMapKeys()4}5func (l *Lib) GetMapKeys() {6 m := map[string]int{7 }8 for key := range m {9 fmt.Println(key)10 }11}

Full Screen

Full Screen

getMapKeys

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 m := map[string]int{"k1": 1, "k2": 2}4 fmt.Println(lib.GetMapKeys(m))5}6import (7func GetMapKeys(m interface{}) []string {8 keys := reflect.ValueOf(m).MapKeys()9 result := make([]string, len(keys))10 for i, key := range keys {11 result[i] = key.String()12 }13}

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful