Skip to content

Commit

Permalink
add JSON marshalling support for more AST elements
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Aug 10, 2020
1 parent bc078f3 commit 31839be
Show file tree
Hide file tree
Showing 8 changed files with 785 additions and 20 deletions.
30 changes: 30 additions & 0 deletions runtime/ast/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ func (l IdentifierLocation) ID() LocationID {
return NewLocationID(IdentifierLocationPrefix, string(l))
}

func (l IdentifierLocation) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type string
Identifier string
}{
Type: "IdentifierLocation",
Identifier: string(l),
})
}

// StringLocation

const StringLocationPrefix = "S"
Expand All @@ -159,6 +169,16 @@ func (l StringLocation) ID() LocationID {
return NewLocationID(StringLocationPrefix, string(l))
}

func (l StringLocation) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type string
String string
}{
Type: "StringLocation",
String: string(l),
})
}

// AddressLocation

const AddressLocationPrefix = "A"
Expand All @@ -177,6 +197,16 @@ func (l AddressLocation) ToAddress() common.Address {
return common.BytesToAddress(l)
}

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

// HasImportLocation

type HasImportLocation interface {
Expand Down
81 changes: 81 additions & 0 deletions runtime/ast/import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright 2019-2020 Dapper Labs, Inc.
*
* 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"
"testing"

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

func TestIdentifierLocation_MarshalJSON(t *testing.T) {

loc := IdentifierLocation("test")

actual, err := json.Marshal(loc)
require.NoError(t, err)

assert.JSONEq(t,
`
{
"Type": "IdentifierLocation",
"Identifier": "test"
}
`,
string(actual),
)
}

func TestStringLocation_MarshalJSON(t *testing.T) {

loc := StringLocation("test")

actual, err := json.Marshal(loc)
require.NoError(t, err)

assert.JSONEq(t,
`
{
"Type": "StringLocation",
"String": "test"
}
`,
string(actual),
)
}

func TestAddressLocation_MarshalJSON(t *testing.T) {

loc := AddressLocation([]byte{1})

actual, err := json.Marshal(loc)
require.NoError(t, err)

assert.JSONEq(t,
`
{
"Type": "AddressLocation",
"Address": "0x1"
}
`,
string(actual),
)
}
26 changes: 26 additions & 0 deletions runtime/ast/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,19 @@ func (s *AssignmentStatement) Accept(visitor Visitor) Repr {
return visitor.VisitAssignmentStatement(s)
}

func (s *AssignmentStatement) MarshalJSON() ([]byte, error) {
type Alias AssignmentStatement
return json.Marshal(&struct {
Type string
Range
*Alias
}{
Type: "AssignmentStatement",
Range: NewRangeFromPositioned(s),
Alias: (*Alias)(s),
})
}

// SwapStatement

type SwapStatement struct {
Expand All @@ -277,6 +290,19 @@ func (s *SwapStatement) Accept(visitor Visitor) Repr {
return visitor.VisitSwapStatement(s)
}

func (s *SwapStatement) MarshalJSON() ([]byte, error) {
type Alias SwapStatement
return json.Marshal(&struct {
Type string
Range
*Alias
}{
Type: "SwapStatement",
Range: NewRangeFromPositioned(s),
Alias: (*Alias)(s),
})
}

// ExpressionStatement

type ExpressionStatement struct {
Expand Down
108 changes: 108 additions & 0 deletions runtime/ast/statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,111 @@ func TestForStatement_MarshalJSON(t *testing.T) {
string(actual),
)
}

func TestAssignmentStatement_MarshalJSON(t *testing.T) {

stmt := &AssignmentStatement{
Target: &IdentifierExpression{
Identifier: Identifier{
Identifier: "foobar",
Pos: Position{Offset: 1, Line: 2, Column: 3},
},
},
Transfer: &Transfer{
Operation: TransferOperationCopy,
Pos: Position{Offset: 4, Line: 5, Column: 6},
},
Value: &BoolExpression{
Value: false,
Range: Range{
StartPos: Position{Offset: 7, Line: 8, Column: 9},
EndPos: Position{Offset: 10, Line: 11, Column: 12},
},
},
}

actual, err := json.Marshal(stmt)
require.NoError(t, err)

assert.JSONEq(t,
`
{
"Type": "AssignmentStatement",
"Target": {
"Type": "IdentifierExpression",
"Identifier": {
"Identifier": "foobar",
"StartPos": {"Offset": 1, "Line": 2, "Column": 3},
"EndPos": {"Offset": 6, "Line": 2, "Column": 8}
},
"StartPos": {"Offset": 1, "Line": 2, "Column": 3},
"EndPos": {"Offset": 6, "Line": 2, "Column": 8}
},
"Transfer": {
"Type": "Transfer",
"Operation": "TransferOperationCopy",
"StartPos": {"Offset": 4, "Line": 5, "Column": 6},
"EndPos": {"Offset": 4, "Line": 5, "Column": 6}
},
"Value": {
"Type": "BoolExpression",
"Value": false,
"StartPos": {"Offset": 7, "Line": 8, "Column": 9},
"EndPos": {"Offset": 10, "Line": 11, "Column": 12}
},
"StartPos": {"Offset": 1, "Line": 2, "Column": 3},
"EndPos": {"Offset": 10, "Line": 11, "Column": 12}
}
`,
string(actual),
)
}

func TestSwapStatement_MarshalJSON(t *testing.T) {

stmt := &SwapStatement{
Left: &IdentifierExpression{
Identifier: Identifier{
Identifier: "foobar",
Pos: Position{Offset: 1, Line: 2, Column: 3},
},
},
Right: &BoolExpression{
Value: false,
Range: Range{
StartPos: Position{Offset: 4, Line: 5, Column: 6},
EndPos: Position{Offset: 7, Line: 8, Column: 9},
},
},
}

actual, err := json.Marshal(stmt)
require.NoError(t, err)

assert.JSONEq(t,
`
{
"Type": "SwapStatement",
"Left": {
"Type": "IdentifierExpression",
"Identifier": {
"Identifier": "foobar",
"StartPos": {"Offset": 1, "Line": 2, "Column": 3},
"EndPos": {"Offset": 6, "Line": 2, "Column": 8}
},
"StartPos": {"Offset": 1, "Line": 2, "Column": 3},
"EndPos": {"Offset": 6, "Line": 2, "Column": 8}
},
"Right": {
"Type": "BoolExpression",
"Value": false,
"StartPos": {"Offset": 4, "Line": 5, "Column": 6},
"EndPos": {"Offset": 7, "Line": 8, "Column": 9}
},
"StartPos": {"Offset": 1, "Line": 2, "Column": 3},
"EndPos": {"Offset": 7, "Line": 8, "Column": 9}
}
`,
string(actual),
)
}
Loading

0 comments on commit 31839be

Please sign in to comment.