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

Function normalizeIDs

cel/optimizer.go:134–176  ·  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

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