Skip to content

Commit

Permalink
Merge pull request #460 from onflow/bastian/refactor-address-location…
Browse files Browse the repository at this point in the history
…s-0.10
  • Loading branch information
turbolent authored Nov 23, 2020
2 parents 1d2d444 + dfb5c13 commit 922f49a
Show file tree
Hide file tree
Showing 23 changed files with 651 additions and 277 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: golangci/golangci-lint-action@v1
- uses: golangci/golangci-lint-action@v2
with:
version: v1.26
version: v1.29
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ build:

.PHONY: install-tools
install-tools:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ${GOPATH}/bin v1.26.0
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ${GOPATH}/bin v1.29.0

.PHONY: lint
lint:
Expand Down
2 changes: 1 addition & 1 deletion languageserver/integration/resolving.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func resolveFileImport(mainPath string, location ast.StringLocation) (string, er
}

func (i *FlowIntegration) resolveAccountImport(location ast.AddressLocation) (string, error) {
accountAddr := location.ToAddress()
accountAddr := location.Address

acct, err := i.flowClient.GetAccount(context.Background(), flow.BytesToAddress(accountAddr[:]))
if err != nil {
Expand Down
161 changes: 107 additions & 54 deletions runtime/ast/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ func (d *ImportDeclaration) MarshalJSON() ([]byte, error) {
type Location interface {
// ID returns the canonical ID for this import location.
ID() LocationID
// TypeID returns a type ID for the given qualified identifier
TypeID(qualifiedIdentifier string) TypeID
// QualifiedIdentifier returns the qualified identifier for the given type ID
QualifiedIdentifier(typeID TypeID) string
}

func LocationsMatch(first, second Location) bool {
Expand Down Expand Up @@ -137,39 +141,65 @@ func LocationFromTypeID(typeID string) Location {
return nil
}

return AddressLocation(address)

case AddressContractLocationPrefix:
address, err := hex.DecodeString(pieces[1])
if err != nil {
return nil
var name string
if len(pieces) > 2 {
name = pieces[2]
}

return AddressContractLocation{
AddressLocation: address,
Name: pieces[2],
return AddressLocation{
Address: common.BytesToAddress(address),
Name: name,
}
}

return nil
}

// LocationID

//
type LocationID string

func NewLocationID(parts ...string) LocationID {
return LocationID(strings.Join(parts, "."))
}

// IdentifierLocation
// TypeID
//
type TypeID string

func NewTypeID(parts ...string) TypeID {
return TypeID(strings.Join(parts, "."))
}

// IdentifierLocation
//
const IdentifierLocationPrefix = "I"

type IdentifierLocation string

func (l IdentifierLocation) ID() LocationID {
return NewLocationID(IdentifierLocationPrefix, string(l))
return NewLocationID(
IdentifierLocationPrefix,
string(l),
)
}

func (l IdentifierLocation) TypeID(qualifiedIdentifier string) TypeID {
return NewTypeID(
IdentifierLocationPrefix,
string(l),
qualifiedIdentifier,
)
}

func (l IdentifierLocation) QualifiedIdentifier(typeID TypeID) string {
pieces := strings.SplitN(string(typeID), ".", 3)

if len(pieces) < 3 {
return ""
}

return pieces[2]
}

func (l IdentifierLocation) MarshalJSON() ([]byte, error) {
Expand All @@ -182,14 +212,35 @@ func (l IdentifierLocation) MarshalJSON() ([]byte, error) {
})
}

// StringLocation

const StringLocationPrefix = "S"

// StringLocation
//
type StringLocation string

func (l StringLocation) ID() LocationID {
return NewLocationID(StringLocationPrefix, string(l))
return NewLocationID(
StringLocationPrefix,
string(l),
)
}

func (l StringLocation) TypeID(qualifiedIdentifier string) TypeID {
return NewTypeID(
StringLocationPrefix,
string(l),
qualifiedIdentifier,
)
}

func (l StringLocation) QualifiedIdentifier(typeID TypeID) string {
pieces := strings.SplitN(string(typeID), ".", 3)

if len(pieces) < 3 {
return ""
}

return pieces[2]
}

func (l StringLocation) MarshalJSON() ([]byte, error) {
Expand All @@ -202,66 +253,68 @@ func (l StringLocation) MarshalJSON() ([]byte, error) {
})
}

// AddressLocation

const AddressLocationPrefix = "A"

type AddressLocation []byte
// AddressLocation is the location of a contract/contract interface at an address
//
type AddressLocation struct {
Address common.Address
Name string
}

func (l AddressLocation) String() string {
return l.ToAddress().String()
if l.Name == "" {
return l.Address.String()
}

return fmt.Sprintf(
"%s.%s",
l.Address.String(),
l.Name,
)
}

func (l AddressLocation) ID() LocationID {
return NewLocationID(AddressLocationPrefix, l.ToAddress().Hex())
}
if l.Name == "" {
return NewLocationID(
AddressLocationPrefix,
l.Address.Hex(),
)
}

func (l AddressLocation) ToAddress() common.Address {
return common.BytesToAddress(l)
return NewLocationID(
AddressLocationPrefix,
l.Address.Hex(),
l.Name,
)
}

func (l AddressLocation) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type string
Address string
}{
Type: "AddressLocation",
Address: l.ToAddress().ShortHexWithPrefix(),
})
func (l AddressLocation) TypeID(qualifiedIdentifier string) TypeID {
return NewTypeID(
AddressLocationPrefix,
l.Address.Hex(),
qualifiedIdentifier,
)
}

