1// Copyright 2019 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 "cuelang.org/go/cue/ast"
19)
20
21// An orderedMap is a set of key-value pairs that preserves the order in which
22// items were added.
23//
24// Deprecated: the API now returns an ast.File. This allows OpenAPI to be
25// represented as JSON, YAML, or CUE data, in addition to being able to use
26// all the ast-related tooling.
27type orderedMap ast.StructLit
28
29func (m *orderedMap) len() int {
30 return len(m.Elts)
31}
32
33func (m *orderedMap) find(key string) *ast.Field {
34 for _, v := range m.Elts {
35 f, ok := v.(*ast.Field)
36 if !ok {
37 continue
38 }
39 s, _, err := ast.LabelName(f.Label)
40 if err == nil && s == key {
41 return f
42 }
43 }
44 return nil
45}
46
47func (m *orderedMap) setExpr(key string, expr ast.Expr) {
48 if f := m.find(key); f != nil {
49 f.Value = expr
50 return
51 }
52 m.Elts = append(m.Elts, &ast.Field{
53 Label: ast.NewString(key),
54 Value: expr,
55 })
56}
57
58// exists reports whether a key-value pair exists for the given key.
59func (m *orderedMap) exists(key string) bool {
60 return m.find(key) != nil
61}
62
63// exists reports whether a key-value pair exists for the given key.
64func (m *orderedMap) getMap(key string) *orderedMap {
65 f := m.find(key)
66 if f == nil {
67 return nil
68 }
69 return (*orderedMap)(f.Value.(*ast.StructLit))
70}