celfmt

package module
v0.0.0-...-85dd1b5 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

README

celfmt

This contains a library function, celfmt.Format that can be used to canonically format CEL programs in a non-minified way and a zero-configuration command that uses that function to format CEL programs.

The command can be installed with go install github.com/elastic/celfmt/cmd/celfmt@latest.

celfmt.Format is forked from the original minifying formatter here.

The command may be used to format CEL programs in elastic agent integration configurations with some limitations. In particular, CEL programs MUST be included in YAML pipe string literals with the field name program starting from the first column of the line.

License

This software is licensed under the Apache License, version 2 ("Apache-2.0").

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(dst io.Writer, ast *ast.AST, src common.Source, opts ...FormatOption) error

Format takes an input expression and source information and generates a human-readable expression, writing it to dst.

Note, formatting an AST will often generate the same expression as was originally parsed, but some formatting may be lost in translation, notably:

- All quoted literals are doubled quoted unless triple quoted string literal syntax is used. - Byte literals are represented as octal escapes (same as Google SQL) unless using triple quotes. - Floating point values are converted to the small number of digits needed to represent the value. - Spacing around punctuation marks may be lost. - Parentheses will only be applied when they affect operator precedence.

This function optionally takes in one or more UnparserOption to alter the formatting behavior, such as performing word wrapping on expressions.

Types

type FormatOption

type FormatOption func(*unparserOption) (*unparserOption, error)

FormatOption is a functional option for configuring the output formatting of the Unparse function.

func AlwaysComma

func AlwaysComma() FormatOption

func Pretty

func Pretty() FormatOption

func WrapAfterColumnLimit

func WrapAfterColumnLimit(wrapAfter bool) FormatOption

WrapAfterColumnLimit dictates whether to insert a newline before or after the specified operator when word wrapping is performed.

Example usage:

Unparse(expr, sourceInfo, WrapOnColumn(40), WrapOnOperators(Operators.LogicalAnd), WrapAfterColumnLimit(false))

This will insert a newline immediately before the logical AND operator for the below example input, ensuring that the length of a line never exceeds the specified column limit:

Input: 'my-principal-group' in request.auth.claims && request.auth.claims.iat > now - duration('5m')

Output: 'my-principal-group' in request.auth.claims && request.auth.claims.iat > now - duration('5m')

func WrapOnColumn

func WrapOnColumn(col int) FormatOption

WrapOnColumn wraps the output expression when its string length exceeds a specified limit for operators set by WrapOnOperators function or by default, "&&" and "||" will be wrapped.

Example usage:

Unparse(expr, sourceInfo, WrapOnColumn(40), WrapOnOperators(Operators.LogicalAnd))

This will insert a newline immediately after the logical AND operator for the below example input:

Input: 'my-principal-group' in request.auth.claims && request.auth.claims.iat > now - duration('5m')

Output: 'my-principal-group' in request.auth.claims && request.auth.claims.iat > now - duration('5m')

func WrapOnOperators

func WrapOnOperators(symbols ...string) FormatOption

WrapOnOperators specifies which operators to perform word wrapping on an output expression when its string length exceeds the column limit set by WrapOnColumn function.

Word wrapping is supported on non-unary symbolic operators. Refer to operators.go for the full list

This will replace any previously supplied operators instead of merging them.

Directories

Path Synopsis
cmd
celfmt
The celfmt command formats a CEL program in a canonical format.
The celfmt command formats a CEL program in a canonical format.

Jump to

Keyboard shortcuts

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