Skip to content

Commit

Permalink
add sema.Access.QualifiedKeyword
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Aug 27, 2024
1 parent d947a94 commit 932d9de
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 9 deletions.
42 changes: 33 additions & 9 deletions runtime/sema/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package sema

import (
"io"
"strings"
"sync"

Expand All @@ -39,6 +40,7 @@ type Access interface {
Equal(other Access) bool
// PermitsAccess returns whether receiver access permits argument access
PermitsAccess(Access) bool
QualifiedKeyword() string
}

type EntitlementSetKind uint8
Expand Down Expand Up @@ -132,8 +134,7 @@ func FormatEntitlementSetTypeID[T ~string](entitlementTypeIDs []T, kind Entitlem
return T(builder.String())
}

func (e EntitlementSetAccess) string(typeFormatter func(Type) string) string {
var builder strings.Builder
func (e EntitlementSetAccess) format(w io.StringWriter, typeFormatter func(Type) string) {
var separator string

switch e.SetKind {
Expand All @@ -149,25 +150,36 @@ func (e EntitlementSetAccess) string(typeFormatter func(Type) string) string {

e.Entitlements.ForeachWithIndex(func(i int, entitlement *EntitlementType, _ struct{}) {
if i > 0 {
builder.WriteString(separator)
w.WriteString(separator)

Check failure on line 153 in runtime/sema/access.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `w.WriteString` is not checked (errcheck)

Check failure on line 153 in runtime/sema/access.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `w.WriteString` is not checked (errcheck)
}
builder.WriteString(typeFormatter(entitlement))

w.WriteString(typeFormatter(entitlement))

Check failure on line 155 in runtime/sema/access.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `w.WriteString` is not checked (errcheck)

Check failure on line 155 in runtime/sema/access.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `w.WriteString` is not checked (errcheck)
})

return builder.String()
}

func (e EntitlementSetAccess) String() string {
return e.string(func(t Type) string {
var sb strings.Builder
e.format(&sb, func(t Type) string {
return t.String()
})
return sb.String()
}

func (e EntitlementSetAccess) QualifiedString() string {
return e.string(func(t Type) string {
var sb strings.Builder
e.format(&sb, func(t Type) string {
return t.QualifiedString()
})
return sb.String()
}

func (e EntitlementSetAccess) QualifiedKeyword() string {
var sb strings.Builder
sb.WriteString("access(")
e.format(&sb, func(t Type) string {
return t.QualifiedString()
})
sb.WriteString(")")
return sb.String()
}

func (e EntitlementSetAccess) Equal(other Access) bool {
Expand Down Expand Up @@ -288,6 +300,14 @@ func (e *EntitlementMapAccess) QualifiedString() string {
return e.Type.QualifiedString()
}

func (e *EntitlementMapAccess) QualifiedKeyword() string {
var sb strings.Builder
sb.WriteString("access(mapping ")
sb.WriteString(e.Type.QualifiedIdentifier())
sb.WriteString(")")
return sb.String()
}

func (e *EntitlementMapAccess) Equal(other Access) bool {
switch otherAccess := other.(type) {
case *EntitlementMapAccess:
Expand Down Expand Up @@ -455,6 +475,10 @@ func (a PrimitiveAccess) QualifiedString() string {
return ast.PrimitiveAccess(a).Description()
}

func (a PrimitiveAccess) QualifiedKeyword() string {
return ast.PrimitiveAccess(a).Keyword()
}

func (a PrimitiveAccess) Equal(other Access) bool {
switch otherAccess := other.(type) {
case PrimitiveAccess:
Expand Down
84 changes: 84 additions & 0 deletions runtime/sema/access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,32 @@ import (

"github.com/stretchr/testify/assert"

"github.com/onflow/cadence/runtime/ast"
"github.com/onflow/cadence/runtime/common"
)

func TestPrimitiveAccess_QualifiedKeyword(t *testing.T) {

t.Parallel()

expected := map[ast.PrimitiveAccess]string{
ast.AccessNotSpecified: "",
ast.AccessSelf: "access(self)",
ast.AccessAll: "access(all)",
ast.AccessAccount: "access(account)",
ast.AccessContract: "access(contract)",
ast.AccessPubSettableLegacy: "pub(set)",
ast.AccessNone: "inaccessible",
}

for access := 0; access < ast.PrimitiveAccessCount(); access++ {
assert.Equal(t,
expected[ast.PrimitiveAccess(access)],
PrimitiveAccess(access).QualifiedKeyword(),
)
}
}

func TestNewEntitlementAccess(t *testing.T) {

t.Parallel()
Expand Down Expand Up @@ -181,6 +204,43 @@ func TestNewEntitlementAccess(t *testing.T) {
})
}

func TestEntitlementSetAccess_QualifiedKeyword(t *testing.T) {

t.Parallel()

location := common.NewAddressLocation(nil, common.MustBytesToAddress([]byte{0x1}), "Foo")

fooType := &CompositeType{
Location: location,
Identifier: "Foo",
}

barType := NewEntitlementType(
nil,
location,
"Bar",
)
barType.SetContainerType(fooType)

bazType := NewEntitlementType(
nil,
location,
"Baz",
)
bazType.SetContainerType(fooType)

assert.Equal(t,
"access(Foo.Bar | Foo.Baz)",
newEntitlementAccess(
[]Type{
barType,
bazType,
},
Disjunction,
).QualifiedKeyword(),
)
}

func TestEntitlementMapAccess_ID(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -550,3 +610,27 @@ func TestEntitlementSetAccess_QualifiedString(t *testing.T) {
)
})
}

func TestEntitlementMapAccess_QualifiedKeyword(t *testing.T) {

t.Parallel()

location := common.NewAddressLocation(nil, common.MustBytesToAddress([]byte{0x1}), "Foo")

fooType := &CompositeType{
Location: location,
Identifier: "Foo",
}

barType := NewEntitlementMapType(
nil,
location,
"Bar",
)
barType.SetContainerType(fooType)

assert.Equal(t,
"access(mapping Foo.Bar)",
NewEntitlementMapAccess(barType).QualifiedKeyword(),
)
}

0 comments on commit 932d9de

Please sign in to comment.