Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ianthpun committed May 24, 2024
1 parent 008bcb7 commit 826bf90
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 45 deletions.
13 changes: 2 additions & 11 deletions encoding/json/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1380,17 +1380,8 @@ func (d *Decoder) decodeCapability(valueJSON any) cadence.Capability {
obj := toObject(valueJSON)

if d.backwardsCompatible {
if _, hasKey := obj[idKey]; hasKey {
// return the deprecated capability
return cadence.NewDeprecatedMeteredCapability(
d.gauge,
d.decodeUInt64(obj.Get(idKey)),
d.decodeAddress(obj.Get(addressKey)),
d.decodeType(obj.Get(borrowTypeKey), typeDecodingResults{}),
)
} else {
// return the deprecatedPathCapability
path, ok := d.decodeJSON(obj.Get(pathKey)).(cadence.Path)
if _, hasKey := obj[idKey]; !hasKey {
path, ok := d.DecodeJSON(obj.Get(pathKey)).(cadence.Path)
if !ok {
panic(errors.NewDefaultUserError("invalid capability: missing or invalid path"))
}
Expand Down
9 changes: 9 additions & 0 deletions runtime/format/capability.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ import (
"fmt"
)

func PathCapability(address string, id string, path string) string {
return fmt.Sprintf(
"Path(address: %s, id: %s, path %s)",
address,
id,
path,
)
}

func Capability(borrowType string, address string, id string) string {
return fmt.Sprintf(
"Capability<%s>(address: %s, id: %s)",
Expand Down
89 changes: 55 additions & 34 deletions values.go
Original file line number Diff line number Diff line change
Expand Up @@ -2068,76 +2068,95 @@ func (v TypeValue) String() string {
return format.TypeValue(v.StaticType.ID())
}

// DeprecatedCapability
// Capability

type Capability interface {
Value
isCapability()
}

// PathCapability
// Deprecated: removed in v1.0.0

type DeprecatedCapability struct {
type DeprecatedPathCapability struct {
BorrowType Type
Path Path
Address Address
ID UInt64
}

var _ Value = DeprecatedCapability{}
var _ Value = DeprecatedPathCapability{}

func NewDeprecatedCapability(
id UInt64,
func NewDeprecatedPathCapability(
address Address,
path Path,
borrowType Type,
) DeprecatedCapability {
return DeprecatedCapability{
ID: id,
) DeprecatedPathCapability {
return DeprecatedPathCapability{
Path: path,
Address: address,
BorrowType: borrowType,
}
}

func NewDeprecatedMeteredCapability(
func NewDeprecatedMeteredPathCapability(
gauge common.MemoryGauge,
id UInt64,
address Address,
path Path,
borrowType Type,
) DeprecatedCapability {
common.UseMemory(gauge, common.CadenceCapabilityValueMemoryUsage)
return NewDeprecatedCapability(
id,
) DeprecatedPathCapability {
common.UseMemory(gauge, common.PathCapabilityValueStringMemoryUsage) // TODO: confirm if this is right
return NewDeprecatedPathCapability(
address,
path,
borrowType,
)
}

func (DeprecatedCapability) isValue() {}
func (DeprecatedPathCapability) isValue() {}

func (DeprecatedPathCapability) isCapability() {}

func (v DeprecatedCapability) Type() Type {
return NewDeprecatedCapabilityType(v.BorrowType)
func (v DeprecatedPathCapability) Type() Type {
return NewCapabilityType(v.BorrowType)
}

func (v DeprecatedCapability) MeteredType(gauge common.MemoryGauge) Type {
return NewDeprecatedMeteredCapabilityType(gauge, v.BorrowType)
func (v DeprecatedPathCapability) MeteredType(gauge common.MemoryGauge) Type {
return NewMeteredCapabilityType(gauge, v.BorrowType)
}

func (v DeprecatedCapability) String() string {
return format.Capability(
v.BorrowType.ID(),
func (DeprecatedPathCapability) ToGoValue() any {
return nil
}

func (v DeprecatedPathCapability) String() string {
var borrowType string
if v.BorrowType != nil {
borrowType = v.BorrowType.ID()
}

return format.PathCapability(
borrowType,
v.Address.String(),
v.ID.String(),
v.Path.String(),
)
}

// Capability
// IDCapability

type Capability struct {
type IDCapability struct {
BorrowType Type
Address Address
ID UInt64
}

var _ Value = Capability{}
var _ Value = IDCapability{}

func NewCapability(
id UInt64,
address Address,
borrowType Type,
) Capability {
return Capability{
) IDCapability {
return IDCapability{
ID: id,
Address: address,
BorrowType: borrowType,
Expand All @@ -2149,7 +2168,7 @@ func NewMeteredCapability(
id UInt64,
address Address,
borrowType Type,
) Capability {
) IDCapability {
common.UseMemory(gauge, common.CadenceCapabilityValueMemoryUsage)
return NewCapability(
id,
Expand All @@ -2158,17 +2177,19 @@ func NewMeteredCapability(
)
}

func (Capability) isValue() {}
func (IDCapability) isValue() {}

func (IDCapability) isCapability() {}

func (v Capability) Type() Type {
func (v IDCapability) Type() Type {
return NewCapabilityType(v.BorrowType)
}

func (v Capability) MeteredType(gauge common.MemoryGauge) Type {
func (v IDCapability) MeteredType(gauge common.MemoryGauge) Type {
return NewMeteredCapabilityType(gauge, v.BorrowType)
}

func (v Capability) String() string {
func (v IDCapability) String() string {
return format.Capability(
v.BorrowType.ID(),
v.Address.String(),
Expand Down

0 comments on commit 826bf90

Please sign in to comment.