dsl

package
v0.17.2 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2024 License: Apache-2.0 Imports: 27 Imported by: 0

README

Hops DSL

The hops dsl package provides definitions of the hops syntax and parsing logic.

Core functions and expressions within the syntax are handled here, though dispatch and orchestration logic are not.

Documentation

Overview

Package schema defines the schema, parsing, validation and evaluation logic for .hops files and automations

Index

Constants

View Source
const (
	BlockIDOn       = "on"
	BlockIDCall     = "call"
	BlockIDDone     = "done"
	BlockIDTask     = "task"
	BlockIDParam    = "param"
	BlockIDSchedule = "schedule"
)
View Source
const (
	TagValidateCron            = "standard_cron"
	TagValidateLabel           = "block_label"
	ValidateLabelMaxLen        = 50
	InvalidRequired     string = "Required"
	InvalidNotString    string = "Should be a string"
	InvalidNotText      string = "Should be text"
	InvalidNotNumber    string = "Should be a number"
	InvalidNotBool      string = "Should be a boolean"
)
View Source
const HopsMetaKey = "hops"

Variables

View Source
var (
	SchemaCall, _     = gohcl.ImpliedBodySchema(CallAST{})
	SchemaDone, _     = gohcl.ImpliedBodySchema(DoneAST{})
	SchemaHops, _     = gohcl.ImpliedBodySchema(HopsAST{})
	SchemaOn, _       = gohcl.ImpliedBodySchema(OnAST{})
	SchemaParam, _    = gohcl.ImpliedBodySchema(ParamAST{})
	SchemaSchedule, _ = gohcl.ImpliedBodySchema(ScheduleAST{})
	SchemaTask, _     = gohcl.ImpliedBodySchema(TaskAST{})
)
View Source
var ManifestNames = []string{"manifest.yaml", "manifest.yml"}

Functions

func BlockErrsToDiagnostics added in v0.17.0

func BlockErrsToDiagnostics(ast hclReader, errs validator.ValidationErrors) hcl.Diagnostics

BlockErrsToDiagnostics converts validation errors into hcl.Diagnostics

Note that this _only_ works for hcl attributes and labels, not block fields. Labels must have a name starting with `label` e.g. `hcl:"label,label" or hcl:"label_1,label"`

func CreateSourceEvent deprecated

func CreateSourceEvent(rawEvent map[string]any, source string, event string, action string) ([]byte, string, error)

Deprecated: Use github.com/hiphops-io/hops/nats CreateSourceEvent

func EvaluateBoolExpression added in v0.17.0

func EvaluateBoolExpression(expr hcl.Expression, unsetVal bool, evalCtx *hcl.EvalContext) (bool, hcl.Diagnostics)

func EvaluateGenericExpression added in v0.17.0

func EvaluateGenericExpression[T any](expr hcl.Expression, evalCtx *hcl.EvalContext) (T, bool, hcl.Diagnostics)

func EvaluateInputsExpression added in v0.17.0

func EvaluateInputsExpression(expr hcl.Expression, evalCtx *hcl.EvalContext) ([]byte, hcl.Diagnostics)

func HCLTagName added in v0.17.0

func HCLTagName(fl reflect.StructField) string

func ValidateCron added in v0.17.0

func ValidateCron(fl validator.FieldLevel) bool

func ValidateDir added in v0.17.0

func ValidateDir(automationDir string, pretty bool) error

ValidateDir validates a given automation dir and prints the result to the console

This is intended for end users to validate their automations are syntactically correct

func ValidateLabel added in v0.17.0

func ValidateLabel(fl validator.FieldLevel) bool

func ValidateTaskInput added in v0.17.0

func ValidateTaskInput(t *TaskAST, input map[string]any) map[string][]string

ValidateTaskInput validates a struct of param inputs against a task

Returns a map of parameter names with an array of validation error messages if any. Map will be empty (but not nil) if all input is valid.

Types

type AutomationFile added in v0.17.0

