-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathaccess.go
More file actions
376 lines (308 loc) · 8.26 KB
/
Copy pathaccess.go
File metadata and controls
376 lines (308 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Flow Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ast
import (
"encoding/json"
"fmt"
"strings"
"github.com/turbolent/prettier"
"github.com/onflow/cadence/common"
"github.com/onflow/cadence/errors"
)
//go:generate stringer -type=PrimitiveAccess
type Access interface {
isAccess()
Walk(walkChild func(Element))
Keyword() string
Description() string
String() string
MarshalJSON() ([]byte, error)
Doc() prettier.Doc
}
type Separator uint8
const (
Disjunction Separator = iota
Conjunction
)
func (s Separator) String() string {
switch s {
case Disjunction:
return " | "
case Conjunction:
return ", "
}
panic(errors.NewUnreachableError())
}
type EntitlementSet interface {
Authorization
Entitlements() []*NominalType
Separator() Separator
}
type ConjunctiveEntitlementSet struct {
Elements []*NominalType `json:"ConjunctiveElements"`
}
var _ EntitlementSet = &ConjunctiveEntitlementSet{}
func NewConjunctiveEntitlementSet(entitlements []*NominalType) *ConjunctiveEntitlementSet {
return &ConjunctiveEntitlementSet{Elements: entitlements}
}
func (*ConjunctiveEntitlementSet) isAuthorization() {}
func (s *ConjunctiveEntitlementSet) Entitlements() []*NominalType {
return s.Elements
}
func (s *ConjunctiveEntitlementSet) Separator() Separator {
return Conjunction
}
func (s *ConjunctiveEntitlementSet) Walk(walkChild func(Element)) {
for _, entitlement := range s.Elements {
walkChild(entitlement)
}
}
func (s *ConjunctiveEntitlementSet) CheckEqual(
other Authorization,
c TypeEqualityChecker,
pos HasPosition,
) error {
return c.CheckConjunctiveEntitlementSetEquality(s, other, pos)
}
func (s *ConjunctiveEntitlementSet) String() string {
var sb strings.Builder
for i, entitlement := range s.Elements {
sb.WriteString(entitlement.String())
if i < len(s.Elements)-1 {
sb.WriteString(s.Separator().String())
}
}
return sb.String()
}
type DisjunctiveEntitlementSet struct {
Elements []*NominalType `json:"DisjunctiveElements"`
}
var _ EntitlementSet = &DisjunctiveEntitlementSet{}
func NewDisjunctiveEntitlementSet(entitlements []*NominalType) *DisjunctiveEntitlementSet {
return &DisjunctiveEntitlementSet{Elements: entitlements}
}
func (*DisjunctiveEntitlementSet) isAuthorization() {}
func (s *DisjunctiveEntitlementSet) Entitlements() []*NominalType {
return s.Elements
}
func (s *DisjunctiveEntitlementSet) Separator() Separator {
return Disjunction
}
func (s *DisjunctiveEntitlementSet) Walk(walkChild func(Element)) {
for _, entitlement := range s.Elements {
walkChild(entitlement)
}
}
func (s *DisjunctiveEntitlementSet) CheckEqual(
other Authorization,
c TypeEqualityChecker,
pos HasPosition,
) error {
return c.CheckDisjunctiveEntitlementSetEquality(s, other, pos)
}
func (s *DisjunctiveEntitlementSet) String() string {
var sb strings.Builder
for i, entitlement := range s.Elements {
sb.WriteString(entitlement.String())
if i < len(s.Elements)-1 {
sb.WriteString(s.Separator().String())
}
}
return sb.String()
}
type Authorization interface {
fmt.Stringer
isAuthorization()
Walk(walkChild func(Element))
CheckEqual(Authorization, TypeEqualityChecker, HasPosition) error
}
type EntitlementAccess struct {
EntitlementSet EntitlementSet
}
var _ Access = EntitlementAccess{}
func NewEntitlementAccess(entitlements EntitlementSet) EntitlementAccess {
return EntitlementAccess{EntitlementSet: entitlements}
}
func (EntitlementAccess) isAccess() {}
func (e EntitlementAccess) Walk(walkChild func(Element)) {
e.EntitlementSet.Walk(walkChild)
}
func (EntitlementAccess) Description() string {
return "entitled access"
}
func (e EntitlementAccess) entitlementsString(prefix *strings.Builder) {
for i, entitlement := range e.EntitlementSet.Entitlements() {
prefix.WriteString(entitlement.String())
if i < len(e.EntitlementSet.Entitlements())-1 {
prefix.WriteString(e.EntitlementSet.Separator().String())
}
}
}
func (e EntitlementAccess) String() string {
var sb strings.Builder
sb.WriteString("EntitlementAccess ")
e.entitlementsString(&sb)
return sb.String()
}
func (e EntitlementAccess) Keyword() string {
var sb strings.Builder
sb.WriteString("access(")
e.entitlementsString(&sb)
sb.WriteString(")")
return sb.String()
}
func (e EntitlementAccess) Doc() prettier.Doc {
return prettier.Text(e.Keyword())
}
func (e EntitlementAccess) MarshalJSON() ([]byte, error) {
return json.Marshal(e.String())
}
type MappedAccess struct {
EntitlementMap *NominalType
StartPos Position
}
var _ Access = &MappedAccess{}
func (*MappedAccess) isAccess() {}
func (*MappedAccess) isAuthorization() {}
func (*MappedAccess) Description() string {
return "entitlement-mapped access"
}
func NewMappedAccess(
typ *NominalType,
startPos Position,
) *MappedAccess {
return &MappedAccess{
EntitlementMap: typ,
StartPos: startPos,
}
}
func (a *MappedAccess) StartPosition() Position {
return a.StartPos
}
func (a *MappedAccess) EndPosition(memoryGauge common.MemoryGauge) Position {
return a.EntitlementMap.EndPosition(memoryGauge)
}
func (a *MappedAccess) String() string {
var str strings.Builder
str.WriteString("mapping ")
str.WriteString(a.EntitlementMap.String())
return str.String()
}
func (a *MappedAccess) Keyword() string {
var str strings.Builder
str.WriteString("access(")
str.WriteString(a.String())
str.WriteString(")")
return str.String()
}
func (a *MappedAccess) Doc() prettier.Doc {
return prettier.Text(a.Keyword())
}
func (a *MappedAccess) MarshalJSON() ([]byte, error) {
type Alias MappedAccess
return json.Marshal(&struct {
*Alias
Range
}{
Range: NewUnmeteredRangeFromPositioned(a),
Alias: (*Alias)(a),
})
}
func (a *MappedAccess) Walk(walkChild func(Element)) {
if a.EntitlementMap != nil {
walkChild(a.EntitlementMap)
}
}
func (a *MappedAccess) CheckEqual(other Authorization, c TypeEqualityChecker, pos HasPosition) error {
return c.CheckMappedAccessEquality(a, other, pos)
}
type PrimitiveAccess uint8
// NOTE: order indicates permissiveness: from least to most permissive!
const (
AccessNotSpecified PrimitiveAccess = iota
AccessNone // "top" access, only used for mapping operations, not actually expressible in the language
AccessSelf
AccessContract
AccessAccount
AccessAll
AccessPubSettableLegacy // Deprecated
)
func PrimitiveAccessCount() int {
return len(_PrimitiveAccess_index) - 1
}
func (PrimitiveAccess) isAccess() {}
func (PrimitiveAccess) Walk(_ func(Element)) {}
// TODO: remove.
// only used by tests which are not updated yet
// to include contract and account access
var BasicAccesses = []PrimitiveAccess{
AccessNotSpecified,
AccessSelf,
AccessAll,
}
var AllAccesses = common.Concat(
BasicAccesses,
[]PrimitiveAccess{
AccessContract,
AccessAccount,
},
)
func (a PrimitiveAccess) Keyword() string {
switch a {
case AccessNotSpecified:
return ""
case AccessSelf:
return "access(self)"
case AccessAll:
return "access(all)"
case AccessAccount:
return "access(account)"
case AccessContract:
return "access(contract)"
case AccessPubSettableLegacy:
return "pub(set)"
case AccessNone:
return "inaccessible"
}
panic(errors.NewUnreachableError())
}
func (a PrimitiveAccess) Description() string {
switch a {
case AccessNotSpecified:
return "not specified"
case AccessSelf:
return "self"
case AccessAll:
return "all"
case AccessAccount:
return "account"
case AccessContract:
return "contract"
case AccessPubSettableLegacy:
return "legacy public settable"
case AccessNone:
return "inaccessible"
}
panic(errors.NewUnreachableError())
}
func (a PrimitiveAccess) MarshalJSON() ([]byte, error) {
return json.Marshal(a.String())
}
func (a PrimitiveAccess) Doc() prettier.Doc {
return prettier.Text(a.Keyword())
}