// Unparse takes an input expression and source position information and generates a human-readable // expression. // // Note, unparsing 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. // - Byte literals are represented as octal escapes (same as Google SQL). // - 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 unparsing behavior, such as // performing word wrapping on expressions. func Unparse(expr ast.Expr, info *ast.SourceInfo, opts ...UnparserOption) (string, error) { … } type unparser … func (un *unparser) visit(expr ast.Expr) error { … } func (un *unparser) visitCall(expr ast.Expr) error { … } func (un *unparser) visitCallBinary(expr ast.Expr) error { … } func (un *unparser) visitCallConditional(expr ast.Expr) error { … } func (un *unparser) visitCallFunc(expr ast.Expr) error { … } func (un *unparser) visitCallIndex(expr ast.Expr) error { … } func (un *unparser) visitCallOptIndex(expr ast.Expr) error { … } func (un *unparser) visitCallIndexInternal(expr ast.Expr, op string) error { … } func (un *unparser) visitCallUnary(expr ast.Expr) error { … } func (un *unparser) visitConst(expr ast.Expr) error { … } func (un *unparser) visitIdent(expr ast.Expr) error { … } func (un *unparser) visitList(expr ast.Expr) error { … } func (un *unparser) visitOptSelect(expr ast.Expr) error { … } func (un *unparser) visitSelect(expr ast.Expr) error { … } func (un *unparser) visitSelectInternal(operand ast.Expr, testOnly bool, op string, field string) error { … } func (un *unparser) visitStructMsg(expr ast.Expr) error { … } func (un *unparser) visitStructMap(expr ast.Expr) error { … } func (un *unparser) visitMaybeMacroCall(expr ast.Expr) (bool, error) { … } func (un *unparser) visitMaybeNested(expr ast.Expr, nested bool) error { … } // isLeftRecursive indicates whether the parser resolves the call in a left-recursive manner as // this can have an effect of how parentheses affect the order of operations in the AST. func isLeftRecursive(op string) bool { … } // isSamePrecedence indicates whether the precedence of the input operator is the same as the // precedence of the (possible) operation represented in the input Expr. // // If the expr is not a Call, the result is false. func isSamePrecedence(op string, expr ast.Expr) bool { … } // isLowerPrecedence indicates whether the precedence of the input operator is lower precedence // than the (possible) operation represented in the input Expr. // // If the expr is not a Call, the result is false. func isLowerPrecedence(op string, expr ast.Expr) bool { … } // Indicates whether the expr is a complex operator, i.e., a call expression // with 2 or more arguments. func isComplexOperator(expr ast.Expr) bool { … } // Indicates whether it is a complex operation compared to another. // expr is *not* considered complex if it is not a call expression or has // less than two arguments, or if it has a higher precedence than op. func isComplexOperatorWithRespectTo(op string, expr ast.Expr) bool { … } // Indicate whether this is a binary or ternary operator. func isBinaryOrTernaryOperator(expr ast.Expr) bool { … } // bytesToOctets converts byte sequences to a string using a three digit octal encoded value // per byte. func bytesToOctets(byteVal []byte) string { … } // writeOperatorWithWrapping outputs the operator and inserts a newline for operators configured // in the unparser options. func (un *unparser) writeOperatorWithWrapping(fun string, unmangled string) bool { … } var defaultWrapOnColumn … var defaultWrapAfterColumnLimit … var defaultOperatorsToWrapOn … type UnparserOption … type unparserOption … // 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 WrapOnColumn(col int) UnparserOption { … } // 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. func WrapOnOperators(symbols ...string) UnparserOption { … } // 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 WrapAfterColumnLimit(wrapAfter bool) UnparserOption { … }