type AutomationFile struct {
	Path    string
	Content []byte
}

type Automations added in v0.17.0

type Automations struct {
	Files     map[string][]byte
	Hash      string
	Hops      *HopsAST
	Manifests map[string]*Manifest
}

func NewAutomations added in v0.17.0

func NewAutomations(files []*AutomationFile) (*Automations, hcl.Diagnostics)

func NewAutomationsFromContent added in v0.17.0

func NewAutomationsFromContent(contents map[string][]byte) (*Automations, hcl.Diagnostics)

func NewAutomationsFromDir added in v0.17.0

func NewAutomationsFromDir(dirPath string) (*Automations, hcl.Diagnostics, error)

func (*Automations) EventCalls added in v0.17.0

func (a *Automations) EventCalls(callASTs []*CallAST, evalCtx *hcl.EvalContext) ([]*Call, hcl.Diagnostics)

EventCalls evaluates the calls for an on block against a populated event bundle returning all calls that have their conditions for dispatch met.

func (*Automations) EventDefaultDone added in v0.17.0

func (a *Automations) EventDefaultDone(calls []*Call, responseVars map[string]cty.Value) *Done

EventDefaultDone checks if an on block is done by default, regardless of explicit done blocks.

An on block becomes done if all dispatchable calls already have results

func (*Automations) EventDone added in v0.17.0

func (a *Automations) EventDone(doneASTs []*DoneAST, evalCtx *hcl.EvalContext) (*Done, hcl.Diagnostics)

EventDone returns the first done block that is either errored or completed, or nil

func (*Automations) EventOns added in v0.17.0

func (a *Automations) EventOns(eventBundle map[string][]byte) ([]*On, hcl.Diagnostics)

func (*Automations) GetSchedules added in v0.17.0

func (a *Automations) GetSchedules() []*ScheduleAST

func (*Automations) GetTask added in v0.17.0

func (a *Automations) GetTask(label string) (*TaskAST, error)

func (*Automations) GetTasks added in v0.17.0

func (a *Automations) GetTasks() []*TaskAST

func (*Automations) GetTasksInPath added in v0.17.0

func (a *Automations) GetTasksInPath(path string) []*TaskAST

type Call added in v0.17.0

type Call struct {
	Inputs []byte
	*CallAST
}

func (Call) Block added in v0.17.0

func (h Call) Block() *hcl.Block

type CallAST

type CallAST struct {
	Label      string         `json:"label" hcl:"label,label" validate:"block_label"`
	IfExpr     hcl.Expression `json:"-" hcl:"if,optional"`
	InputsExpr hcl.Expression `json:"-" hcl:"inputs,optional"`
	Name       string         `json:"name,omitempty" hcl:"name,optional"`
	Slug       string         `json:"-"`
	// contains filtered or unexported fields
}

func (*CallAST) Block added in v0.17.0

func (h *CallAST) Block() *hcl.Block

type DiagnosticResult added in v0.17.0

type DiagnosticResult struct {
	Severity    hcl.DiagnosticSeverity `json:"Severity"`
	Summary     string                 `json:"Summary"`
	Detail      string                 `json:"Detail,omitempty"`
	Subject     *hcl.Range             `json:"Subject,omitempty"`
	Context     *hcl.Range             `json:"Context,omitempty"`
	Expression  hcl.Expression         `json:"-"`
	EvalContext *hcl.EvalContext       `json:"-"`
	Extra       interface{}            `json:"Extra,omitempty"`
}

DiagnosticResult mirrors hcl.Diagnostic + json tags to control marshalling We keep uppercase field names as this matches the runtime logged diagnostics

type Done added in v0.17.0

type Done struct {
	Completed bool
	Errored   bool
	*DoneAST
}

type DoneAST added in v0.6.0

type DoneAST struct {
	Completed     bool           `json:"completed,omitempty"`
	CompletedExpr hcl.Expression `json:"-" hcl:"completed,optional"`
	Errored       bool           `json:"errored,omitempty"`
	ErroredExpr   hcl.Expression `json:"-" hcl:"errored,optional"`
}

