resolveStringExpr attempts to resolve an expression to a string value. Examples: - "my_metric": "my_metric" (string literal) - metricName: resolved value of metricName constant (identifier) - agentmetrics.LabelUsername: resolved from package constants (selector)
(expr ast.Expr, decls declarations)
| 273 | // - metricName: resolved value of metricName constant (identifier) |
| 274 | // - agentmetrics.LabelUsername: resolved from package constants (selector) |
| 275 | func resolveStringExpr(expr ast.Expr, decls declarations) string { |
| 276 | switch e := expr.(type) { |
| 277 | case *ast.BasicLit: |
| 278 | return strings.Trim(e.Value, `"`) |
| 279 | case *ast.Ident: |
| 280 | return decls.strings[e.Name] |
| 281 | case *ast.BinaryExpr: |
| 282 | return resolveBinaryExpr(e, decls) |
| 283 | case *ast.SelectorExpr: |
| 284 | // Handle pkg.Const syntax. |
| 285 | if ident, ok := e.X.(*ast.Ident); ok { |
| 286 | if pkgConsts, ok := packageDeclarations[ident.Name]; ok { |
| 287 | return pkgConsts[e.Sel.Name] |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | return "" |
| 293 | } |
| 294 | |
| 295 | // resolveBinaryExpr resolves a binary expression (string concatenation) to a string. |
| 296 | // It recursively resolves the left and right operands. |
no test coverage detected