expr

package
v1.9.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Options = []expr.Option{
	expr.Function(
		"fetch",
		func(params ...any) (any, error) {
			var req *http.Request
			var err error

			url := params[0].(string)

			if len(params) == 2 {
				options := params[1].(map[string]any)
				method, _ := options["method"].(string)
				headers, _ := options["headers"].(map[string]any)
				req, err = newRequest(method, url, headers)
			} else {
				req, err = http.NewRequest("GET", url, nil)
			}

			if err != nil {
				return nil, err
			}

			res, err := tcp.Do(req)
			if err != nil {
				return nil, err
			}

			b, _ := io.ReadAll(res.Body)

			return map[string]any{
				"ok":     res.StatusCode < 400,
				"status": res.Status,
				"text":   string(b),
				"json": func() (v any) {
					_ = json.Unmarshal(b, &v)
					return
				},
			}, nil
		},
	),
	expr.Function(
		"match",
		func(params ...any) (any, error) {
			re, err := regExp(params[1:]...)
			if err != nil {
				return nil, err
			}
			str := params[0].(string)
			return re.FindStringSubmatch(str), nil
		},
	),
	expr.Function(
		"RegExp",
		func(params ...any) (any, error) {
			return regExp(params)
		},
	),
}

Functions

func Run

func Run(input string) (any, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL