goatfile

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: BSD-3-Clause Imports: 30 Imported by: 0

Documentation

Overview

Package goatfile provides functionalities to unmarshal and parse a Goatfile.

Here you can find the Goatfile specification on which basis this parser in built on. https://github.com/studio-b12/goat/blob/main/docs/goatfile-spec.md

Index

Constants

View Source
const (
	ActionRequest = ActionType(iota + 1)
	ActionLogSection
	ActionExecute
)
View Source
const (
	SectionSetup    = SectionName("setup")
	SectionTests    = SectionName("tests")
	SectionTeardown = SectionName("teardown")
	SectionDefaults = SectionName("defaults")
)
View Source
const FileExtension = "goat"

Variables

View Source
var (
	ErrTemplateAlreadyParsed       = errors.New("request template has already been parsed")
	ErrTemplateAlreadyPreParsed    = errors.New("request template has already been pre-parsed")
	ErrIllegalCharacter            = errors.New("illegal character")
	ErrUnexpected                  = errors.New("unexpected error")
	ErrInvalidStringLiteral        = errors.New("invalid string literal")
	ErrEmptyUsePath                = errors.New("empty use path")
	ErrEmptyCallPath               = errors.New("empty call path")
	ErrInvalidSection              = errors.New("invalid section")
	ErrInvalidLogSection           = errors.New("invalid log section")
	ErrInvalidRequestMethod        = errors.New("invalid request method")
	ErrNoRequestURI                = errors.New("method must be followed by the request URI")
	ErrInvalidToken                = errors.New("invalid token")
	ErrInvalidLiteral              = errors.New("invalid literal")
	ErrInvalidBlockHeader          = errors.New("invalid block header")
	ErrInvalidBlockEntryAssignment = errors.New("block entry must start with an assignment")
	ErrInvalidHeaderKey            = errors.New("header values must start with a key")
	ErrInvalidHeaderSeparator      = errors.New("header key and value must be separated by a colon (:)")
	ErrNoHeaderValue               = errors.New("no header value")
	ErrFollowingImport             = errors.New("failed following import")
	ErrOpenEscapeBlock             = errors.New("open escape block")
	ErrBlockOutOfRequest           = errors.New("blocks must follow after a request head")
	ErrMultiImport                 = errors.New("the impoted resource has already been imported")
	ErrInvalidFileDescriptor       = errors.New("invalid file descriptor")
	ErrSectionDefinedMultiple      = errors.New("the section has been already defined")
	ErrUnclosedGroup               = errors.New("group has not been closed")
	ErrMissingGroup                = errors.New("missing group definition")
)

Functions

func ApplyTemplate added in v0.13.0

func ApplyTemplate(raw string, params any) (string, error)

ApplyTemplate parses the given raw string as a template and applies the given values in params onto it returning the result as string.

If a key in the template is not present in the params, an error will be returned.

func ApplyTemplateBuf added in v0.13.0

func ApplyTemplateBuf(raw string, params any) (*bytes.Buffer, error)

ApplyTemplateBuf parses the given raw string as a template and applies the given values in params onto it returning the result as bytes buffer.

If a key in the template is not present in the params, an error will be returned.

func ApplyTemplateToArray added in v0.13.0

func ApplyTemplateToArray(arr []any, params any) (err error)

ApplyTemplateToArray executes applyTemplate on all string instances in the given array or sub arrays.

func ApplyTemplateToMap added in v0.13.0

func ApplyTemplateToMap(m map[string]any, params any) (err error)

ApplyTemplateToMap executes applyTemplate on all values in the given map.

func Extend added in v0.13.0

func Extend(v string, ext string) string

Extend takes a file path and adds the given extension to it if the path does not end with any file extension.

func IsNoContent added in v0.11.0

func IsNoContent(d Data) bool

Types

type Action added in v0.9.0

type Action interface {
	Type() ActionType
}

Action is used to determine the ActionType of an action definition used to cast the action to the specific Action implementation.

func ActionFromAst added in v1.1.0

func ActionFromAst(act ast.Action, path string) (Action, error)

type ActionType added in v0.9.0

type ActionType int

type Data added in v0.4.0

type Data interface {
	// Reader returns a reader to ther internal
	// data or an error. The returned reader
	// might be nil.
	Reader() (io.Reader, error)
}

Data provides a getter to receive a reader to internal data.

func DataFromAst added in v1.1.0

