1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-02 09:21:54 +02:00

Updated root API (#622)

This commit is contained in:
Tim Voronov 2021-05-04 15:01:27 -04:00 committed by GitHub
parent 4346a91513
commit 4c79cb4343
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 6 deletions

View File

@ -14,9 +14,11 @@ type Instance struct {
drivers *drivers.Container
}
func New() *Instance {
func New(setters ...Option) *Instance {
opts := NewOptions(setters)
return &Instance{
compiler: compiler.New(),
compiler: compiler.New(opts.compiler...),
drivers: drivers.NewContainer(),
}
}
@ -33,6 +35,10 @@ func (i *Instance) Compile(query string) (*runtime.Program, error) {
return i.compiler.Compile(query)
}
func (i *Instance) MustCompile(query string) *runtime.Program {
return i.compiler.MustCompile(query)
}
func (i *Instance) Exec(ctx context.Context, query string, opts ...runtime.Option) ([]byte, error) {
p, err := i.Compile(query)
@ -46,3 +52,13 @@ func (i *Instance) Exec(ctx context.Context, query string, opts ...runtime.Optio
return p.Run(ctx, opts...)
}
func (i *Instance) MustExec(ctx context.Context, query string, opts ...runtime.Option) []byte {
out, err := i.Exec(ctx, query, opts...)
if err != nil {
panic(err)
}
return out
}

2
go.mod
View File

@ -6,7 +6,7 @@ require (
github.com/PuerkitoBio/goquery v1.6.1
github.com/antchfx/htmlquery v1.2.3
github.com/antchfx/xpath v1.1.11
github.com/antlr/antlr4 v0.0.0-20200417160354-8c50731894e0
github.com/antlr/antlr4 v0.0.0-20210503161921-88ad58426b90
github.com/corpix/uarand v0.1.1
github.com/gobwas/glob v0.2.3
github.com/gorilla/css v1.0.0

6
go.sum
View File

@ -10,8 +10,8 @@ github.com/antchfx/htmlquery v1.2.3/go.mod h1:B0ABL+F5irhhMWg54ymEZinzMSi0Kt3I2i
github.com/antchfx/xpath v1.1.6/go.mod h1:Yee4kTMuNiPYJ7nSNorELQMr1J33uOpXDMByNYhvtNk=
github.com/antchfx/xpath v1.1.11 h1:WOFtK8TVAjLm3lbgqeP0arlHpvCEeTANeWZ/csPpJkQ=
github.com/antchfx/xpath v1.1.11/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
github.com/antlr/antlr4 v0.0.0-20200417160354-8c50731894e0 h1:j7MyDjg6pb7A2ziow17FDZ2Oj5vGnJsLyDmjpN4Jkcg=
github.com/antlr/antlr4 v0.0.0-20200417160354-8c50731894e0/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y=
github.com/antlr/antlr4 v0.0.0-20210503161921-88ad58426b90 h1:cAZnSAoHQ/c4jiymsGPsUzDXVot50CtiEMQNQTf7W6o=
github.com/antlr/antlr4 v0.0.0-20210503161921-88ad58426b90/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/codegangsta/cli v1.20.0/go.mod h1:/qJNoX69yVSKu5o4jLyXAENLRyk1uhi7zkbQ3slBdOA=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
@ -101,8 +101,8 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=

29
options.go Normal file
View File

@ -0,0 +1,29 @@
package ferret
import "github.com/MontFerret/ferret/pkg/compiler"
type (
Options struct {
compiler []compiler.Option
}
Option func(opts *Options)
)
func NewOptions(setters []Option) *Options {
res := &Options{
compiler: make([]compiler.Option, 0, 2),
}
for _, setter := range setters {
setter(res)
}
return res
}
func WithoutStdlib() Option {
return func(opts *Options) {
opts.compiler = append(opts.compiler, compiler.WithoutStdlib())
}
}