glazed

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2023 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateJSONProcessor added in v0.2.18

func CreateJSONProcessor(_ *gin.Context, pc *CommandContext) (
	processor.Processor,
	string,
	error,
)

func GinHandleGlazedCommand added in v0.2.13

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

GinHandleGlazedCommand 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 GinHandleGlazedCommandWithOutputFile added in v0.2.13

func GinHandleGlazedCommandWithOutputFile(
	cmd cmds.GlazeCommand,
	outputFile string,
	fileName string,
	opts *HandleOptions,
) gin.HandlerFunc

GinHandleGlazedCommandWithOutputFile returns a gin.HandlerFunc that is responsible for running the GlazeCommand, and then returning the output file as an attachment. This usually requires the caller to provide a temporary file path.

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, options ...processor.GlazeProcessorOption) (*processor.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() []*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() []*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

NewCommandHandlerFunc creates a CommandHandlerFunc using the given Parser struct. This first establishes a set of defaults by loading them from an alias definition.

When the CommandHandler is invoked, we first gather all the parameterDefinitions from the cmd (fresh on every invocation, because the parsers are allowed to modify them).

type CreateProcessorFunc

type CreateProcessorFunc func(c *gin.Context, pc *CommandContext) (
	processor.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

func WithWriter added in v0.2.26

func WithWriter(w io.Writer) HandleOption

type HandleOptions

type HandleOptions struct {
	// ParserOptions are passed to the given parser (the thing that gathers the glazed.Command
	// flags and arguments.
	ParserOptions []ParserOption

	// Handlers are run right at the start to build up the CommandContext based on the
	// gin.Context and the previous value of CommandContext.
	//
	// TODO(manuel, 2023-06-04) It is unclear how CommandHandler and Parser interact, since
	// NewCommandHandlerFunc takes a parser (see HandleSimpleQueryOutputFileCommand and HandleSimpleQueryCommand)
	Handlers []CommandHandlerFunc

	// CreateProcessor takes a gin.Context and a CommandContext and returns a processor.Processor (and a content-type)
	CreateProcessor CreateProcessorFunc

	// This is the actual gin output writer
	Writer io.Writer
}

HandleOptions groups all the settings for a gin handler that handles a glazed command.

func NewHandleOptions

func NewHandleOptions(options []HandleOption) *HandleOptions

func (*HandleOptions) Copy added in v0.2.26

func (h *HandleOptions) Copy(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,

	defaults map[string]string,
	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 NewDefaultsFromLayerParserFunc added in v0.2.28

func NewDefaultsFromLayerParserFunc(l layers.ParameterLayer) ParserFunc

NewDefaultsFromLayerParserFunc returns a parser that adds the default values from the given layers.ParameterLayer to the parameter map (if they haven't been set in the `defaults` map first, which needs to be replaced anyway.

NOTE(manuel, 2023-04-16) How this actually relate to the ParkaContext... NOTE(manuel, 2023-05-25) We shouldn't be dealing with the `defaults` map here

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 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

WithAppendLayerParser adds the given ParserFunc to the end of the list of layer parsers. Be mindful that this can later on be overwritten by a WithReplaceLayerParser.

func WithAppendOverrideLayer added in v0.2.28

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

WithAppendOverrideLayer is a convenience function to override the parameters of a layer. The overrides are appended past currently present parser functions.

func WithAppendParser

func WithAppendParser(ps ...ParserFunc) ParserOption

WithAppendParser adds the given ParserFunc to the end of the list of parsers. Be mindful that this can later on be overwritten by a WithReplaceParser.

func WithGlazeOutputParserOption

func WithGlazeOutputParserOption(output string, tableFormat string) ParserOption

WithGlazeOutputParserOption is a convenience function to override the output and table format glazed settings.

func WithPrependLayerParser

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

WithPrependLayerParser adds the given ParserFunc to the beginning of the list of layer parsers. Be mindful that this can later on be overwritten by a WithReplaceLayerParser.

func WithPrependParser

func WithPrependParser(ps ...ParserFunc) ParserOption

WithPrependParser adds the given ParserFunc to the beginning of the list of parsers. Be mindful that this can later on be overwritten by a WithReplaceParser.

func WithReplaceCustomizedParameterLayerParser added in v0.2.28

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

WithReplaceCustomizedParameterLayerParser adds a DefaultFromLayerParserFunc tweaked by the given `overrides` map. This entirely replaces the current layer parser, but can later on be amended with other parsers, for example with WithAppendOverrideLayer.

func WithReplaceLayerParser

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

WithReplaceLayerParser replaces the list of layer parsers with the given ParserFunc.

func WithReplaceParser

func WithReplaceParser(ps ...ParserFunc) ParserOption

WithReplaceParser replaces the list of parsers with the given ParserFunc. This will remove all previously added prepend, replace, append parsers.

func WithReplaceStaticLayer added in v0.2.28

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

WithReplaceStaticLayer is a convenience function to use static layer parsing. This entirely replaces current layer parsers, but can later on be amended with other parsers, for example with WithAppendOverrideLayer.

Jump to

Keyboard shortcuts

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