const AddressContractLocationPrefix = "AC"

// AddressContractLocation is the location of a contract/contract interface at an address
func (l AddressLocation) QualifiedIdentifier(typeID TypeID) string {
pieces := strings.SplitN(string(typeID), ".", 3)

type AddressContractLocation struct {
AddressLocation AddressLocation
Name string
}

func (l AddressContractLocation) String() string {
return fmt.Sprintf("%s.%s",
l.AddressLocation.String(),
l.Name,
)
}
if len(pieces) < 3 {
return ""
}

func (l AddressContractLocation) ID() LocationID {
return NewLocationID(
AddressContractLocationPrefix,
l.AddressLocation.ToAddress().Hex(),
l.Name,
)
return pieces[2]
}

func (l AddressContractLocation) MarshalJSON() ([]byte, error) {
func (l AddressLocation) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type string
Address string
Name string
}{
Type: "AddressContractLocation",
Address: l.AddressLocation.ToAddress().ShortHexWithPrefix(),
Type: "AddressLocation",
Address: l.Address.ShortHexWithPrefix(),
Name: l.Name,
})
}
Expand Down
10 changes: 8 additions & 2 deletions runtime/ast/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

func TestIdentifierLocation_MarshalJSON(t *testing.T) {
Expand Down Expand Up @@ -70,7 +72,10 @@ func TestAddressLocation_MarshalJSON(t *testing.T) {

t.Parallel()

loc := AddressLocation([]byte{1})
loc := AddressLocation{
Address: common.BytesToAddress([]byte{1}),
Name: "A",
}

actual, err := json.Marshal(loc)
require.NoError(t, err)
Expand All @@ -79,7 +84,8 @@ func TestAddressLocation_MarshalJSON(t *testing.T) {
`
{
"Type": "AddressLocation",
"Address": "0x1"
"Address": "0x1",
"Name": "A"
}
`,
string(actual),
Expand Down
2 changes: 1 addition & 1 deletion runtime/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func PrettyPrintError(writer io.Writer, err error, filename string, codes map[st
case ast.StringLocation:
filename = string(importLocation)
case ast.AddressLocation:
filename = importLocation.ToAddress().ShortHexWithPrefix()
filename = importLocation.Address.ShortHexWithPrefix()
case ast.IdentifierLocation:
filename = string(importLocation)
}
Expand Down
8 changes: 4 additions & 4 deletions runtime/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,13 +499,13 @@ func TestRuntimeImportMultipleContracts(t *testing.T) {
},
resolveLocation: func(identifiers []ast.Identifier, location ast.Location) (result []sema.ResolvedLocation) {

// Resolve each identifier as an address contract location
// Resolve each identifier as an address location

for _, identifier := range identifiers {
result = append(result, sema.ResolvedLocation{
Location: ast.AddressContractLocation{
AddressLocation: location.(ast.AddressLocation),
Name: identifier.Identifier,
Location: ast.AddressLocation{
Address: location.(ast.AddressLocation).Address,
Name: identifier.Identifier,
},
Identifiers: []ast.Identifier{
identifier,
Expand Down
Loading

0 comments on commit 922f49a

Please sign in to comment.