-
Notifications
You must be signed in to change notification settings - Fork 85
/
node_declare.go
62 lines (50 loc) · 1.6 KB
/
node_declare.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package controller
import (
"strings"
"sync"
"github.com/grafana/alloy/syntax/ast"
"github.com/grafana/alloy/syntax/vm"
)
// DeclareNode represents a declare block in the DAG.
type DeclareNode struct {
label string
nodeID string
componentName string
mut sync.RWMutex
block *ast.BlockStmt
}
var _ BlockNode = (*DeclareNode)(nil)
const declareType = "declare"
// NewDeclareNode creates a new declare node with a content which will be loaded by custom components.
func NewDeclareNode(block *ast.BlockStmt) *DeclareNode {
return &DeclareNode{
label: block.Label,
nodeID: BlockComponentID(block).String(),
componentName: block.GetBlockName(),
block: block,
}
}
// Evaluate does nothing for this node.
func (cn *DeclareNode) Evaluate(scope *vm.Scope) error {
return nil
}
// Label returns the label of the block.
func (cn *DeclareNode) Label() string { return cn.label }
// Block implements BlockNode and returns the current block of the managed config node.
func (cn *DeclareNode) Block() *ast.BlockStmt {
return cn.block
}
// NodeID implements dag.Node and returns the unique ID for the config node.
func (cn *DeclareNode) NodeID() string { return cn.nodeID }
// UpdateBlock updates the managed Alloy block.
//
// UpdateBlock will panic if the block does not match the component ID of the
// DeclareNode.
func (cn *DeclareNode) UpdateBlock(b *ast.BlockStmt) {
if !BlockComponentID(b).Equals(strings.Split(cn.nodeID, ".")) {
panic("UpdateBlock called with an Alloy block with a different ID")
}
cn.mut.Lock()
defer cn.mut.Unlock()
cn.block = b
}