glazed

package
v0.2.11 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewGinHandlerFromCommandHandlers

func NewGinHandlerFromCommandHandlers(
	cmd cmds.GlazeCommand,
	opts *HandleOptions,
) gin.HandlerFunc

NewGinHandlerFromCommandHandlers returns a gin.HandlerFunc that is responsible for running the provided command, parsing the necessary context from the provided handlers. This context is then used to create a cmds.Processor and to provide the necessary parameters and layers to the command, calling Run.

TODO(manuel, 2023-04-16) Here we want to pass handlers that can modify the resulting output For example, take the HTML and add a page around it. Take the response and render it into a template (although that might be able to get done with the standard setup).

NOTE(manuel, 2023-04-16)

func ParseObjectFromFile

func ParseObjectFromFile(c *gin.Context, name string) (map[string]interface{}, error)

ParseObjectFromFile takes a multipart.File named `name` from the request and reads it into a map[string]interface{}. If the file is a JSON file (ends with .json), it will be parsed as JSON, if it ends with .yaml or .yml, it will be parsed as YAML.

func ParseStringFromFile

func ParseStringFromFile(c *gin.Context, name string) (string, error)

ParseStringFromFile takes a multipart.File named `name` from the request and reads it into a string.

func SetupProcessor

func SetupProcessor(pc *CommandContext) (*cmds.GlazeProcessor, error)

SetupProcessor creates a new cmds.GlazeProcessor. It uses the parsed layer glazed if present, and return a simple JsonOutputFormatter and standard glazed processor otherwise.

Types

type CommandContext

type CommandContext struct {
	// Cmd is the command that will be executed
	Cmd cmds.GlazeCommand
	// ParsedLayers contains the map of parsed layers parsed so far
	ParsedLayers map[string]*layers.ParsedParameterLayer
	// ParsedParameters contains the map of parsed parameters parsed so far
	ParsedParameters map[string]interface{}
}

CommandContext keeps the context for execution of a glazed command, and can be worked upon by CommandHandlerFunc.

A CommandContext is progressively built up from the query by passing through a list of registered ParkaHandlerFuncs. These handler functions are registered to a specific route.

func NewCommandContext

func NewCommandContext(cmd cmds.GlazeCommand) *CommandContext

NewCommandContext creates a new CommandContext for the given command.

func (*CommandContext) GetAllParameterDefinitions

func (pc *CommandContext) GetAllParameterDefinitions() map[string]*parameters.ParameterDefinition

GetAllParameterDefinitions returns a map of all parameter definitions for the command. This includes flags, arguments and all layers.

func (*CommandContext) GetAllParameterValues

func (pc *CommandContext) GetAllParameterValues() map[string]interface{}

func (*CommandContext) GetFlagsAndArgumentsParameterDefinitions

func (pc *CommandContext) GetFlagsAndArgumentsParameterDefinitions() map[string]*parameters.ParameterDefinition

type CommandHandlerFunc

type CommandHandlerFunc func(*gin.Context, *CommandContext) error

CommandHandlerFunc mirrors gin's HandlerFunc, but also gets passed a CommandContext. That allows it to reuse data from the gin.Context, most importantly the request itself.

func HandlePrepopulatedParameters

func HandlePrepopulatedParameters(ps map[string]interface{}) CommandHandlerFunc

HandlePrepopulatedParameters sets the given parameters in the CommandContext's ParsedParameters. If any of the given parameters also belong to a layer, they are also set there.

func HandlePrepopulatedParsedLayers

func HandlePrepopulatedParsedLayers(layers_ map[string]*layers.ParsedParameterLayer) CommandHandlerFunc

HandlePrepopulatedParsedLayers sets the given layers in the CommandContext's ParsedLayers, overriding the parameters of any layers that are already present. This means that if a parameter is not set in layers_ but is set in the ParsedLayers, the value in the ParsedLayers will be kept.

func NewCommandHandlerFunc

func NewCommandHandlerFunc(cmd cmds.GlazeCommand, parserHandler *Parser) CommandHandlerFunc

type CreateProcessorFunc

type CreateProcessorFunc func(c *gin.Context, pc *CommandContext) (
	cmds.Processor,
	string,
	error,
)

CreateProcessorFunc is a simple func type to create a cmds.GlazeProcessor and formatters.OutputFormatter out of a CommandContext.

