Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Extending the CLI by loading externally-defined functions #1

Open
cleopatrio opened this issue Feb 6, 2025 · 0 comments
Open

Comments

@cleopatrio
Copy link
Collaborator

cleopatrio commented Feb 6, 2025

Extensions file

// file: extensions/0001.go
package mock_http_extensions

import (
	"fmt"
	"net/http"
	"time"
)

func RequestTransformerFunc(*http.Request) *http.Request { return nil }

func ResponseTransformerFunc(*http.Response) *http.Response { return nil }

func ResponsePassesValidationFunc(req *http.Request, res *http.Response, tests struct {
	HTTP *struct {
		Status  *int
		Headers map[string][]string
		Body    any
	}
	Duration *time.Duration
}) bool {
        var isOk bool

	status := Unwrap[int](tests.HTTP.Status, -100)

	if status != res.StatusCode {
		fmt.Printf("ERROR: expected status %v, but got %v\n", status, res.StatusCode)
	}

	// TODO: Test body and headers

	return isOk
}

func Unwrap[T any](optional *T, d T) T {
	if optional == nil {
		return d
	}
	return *optional
}

Env

# 1 - fatal
# 2 - error
# 3 - warn
# 4 - info
# 5 - debug
LOG_LEVEL=5
PLUGINS_FILEPATH=./extensions/0001.go

Execution log

go run . http -o yaml -f ./examples/0001.yaml
WARN[0000] Loaded extension: ResponsePassesValidationFunc 0x1044dc5a0
WARN[0000] Loaded extension: RequestTransformerFunc 0x1044dc5a0
WARN[0000] Loaded extension: ResponseTransformerFunc 0x1044dc5a0
DEBU[0000] schema.LoadSchema
DEBU[0000] schema.Execute
DEBU[0000] schema.Consume
DEBU[0000] schema.Send
DEBU[0000] schema.(*Request).ParseURL
DEBU[0000] schema.(*Request).ParseURL https://api.datamuse.com/words?ml=ocean
DEBU[0000] schema.(*Request).HeaderMap
DEBU[0001] schema.Send request sent...
DEBU[0001] Response for: Datamuse - words related to `ocean`
DEBU[0001] schema.(*Request).ParseURL
DEBU[0001] schema.(*Request).ParseURL https://api.datamuse.com/words?ml=ocean
DEBU[0001] schema.BodyMarshalFunc application/json
WARN[0001] Running code for: ResponsePassesValidationFunc
WARN[0001] Running tests for: Datamuse - words related to `ocean`

ERROR: expected status 400, but got 200

For more details, check out the extensions directory and how it is used in main.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant