caveats

package
v0.0.3 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertContextToParameters

func ConvertContextToParameters(
	contextMap map[string]any,
	parameterTypes map[string]*core.CaveatTypeReference,
	unknownParametersOption UnknownParameterOption,
) (map[string]any, error)

ConvertContextToParameters converts the given context into parameters of the types specified. Returns a type error if type conversion failed.

func ConvertContextToStruct

func ConvertContextToStruct(contextValues map[string]any) (*structpb.Struct, error)

ConvertContextToStruct converts the given context values into a context struct.

func NewSource

func NewSource(expressionString string, name string) (common.Source, error)

NewSource creates a new source for compilation into a caveat.

func ParameterTypeString

func ParameterTypeString(typeRef *core.CaveatTypeReference) string

ParameterTypeString returns the string form of the type reference.

func StableContextStringForHashing

func StableContextStringForHashing(context *structpb.Struct) string

StableContextStringForHashing returns a stable string version of the context, for use in hashing.

Types

type CaveatResult

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

CaveatResult holds the result of evaluating a caveat.

func EvaluateCaveat

func EvaluateCaveat(caveat *CompiledCaveat, contextValues map[string]any) (*CaveatResult, error)

EvaluateCaveat evaluates the compiled caveat with the specified values, and returns the result or an error.

func EvaluateCaveatWithConfig

func EvaluateCaveatWithConfig(caveat *CompiledCaveat, contextValues map[string]any, config *EvaluationConfig) (*CaveatResult, error)

EvaluateCaveatWithConfig evaluates the compiled caveat with the specified values, and returns the result or an error.

func (CaveatResult) ContextStruct

func (cr CaveatResult) ContextStruct() (*structpb.Struct, error)

ContextStruct returns the context values used when computing this result as a structpb.

func (CaveatResult) ContextValues

func (cr CaveatResult) ContextValues() map[string]any

ContextValues returns the context values used when computing this result.

func (CaveatResult) ExpressionString

func (cr CaveatResult) ExpressionString() (string, error)

ExpressionString returns the human-readable expression string for the evaluated expression.

func (CaveatResult) IsPartial

func (cr CaveatResult) IsPartial() bool

IsPartial returns true if the caveat was only partially evaluated.

func (CaveatResult) MissingVarNames

func (cr CaveatResult) MissingVarNames() ([]string, error)

MissingVarNames returns the name(s) of the missing variables.

func (CaveatResult) ParentCaveat added in v0.0.3

func (cr CaveatResult) ParentCaveat() *CompiledCaveat

ParentCaveat returns the caveat that was evaluated to produce this result.

func (CaveatResult) PartialValue

func (cr CaveatResult) PartialValue() (*CompiledCaveat, error)

PartialValue returns the partially evaluated caveat. Only applies if IsPartial is true.

func (CaveatResult) Value

func (cr CaveatResult) Value() bool

Value returns the computed value for the result.

type CompilationErrors

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

CompilationErrors is a wrapping error for containing compilation errors for a Caveat.

func (CompilationErrors) ColumnPosition

func (err CompilationErrors) ColumnPosition() int

ColumnPositionis the 0-indexed column position for compilation error.

func (CompilationErrors) DetailsMetadata

func (err CompilationErrors) DetailsMetadata() map[string]string

DetailsMetadata returns the metadata for details for this error.

func (CompilationErrors) LineNumber

func (err CompilationErrors) LineNumber() int

LineNumber is the 0-indexed line number for compilation error.

func (CompilationErrors) MarshalZerologObject

func (err CompilationErrors) MarshalZerologObject(e *zerolog.Event)

MarshalZerologObject implements zerolog.LogObjectMarshaler

type CompiledCaveat

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

CompiledCaveat is a compiled form of a caveat.

func CompileCaveatWithName

func CompileCaveatWithName(env *Environment, exprString, name string) (*CompiledCaveat, error)

CompileCaveatWithName compiles a caveat string into a compiled caveat with a given name, or returns the compilation errors.

func CompileCaveatWithSource

func CompileCaveatWithSource(env *Environment, name string, source common.Source, startPosition SourcePosition) (*CompiledCaveat, error)

CompileCaveatWithSource compiles a caveat source into a compiled caveat, or returns the compilation errors.

func DeserializeCaveat

func DeserializeCaveat(serialized []byte, parameterTypes map[string]types.VariableType) (*CompiledCaveat, error)

DeserializeCaveat deserializes a byte-serialized caveat back into a CompiledCaveat.

func (CompiledCaveat) ExprString

func (cc CompiledCaveat) ExprString() (string, error)

ExprString returns the string-form of the caveat.

func (CompiledCaveat) Name

func (cc CompiledCaveat) Name() string

Name represents a user-friendly reference to a caveat

func (CompiledCaveat) ReferencedParameters

func (cc CompiledCaveat) ReferencedParameters(parameters []string) (*mapz.Set[string], error)

ReferencedParameters returns the names of the parameters referenced in the expression.

func (CompiledCaveat) RewriteVariable added in v0.0.3

func (cc CompiledCaveat) RewriteVariable(oldName, newName string) (CompiledCaveat, error)

RewriteVariable replaces the use of a variable with another variable in the compiled caveat.

func (CompiledCaveat) Serialize

func (cc CompiledCaveat) Serialize() ([]byte, error)

Serialize serializes the compiled caveat into a byte string for storage.

type Environment

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

Environment defines the evaluation environment for a caveat.

func EnvForVariables

func EnvForVariables(vars map[string]types.VariableType) (*Environment, error)

EnvForVariables returns a new environment constructed for the given variables.

func MustEnvForVariables

func MustEnvForVariables(vars map[string]types.VariableType) *Environment

MustEnvForVariables returns a new environment constructed for the given variables or panics.

func NewEnvironment

func NewEnvironment() *Environment

NewEnvironment creates and returns a new environment for compiling a caveat.

func (*Environment) AddVariable

func (e *Environment) AddVariable(name string, varType types.VariableType) error

AddVariable adds a variable with the given type to the environment.

func (*Environment) EncodedParametersTypes

func (e *Environment) EncodedParametersTypes() map[string]*core.CaveatTypeReference

EncodedParametersTypes returns the map of encoded parameters for the environment.

type EvaluationConfig

type EvaluationConfig struct {
	// MaxCost is the max cost of the caveat to be executed.
	MaxCost uint64
}

EvaluationConfig is configuration given to an EvaluateCaveatWithConfig call.

type EvaluationErr

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

EvaluationErr is an error in evaluation of a caveat expression.

func (EvaluationErr) DetailsMetadata

func (err EvaluationErr) DetailsMetadata() map[string]string

DetailsMetadata returns the metadata for details for this error.

func (EvaluationErr) MarshalZerologObject

func (err EvaluationErr) MarshalZerologObject(e *zerolog.Event)

MarshalZerologObject implements zerolog.LogObjectMarshaler

type HashableContext

type HashableContext struct{ *structpb.Struct }

HashableContext is a wrapper around a context Struct that provides hashing.

func (HashableContext) AppendToHash

func (hc HashableContext) AppendToHash(hasher HasherInterface)

type HasherInterface

type HasherInterface interface {
	WriteString(value string)
}

HasherInterface is an interface for writing context to be hashed.

type ParameterConversionErr

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

ParameterConversionErr is an error in type conversion of a supplied parameter.

func (ParameterConversionErr) DetailsMetadata

func (err ParameterConversionErr) DetailsMetadata() map[string]string

DetailsMetadata returns the metadata for details for this error.

func (ParameterConversionErr) MarshalZerologObject

func (err ParameterConversionErr) MarshalZerologObject(e *zerolog.Event)

MarshalZerologObject implements zerolog.LogObjectMarshaler

func (ParameterConversionErr) ParameterName

func (err ParameterConversionErr) ParameterName() string

ParameterName is the name of the parameter.

type SourcePosition

type SourcePosition interface {
	// LineAndColumn returns the 0-indexed line number and column position in the source file.
	LineAndColumn() (int, int, error)

	// RunePosition returns the 0-indexed rune position in the source file.
	RunePosition() (int, error)
}

SourcePosition is an incoming source position.

type UnknownParameterOption

type UnknownParameterOption int

UnknownParameterOption is the option to ConvertContextToParameters around handling of unknown parameters.

const (
	// SkipUnknownParameters indicates that unknown parameters should be skipped in conversion.
	SkipUnknownParameters UnknownParameterOption = 0

	// ErrorForUnknownParameters indicates that unknown parameters should return an error.
	ErrorForUnknownParameters UnknownParameterOption = 1
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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