-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from szkiba/feature/builtin
feat: goquery support
- Loading branch information
Showing
7 changed files
with
148 additions
and
4 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
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
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,44 @@ | ||
// SPDX-FileCopyrightText: 2023 Iván Szkiba | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
package goquery | ||
|
||
import ( | ||
"net/http" | ||
"reflect" | ||
|
||
"github.com/PuerkitoBio/goquery" | ||
"github.com/imdario/mergo" | ||
"github.com/szkiba/xk6-g0/internal/builtin/stdlib" | ||
"github.com/traefik/yaegi/interp" | ||
"go.k6.io/k6/js/modules" | ||
) | ||
|
||
var Symbols = interp.Exports{} | ||
|
||
//go:generate yaegi extract -name goquery github.com/PuerkitoBio/goquery | ||
|
||
func Exports(vu modules.VU) interp.Exports { | ||
newDocument := func(url string) (*goquery.Document, error) { | ||
client := &http.Client{Transport: stdlib.NewTransport(vu)} | ||
|
||
resp, err := client.Get(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return goquery.NewDocumentFromResponse(resp) | ||
} | ||
exports := interp.Exports{ | ||
"github.com/PuerkitoBio/goquery/goquery": { | ||
"NewDocument": reflect.ValueOf(newDocument), | ||
}, | ||
} | ||
|
||
if err := mergo.Merge(&exports, Symbols); err != nil { | ||
panic(err) | ||
} | ||
|
||
return exports | ||
} |
52 changes: 52 additions & 0 deletions
52
internal/builtin/goquery/github_com-PuerkitoBio-goquery.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/PuerkitoBio/goquery" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func Default() error { | ||
doc, err := goquery.NewDocument("https://test.k6.io") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logrus.Info(doc.Find("h1.title span.text-blue").Text()) | ||
|
||
return nil | ||
} |