...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package openapi
16
17 import (
18 "fmt"
19
20 "cuelang.org/go/cue/ast"
21 "cuelang.org/go/cue/literal"
22 "cuelang.org/go/cue/token"
23 internaljson "cuelang.org/go/internal/encoding/json"
24 )
25
26
27
28
29
30
31
32 type OrderedMap ast.StructLit
33
34
35 type KeyValue struct {
36 Key string
37 Value interface{}
38 }
39
40
41
42
43 func fromLegacy(x interface{}) ast.Expr {
44 switch x := x.(type) {
45 case *OrderedMap:
46 return (*ast.StructLit)(x)
47 case []*OrderedMap:
48 a := make([]ast.Expr, len(x))
49 for i, v := range x {
50 a[i] = fromLegacy(v)
51 }
52 return ast.NewList(a...)
53 case string:
54 return ast.NewString(x)
55 case ast.Expr:
56 return x
57 default:
58 panic(fmt.Sprintf("unsupported type %T", x))
59 }
60 }
61
62 func toLegacy(x ast.Expr) interface{} {
63 switch x := x.(type) {
64 case *ast.StructLit:
65 return (*OrderedMap)(x)
66 case *ast.ListLit:
67 a := make([]*OrderedMap, len(x.Elts))
68 for i, v := range x.Elts {
69 e, ok := v.(*ast.StructLit)
70 if !ok {
71 return x
72 }
73 a[i] = (*OrderedMap)(e)
74 }
75 return a
76 case *ast.BasicLit:
77 if x.Kind == token.STRING {
78 str, err := literal.Unquote(x.Value)
79 if err != nil {
80 return x
81 }
82 return str
83 }
84 }
85 return x
86 }
87
88 func (m *OrderedMap) len() int {
89 return len(m.Elts)
90 }
91
92
93 func (m *OrderedMap) Pairs() []KeyValue {
94 kvs := make([]KeyValue, len(m.Elts))
95 for i, e := range m.Elts {
96 kvs[i].Key = label(e)
97 kvs[i].Value = toLegacy(e.(*ast.Field).Value)
98 }
99 return kvs
100 }
101
102 func (m *OrderedMap) find(key string) *ast.Field {
103 for _, v := range m.Elts {
104 f, ok := v.(*ast.Field)
105 if !ok {
106 continue
107 }
108 s, _, err := ast.LabelName(f.Label)
109 if err == nil && s == key {
110 return f
111 }
112 }
113 return nil
114 }
115
116
117
118
119
120
121 func (m *OrderedMap) Set(key string, x interface{}) {
122 switch x := x.(type) {
123 case *OrderedMap:
124 m.setExpr(key, (*ast.StructLit)(x))
125 case string:
126 m.setExpr(key, ast.NewString(x))
127 case ast.Expr:
128 m.setExpr(key, x)
129 default:
130 v, err := toCUE("Set", x)
131 if err != nil {
132 panic(err)
133 }
134 m.setExpr(key, v)
135 }
136 }
137
138 func (m *OrderedMap) setExpr(key string, expr ast.Expr) {
139 if f := m.find(key); f != nil {
140 f.Value = expr
141 return
142 }
143 m.Elts = append(m.Elts, &ast.Field{
144 Label: ast.NewString(key),
145 Value: expr,
146 })
147 }
148
149
150
151 func (m *OrderedMap) SetAll(kvs []KeyValue) {
152 var a []ast.Decl
153 for _, kv := range kvs {
154 a = append(a, &ast.Field{
155 Label: ast.NewString(kv.Key),
156 Value: fromLegacy(kv.Value),
157 })
158 }
159 m.Elts = a
160 }
161
162
163 func (m *OrderedMap) exists(key string) bool {
164 return m.find(key) != nil
165 }
166
167
168 func (m *OrderedMap) getMap(key string) *OrderedMap {
169 f := m.find(key)
170 if f == nil {
171 return nil
172 }
173 return (*OrderedMap)(f.Value.(*ast.StructLit))
174 }
175
176
177 func (m *OrderedMap) MarshalJSON() (b []byte, err error) {
178
179
180 return internaljson.Encode((*ast.StructLit)(m))
181 }
182
View as plain text