MCPcopy Create free account
hub / github.com/cel-expr/cel-go / normalizeIDs

Function normalizeIDs

cel/optimizer.go:135–177  ·  view source on GitHub ↗

normalizeIDs ensures that the metadata present with an AST is reset in a manner such that the ids within the expression correspond to the ids within macros.

(idGen ast.IDGenerator, optimized ast.Expr, info *ast.SourceInfo)

Source from the content-addressed store, hash-verified

133// normalizeIDs ensures that the metadata present with an AST is reset in a manner such
134// that the ids within the expression correspond to the ids within macros.
135func normalizeIDs(idGen ast.IDGenerator, optimized ast.Expr, info *ast.SourceInfo) {
136 optimized.RenumberIDs(idGen)
137 info.RenumberIDs(idGen)
138
139 if len(info.MacroCalls()) == 0 {
140 return
141 }
142
143 // Sort the macro ids to make sure that the renumbering of macro-specific variables
144 // is stable across normalization calls.
145 sortedMacroIDs := []int64{}
146 for id := range info.MacroCalls() {
147 sortedMacroIDs = append(sortedMacroIDs, id)
148 }
149 sort.Slice(sortedMacroIDs, func(i, j int) bool { return sortedMacroIDs[i] < sortedMacroIDs[j] })
150
151 // First, update the macro call ids themselves.
152 callIDMap := map[int64]int64{}
153 for _, id := range sortedMacroIDs {
154 callIDMap[id] = idGen(id)
155 }
156 // Then update the macro call definitions which refer to these ids, but
157 // ensure that the updates don't collide and remove macro entries which haven't
158 // been visited / updated yet.
159 type macroUpdate struct {
160 id int64
161 call ast.Expr
162 }
163 macroUpdates := []macroUpdate{}
164 for _, oldID := range sortedMacroIDs {
165 newID := callIDMap[oldID]
166 call, found := info.GetMacroCall(oldID)
167 if !found {
168 continue
169 }
170 call.RenumberIDs(idGen)
171 macroUpdates = append(macroUpdates, macroUpdate{id: newID, call: call})
172 info.ClearMacroCall(oldID)
173 }
174 for _, u := range macroUpdates {
175 info.SetMacroCall(u.id, u.call)
176 }
177}
178
179func cleanupMacroRefs(expr ast.Expr, info *ast.SourceInfo) {
180 if len(info.MacroCalls()) == 0 {

Callers 2

OptimizeMethod · 0.85
CopyASTMethod · 0.85

Calls 6

GetMacroCallMethod · 0.80
RenumberIDsMethod · 0.65
SliceMethod · 0.65
MacroCallsMethod · 0.45
ClearMacroCallMethod · 0.45
SetMacroCallMethod · 0.45

Tested by

no test coverage detected