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
15package openapi
16
17import (
18 "slices"
19
20 "cuelang.org/go/cue"
21 "cuelang.org/go/cue/errors"
22 "cuelang.org/go/internal/core/adt"
23 "cuelang.org/go/internal/core/dep"
24 "cuelang.org/go/internal/core/eval"
25 internalvalue "cuelang.org/go/internal/value"
26)
27
28func (b *builder) pushNode(v cue.Value) {
29 _, n := internalvalue.ToInternal(v)
30 b.ctx.cycleNodes = append(b.ctx.cycleNodes, n)
31}
32
33func (b *builder) popNode() {
34 b.ctx.cycleNodes = b.ctx.cycleNodes[:len(b.ctx.cycleNodes)-1]
35}
36
37func (b *builder) checkCycle(v cue.Value) bool {
38 if !b.ctx.expandRefs {
39 return true
40 }
41 r, n := internalvalue.ToInternal(v)
42 ctx := eval.NewContext(r, n)
43
44 err := dep.Visit(nil, ctx, n, func(d dep.Dependency) error {
45 if slices.Contains(b.ctx.cycleNodes, d.Node) {
46 err := errors.Newf(adt.Pos(d.Node),
47 "cycle in reference at %v: cyclic structures not allowed when reference expansion is requested", v.Path())
48 b.ctx.errs = errors.Append(b.ctx.errs, err)
49 return err
50 }
51 return nil
52 })
53
54 return err == nil
55}