func DataFromAst(di ast.DataContent, path string) (Data, error)

type Execute added in v0.13.0

type Execute struct {
	File    string
	Params  map[string]any
	Returns map[string]string

	Path string
}

func ExecuteFromAst added in v1.1.0

func ExecuteFromAst(a *ast.Execute, path string) (t Execute, err error)

func (Execute) Type added in v0.13.0

func (t Execute) Type() ActionType

type FileContent added in v0.7.0

type FileContent struct {
	// contains filtered or unexported fields
}

FileContent provides a getter which opens a file with the stored path and returns it as reader.

func (FileContent) Reader added in v0.7.0

func (t FileContent) Reader() (r io.Reader, err error)

type Goatfile

type Goatfile struct {
	Imports []string

	Defaults *Request

	Setup    []Action
	Tests    []Action
	Teardown []Action

	Path string
}

Goatfile holds all sections and their requests.

func FromAst added in v1.1.0

func FromAst(astGf *ast.Goatfile) (gf Goatfile, err error)

func Unmarshal

func Unmarshal(raw string, fileDir string) (gf Goatfile, err error)

Unmarshal takes a raw string of a Goatfile and tries to parse it. Returns the parsed Goatfile.

func (*Goatfile) Merge

func (t *Goatfile) Merge(with Goatfile)

Merge appends all requests in all sections of with to the current Goatfile.

func (Goatfile) String

func (t Goatfile) String() string

String returns the Goatfile as JSON encoded string.

type LogSection added in v0.9.0

type LogSection string

func (LogSection) Type added in v0.9.0

func (t LogSection) Type() ActionType

type NoContent added in v0.7.0

type NoContent struct{}

NoContent implements Data containing no content.

func (NoContent) Reader added in v0.7.0

func (t NoContent) Reader() (io.Reader, error)

type Opts

type Opts struct {
	QueryParams map[string]any
	Options     map[string]any
	Auth        map[string]any
}

Opts holds the specific request options.

type ParameterValue

type ParameterValue string

ParameterValue holds a go template value as string allowing to apply parameters onto it and parsing the result.

func (ParameterValue) ApplyTemplate

func (t ParameterValue) ApplyTemplate(params any) (any, error)

ApplyTemplate applies the passed params onto the teplate value and parses the result using a new instance of Parser as sub-parser.

type ParseError

type ParseError struct {
	errs.InnerError

	Line    int
	LinePos int
}

ParseError wraps an inner error with additional parsing context.

func (ParseError) Error

func (t ParseError) Error() string

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses a Goatfile.

func NewParser

func NewParser(r io.Reader, fileDir string) *Parser

NewParser returns a new Parser scanning from the given Reader.

func (*Parser) Parse

func (t *Parser) Parse() (*ast.Goatfile, error)

Parse parses a Goatfile from the specified source.

type Request

type Request struct {
	Opts

	Method    string
	URI       string
	Header    http.Header
	Body      Data
	PreScript Data
	Script    Data

	Path    string
	PosLine int
	// contains filtered or unexported fields
}

Request holds the specifications for a HTTP request with options and script commands.

func PartialRequestFromAst added in v1.1.0

func PartialRequestFromAst(req ast.PartialRequest, path string) (t Request, err error)

func RequestFromAst added in v1.1.0

func RequestFromAst(req *ast.Request, path string) (t Request, err error)

func (*Request) Merge added in v0.11.0

func (t *Request) Merge(with *Request)

func (*Request) PreSubstitudeWithParams added in v0.15.0

func (t *Request) PreSubstitudeWithParams(params any) error

PreSubstitudeWithParams takes the given parameters and replaces placeholders within specific parts of the request which shall be executed before the actual request is substituted (like PreScript).

func (Request) String

func (t Request) String() string

func (*Request) SubstitudeWithParams added in v0.15.0

func (t *Request) SubstitudeWithParams(params any) error

SubstitudeWithParams takes the given parameters and replaces placeholders within the request with values from the given params.

func (Request) ToHttpRequest

func (t Request) ToHttpRequest() (*http.Request, error)

ToHttpRequest returns a *http.Request built from the given Reuqest.

func (Request) Type added in v0.9.0

func (t Request) Type() ActionType

type SectionName added in v0.13.1

type SectionName string

type StringContent added in v0.7.0

type StringContent string

StringContent stores data as a string.

func (StringContent) Reader added in v0.7.0

func (t StringContent) Reader() (io.Reader, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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