cuelang.org/go@v0.10.1/encoding/openapi/cycle.go (about) 1 // Copyright 2021 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package openapi 16 17 import ( 18 "cuelang.org/go/cue" 19 "cuelang.org/go/cue/errors" 20 "cuelang.org/go/cue/token" 21 "cuelang.org/go/internal/core/dep" 22 "cuelang.org/go/internal/core/eval" 23 internalvalue "cuelang.org/go/internal/value" 24 ) 25 26 func (b *builder) pushNode(v cue.Value) { 27 _, n := internalvalue.ToInternal(v) 28 b.ctx.cycleNodes = append(b.ctx.cycleNodes, n) 29 } 30 31 func (b *builder) popNode() { 32 b.ctx.cycleNodes = b.ctx.cycleNodes[:len(b.ctx.cycleNodes)-1] 33 } 34 35 func (b *builder) checkCycle(v cue.Value) bool { 36 if !b.ctx.expandRefs { 37 return true 38 } 39 r, n := internalvalue.ToInternal(v) 40 ctx := eval.NewContext(r, n) 41 42 err := dep.Visit(nil, ctx, n, func(d dep.Dependency) error { 43 for _, m := range b.ctx.cycleNodes { 44 if m == d.Node { 45 var p token.Pos 46 if src := d.Node.Source(); src != nil { 47 p = src.Pos() 48 } 49 err := errors.Newf(p, 50 "cycle in reference at %v: cyclic structures not allowed when reference expansion is requested", v.Path()) 51 b.ctx.errs = errors.Append(b.ctx.errs, err) 52 return err 53 } 54 } 55 return nil 56 }) 57 58 return err == nil 59 }