codegen

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

README

Codegen

Codegen is a library that creates, decorates or modifies DST objects in specific repeatable ways. This library is a common place for logic around how new nodes in a DST tree get created, as well as how to handle the whitespace and comments related to those nodes and the elements around them. Any function that creates a new node for insertion into the tree should be added here. When implementing functions for this library, the following rules should apply:

  1. Any DST objects (expressions, statements, nodes, etc.) that are consumed as inputs should be defensively cloned before returning them as part of an output. There is a small execution cost to this, but if an object is duplicated anywhere in the tree, a runtime panic will occur.
  2. Please add a comment header about what the output of your function is and what it does. After a while, it gets very hard to interpret what they do.
  3. Unit tests can be very basic since the generated objects are going to be covered in many ways by the end to end tests. However, if a node gets returns that is invalid, it will fail to reneder and may result in a panic, which is not an acceptable outcome. A test to verify that the output is what we expect is a good safegard.

Documentation

Overview

codegen is a library that creates, decorates or modifies DST objects in specific repeatable ways. This library is a common place for logic around how new nodes in a DST tree get created, as well as how to handle the whitespace and comments related to those nodes and the elements around them. Any function that creates a new node for insertion into the tree should be added here. When implementing functions for this library, the following rules should apply:

1. Any DST objects (expressions, statements, nodes, etc.) that are consumed as inputs should be defensively cloned before returning them as part of an output. There is a small execution cost to this, but if an object is duplicated anywhere in the tree, a runtime panic will occur. 2. Please add a comment header about what the output of your function is and what it does. All exported functions MUST be documented in way that is compatible with `godoc`. 3. Unit tests can be basic since the generated objects are going to be covered in many ways by the end to end tests. However, if a node gets returns that is invalid, it will fail to reneder and may result in a panic, which is not an acceptable outcome. A test to verify that the output is what we expect is a good safegard.

Index

Constants

View Source
const DefaultContextParameter = "ctx"
View Source
const (
	DefaultTransactionVariable = "nrTxn"
)
View Source
const (
	// NewRelicAgentImportPath is the import path for the New Relic Go agent
	NewRelicAgentImportPath = "github.com/newrelic/go-agent/v3/newrelic"
)

Variables

This section is empty.

Functions

func CaptureErrorReturnCallExpression

func CaptureErrorReturnCallExpression(pkg *decorator.Package, call *dst.CallExpr, transactionVariable dst.Expr) ([]dst.Stmt, []dst.Expr)

CaptureErrorReturnCallExpression checks if the return values of a function call is an error, and generates code to assign the return values to variables, check if the error is not nil, and call txn.NoticeError(err) if the error is not nil. It returns the statements that need to be added to the tree, and the expressions that are assigned to the return values of the function call. The list of expressions can be used to replace the expression in the return statement.

func CaptureHttpResponse

func CaptureHttpResponse(segmentVariable string, responseVariable dst.Expr) *dst.AssignStmt

func CreateStatementBlock

func CreateStatementBlock(spacingBefore bool, stmts ...dst.Stmt)

CreateStatementBlock modifies the formatting of a set of statements to all be on separate lines, without any additional spacing between them.

White space is always added after the block.

If spacingBefore == true, an emptyline is added before the block.

func DeferSegment

func DeferSegment(segmentName string, transactionVariable dst.Expr) *dst.DeferStmt

func EndExternalSegment

func EndExternalSegment(segmentName string, nodeDecs *dst.NodeDecs) *dst.ExprStmt

func EndTransaction

func EndTransaction(transactionVariableName string) *dst.ExprStmt

func GetApplication

func GetApplication(transactionVariableExpression dst.Expr) dst.Expr

func HttpRequestContext

func HttpRequestContext(reqArgName string) dst.Expr

HttpRequestContext creates an expression for calling the Context() method on an HTTP request Used to extract context from http.Request objects

func IfErrorNotNilNoticeError

func IfErrorNotNilNoticeError(errorVariable, transactionVariable dst.Expr) *dst.IfStmt

IfErrorNotNilNoticeError creates an if statement that checks if the errorVariable is not nil, and calls notice error if its not nil

Example:

if err != nil {
	txn.NoticeError(err)
}

func NewContextExpression

func NewContextExpression(context dst.Expr, transaction dst.Expr) dst.Expr

NewContextExpression creates an expression that creates a new context this is protected from using the same object, and will always clone inputs

func NewContextParameter

func NewContextParameter(name string) *dst.Field

ContextParameter creates a field for a context parameter

func NewTransactionParameter

func NewTransactionParameter(txnName string) *dst.Field

NewTransactionParameter returns a field definition for a transaction parameter

func NoticeError

func NoticeError(errExpr, transactionVariable dst.Expr, stmtBlock dst.Stmt) *dst.ExprStmt

NoticeError Generates a statement that calls txn.NoticeError(err)

func PrependStatementToFunctionDecl

func PrependStatementToFunctionDecl(fn *dst.FuncDecl, stmt dst.Stmt)

func PrependStatementToFunctionLit

func PrependStatementToFunctionLit(fn *dst.FuncLit, stmt dst.Stmt)

func StartExternalSegment

func StartExternalSegment(request, txnVariable dst.Expr, segmentVar string, nodeDecs *dst.NodeDecs) *dst.AssignStmt

func StartTransaction

func StartTransaction(appVariableName, transactionVariableName, transactionName string, overwriteVariable bool) *dst.AssignStmt

starts a NewRelic transaction if overwireVariable is true, the transaction variable will be overwritten by variable assignment, otherwise it will be defined

func TxnFromContext

func TxnFromContext(txnVariable string, contextObject dst.Expr) *dst.AssignStmt

func TxnFromContextExpression

func TxnFromContextExpression(contextObject dst.Expr) dst.Expr

TxnFromContextExpression returns a call to `newrelic.FromContext(contextObject)`

func TxnNewGoroutine

func TxnNewGoroutine(transaction dst.Expr) *dst.CallExpr

TxnNewGoroutine returns a call to txn.NewGoroutine()

func WrapContextExpression

func WrapContextExpression(context dst.Expr, transaction string, async bool) dst.Expr

WrapContextExpression creates an expression that injects a context with a transaction if async is true, the transaction will be cloned by calling NewGoroutine() this is protected from using the same object, and will always clone inputs

func WrapStatements

func WrapStatements(first, wrapped, last dst.Stmt)

Types

This section is empty.

Jump to

Keyboard shortcuts

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