type EvaluationCtx added in v0.17.0

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

func NewEvaluationCtx added in v0.17.0

func NewEvaluationCtx(automations *Automations, variables map[string]cty.Value) *EvaluationCtx

func (*EvaluationCtx) BlockScopedEvalContext added in v0.17.0

func (e *EvaluationCtx) BlockScopedEvalContext(block *hcl.Block, slug string) *hcl.EvalContext

func (*EvaluationCtx) EvalContext added in v0.17.0

func (e *EvaluationCtx) EvalContext() *hcl.EvalContext

type FileType added in v0.17.0

type FileType int
const (
	FileTypeHops FileType = iota
	FileTypeManifest
	FileTypeOther
)

type Hops added in v0.17.0

type Hops struct {
	Ons []*OnAST
	*HopsAST
}

type HopsAST added in v0.17.0

type HopsAST struct {
	Ons       []*OnAST       `json:"ons,omitempty" hcl:"on,block"`
	Schedules []*ScheduleAST `json:"schedules,omitempty" hcl:"schedule,block"`
	Tasks     []*TaskAST     `json:"tasks,omitempty" hcl:"task,block"`
	Body      hcl.Body       `json:"-" hcl:",body"`
	// contains filtered or unexported fields
}

func DecodeToHopsAST added in v0.17.0

func DecodeToHopsAST(body hcl.Body, evaluationCtx *EvaluationCtx) (*HopsAST, hcl.Diagnostics)

DecodeToHopsAST takes a parsed hcl.File and partially decodes it into an AST

Any diagnostic errors will be gathered and returned rather than causing early exit. This means the returned HopsAST may be partially populated. This function will not evaluate runtime expressions (e.g. 'if' statements).

func (*HopsAST) DecodeCallAST added in v0.17.0

func (h *HopsAST) DecodeCallAST(call *CallAST, slugPrefix string, idx int) hcl.Diagnostics

func (*HopsAST) DecodeHopsAST added in v0.17.0

func (h *HopsAST) DecodeHopsAST() hcl.Diagnostics

func (*HopsAST) DecodeOnAST added in v0.17.0

func (h *HopsAST) DecodeOnAST(on *OnAST, idx int) hcl.Diagnostics

func (*HopsAST) DecodeParamAST added in v0.17.0

func (h *HopsAST) DecodeParamAST(param *ParamAST, namePrefix string) hcl.Diagnostics

func (*HopsAST) DecodeScheduleAST added in v0.17.0

func (h *HopsAST) DecodeScheduleAST(schedule *ScheduleAST) hcl.Diagnostics

func (*HopsAST) DecodeTaskAST added in v0.17.0

func (h *HopsAST) DecodeTaskAST(task *TaskAST) hcl.Diagnostics

type HopsValidator added in v0.17.0

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

func NewHopsValidator added in v0.17.0

func NewHopsValidator() *HopsValidator

func (*HopsValidator) BlockStruct added in v0.17.0

func (h *HopsValidator) BlockStruct(ast hclReader) hcl.Diagnostics

func (*HopsValidator) SlugRegister added in v0.17.0

func (h *HopsValidator) SlugRegister(slugRegister []slugRange) hcl.Diagnostics

type Manifest added in v0.17.0

type Manifest struct {
	Description  string         `json:"description" validate:"omitempty,gte=1"`
	Emoji        string         `json:"emoji"`
	Name         string         `json:"name" validate:"required,gte=1"`
	RequiredApps []string       `json:"required_apps"`
	Steps        []ManifestStep `json:"steps"`
	Tags         []string       `json:"tags"`
	Version      string         `json:"version" validate:"required,gte=1"`
}

func NewManifest added in v0.17.0

func NewManifest(content []byte) (*Manifest, error)

type ManifestStep added in v0.17.0