type HandleOption

type HandleOption func(*HandleOptions)

func WithCreateProcessor

func WithCreateProcessor(createProcessor CreateProcessorFunc) HandleOption

func WithHandlers

func WithHandlers(handlers ...CommandHandlerFunc) HandleOption

func WithParserOptions

func WithParserOptions(parserOptions ...ParserOption) HandleOption

type HandleOptions

type HandleOptions struct {
	ParserOptions   []ParserOption
	Handlers        []CommandHandlerFunc
	CreateProcessor CreateProcessorFunc
}

func NewHandleOptions

func NewHandleOptions(options []HandleOption) *HandleOptions

type Parser

type Parser struct {
	Parsers            []ParserFunc
	LayerParsersBySlug map[string][]ParserFunc
}

Parser is contains a list of ParserFunc that are used to parse an incoming request into a proper CommandContext, and ultimately be used to Run a glazed Command.

These ParserFunc can be operating on the general parameters as well as per layer. The flexibility is there so that more complicated commands can ultimately be built that leverage different validations and rewrite rules.

NOTE(manuel, 2023-04-16) I wonder when I will queue multiple ParserFunc and LayerParser Func. We might actually already do this by leveraging it to overwrite layer parameters (say, sqleton connection parameters).

func NewCommandFormParser

func NewCommandFormParser(cmd cmds.GlazeCommand, options ...ParserOption) *Parser

func NewCommandQueryParser

func NewCommandQueryParser(cmd cmds.GlazeCommand, options ...ParserOption) *Parser

func NewParser

func NewParser(options ...ParserOption) *Parser

type ParserFunc

type ParserFunc func(
	c *gin.Context,
	ps map[string]interface{},
	pds map[string]*parameters.ParameterDefinition,
) (map[string]*parameters.ParameterDefinition, error)

ParserFunc is used to parse parameters out of a gin.Context (meaning most certainly out of an incoming *http.Request). These parameters are stored in the hashmap `ps`, according to the parameter definitions in `pds`.

If a parameter definition shouldn't be handled by a follow up step, return a new hashmap with the key deleted.

func NewFormParserFunc

func NewFormParserFunc(onlyDefined bool) ParserFunc

NewFormParserFunc returns a ParserFunc that takes an incoming multipart Form, and can thus also handle uploaded files.

func NewQueryParserFunc

func NewQueryParserFunc(onlyDefined bool) ParserFunc

NewQueryParserFunc returns a ParserFunc that can handle an incoming GET query string. If the parameter is supposed to be read from a file, we will just pass in the query parameter's value.

func NewStaticLayerParserFunc

func NewStaticLayerParserFunc(l layers.ParameterLayer) ParserFunc

NewStaticLayerParserFunc returns a parser that adds the given static parameters to the parameter map, based on the parameters of the given layer.

NOTE(manuel, 2023-04-16) How this actually relate to the ParkaContext...

func NewStaticParserFunc

func NewStaticParserFunc(ps map[string]interface{}) ParserFunc

NewStaticParserFunc returns a parser that adds the given static parameters to the parameter map.

type ParserOption

type ParserOption func(*Parser)

func WithAppendLayerParser

func WithAppendLayerParser(slug string, ps ...ParserFunc) ParserOption

func WithAppendParser

func WithAppendParser(ps ...ParserFunc) ParserOption

func WithCustomizedParameterLayerParser

func WithCustomizedParameterLayerParser(l layers.ParameterLayer, overrides map[string]interface{}) ParserOption

func WithGlazeOutputParserOption

func WithGlazeOutputParserOption(gl *cli.GlazedParameterLayers, output string, tableFormat string) ParserOption

func WithPrependLayerParser

func WithPrependLayerParser(slug string, ps ...ParserFunc) ParserOption

func WithPrependParser

func WithPrependParser(ps ...ParserFunc) ParserOption

NOTE(manuel, 2023-04-16) This might be better called WithPrependParserFunc ? What is a better name for ParserFunc.

func WithReplaceLayerParser

func WithReplaceLayerParser(slug string, ps ...ParserFunc) ParserOption

func WithReplaceParser

func WithReplaceParser(ps ...ParserFunc) ParserOption

func WithStaticLayer

func WithStaticLayer(slug string, overrides map[string]interface{}) ParserOption

Jump to

Keyboard shortcuts

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