type ManifestStep struct {
	Title        string `json:"title" validate:"required,gte=1"`
	Instructions string `json:"instructions"`
	Emoji        string `json:"emoji"`
}

type On added in v0.17.0

type On struct {
	Calls []*Call
	Done  *Done
	*OnAST
}

func (On) Block added in v0.17.0

func (h On) Block() *hcl.Block

type OnAST

type OnAST struct {
	Calls  []*CallAST     `json:"calls,omitempty" hcl:"call,block"`
	Done   []*DoneAST     `json:"done,omitempty" hcl:"done,block"`
	Label  string         `json:"label" hcl:"label,label" validate:"block_label"`
	IfExpr hcl.Expression `json:"-" hcl:"if,optional"`
	Name   string         `json:"name,omitempty" hcl:"name,optional"`
	Slug   string         `json:"-"`
	// contains filtered or unexported fields
}

func (*OnAST) Block added in v0.17.0

func (h *OnAST) Block() *hcl.Block

type ParamAST

type ParamAST struct {
	Default     any            `json:"default,omitempty"`
	DefaultExpr hcl.Expression `json:"-" hcl:"default,optional"`
	DisplayName string         `json:"display_name,omitempty" hcl:"display_name,optional"`
	Flag        string         `json:"flag,omitempty" hcl:"flag,optional"`
	Help        string         `json:"help,omitempty" hcl:"help,optional"`
	Name        string         `json:"name" hcl:"label,label" validate:"block_label"`
	Required    bool           `json:"required" hcl:"required,optional"`
	ShortFlag   string         `json:"shortflag,omitempty" hcl:"shortflag,optional"`
	Type        string         `json:"type" hcl:"type,optional" validate:"omitempty,oneof=string text number bool"`
	// contains filtered or unexported fields
}

func (*ParamAST) Block added in v0.17.0

func (h *ParamAST) Block() *hcl.Block

type ScheduleAST added in v0.7.0

type ScheduleAST struct {
	Cron       string         `json:"cron" hcl:"cron,attr" validate:"standard_cron"`
	Inputs     []byte         `json:"inputs,omitempty"`
	InputsExpr hcl.Expression `json:"-" hcl:"inputs,optional"`
	Name       string         `json:"name" hcl:"label,label" validate:"block_label"`
	// contains filtered or unexported fields
}

func (*ScheduleAST) Block added in v0.17.0

func (h *ScheduleAST) Block() *hcl.Block

type SourceMeta

type SourceMeta struct {
	Source string `json:"source"`
	Event  string `json:"event"`
	Action string `json:"action"`
}

type TaskAST

type TaskAST struct {
	Description string      `json:"description,omitempty" hcl:"description,optional"`
	DisplayName string      `json:"display_name,omitempty" hcl:"display_name,optional"`
	Emoji       string      `json:"emoji,omitempty" hcl:"emoji,optional"`
	Name        string      `json:"name" hcl:"label,label" validate:"block_label"`
	Params      []*ParamAST `json:"params,omitempty" hcl:"param,block"`
	Summary     string      `json:"summary,omitempty" hcl:"summary,optional"`
	FilePath    string      `json:"filepath"`
	// contains filtered or unexported fields
}

func (*TaskAST) Block added in v0.17.0

func (h *TaskAST) Block() *hcl.Block

type ValidationResult added in v0.17.0

type ValidationResult struct {
	Diagnostics map[string][]DiagnosticResult `json:"diagnostics"`
	FileCount   int                           `json:"file_count"`
	IsValid     bool                          `json:"is_valid"`
	NumIssues   int                           `json:"num_issues"`
	ReadError   string                        `json:"read_error,omitempty"`
}

Directories

Path Synopsis
c matches character c (c != '\\', '-', ']') '\\' c matches character c lo '-' hi matches character c for lo <= c <= hi
c matches character c (c != '\\', '-', ']') '\\' c matches character c lo '-' hi matches character c for lo <= c <= hi

Jump to

Keyboard shortcuts

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