Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve JSON marshalling for more AST elements #292

Merged
merged 3 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions runtime/ast/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,19 @@ func (e *MemberExpression) EndPosition() Position {
}
}

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

// IndexExpression

type IndexExpression struct {
Expand Down Expand Up @@ -541,6 +554,17 @@ func (e *IndexExpression) String() string {
)
}

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

// ConditionalExpression

type ConditionalExpression struct {
Expand Down
91 changes: 91 additions & 0 deletions runtime/ast/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,94 @@ func TestPathExpression_MarshalJSON(t *testing.T) {
string(actual),
)
}

func TestMemberExpression_MarshalJSON(t *testing.T) {

expr := &MemberExpression{
Expression: &BoolExpression{
Value: true,
Range: Range{
StartPos: Position{Offset: 1, Line: 2, Column: 3},
EndPos: Position{Offset: 4, Line: 5, Column: 6},
},
},
Optional: true,
AccessPos: Position{Offset: 7, Line: 8, Column: 9},
Identifier: Identifier{
Identifier: "foobar",
Pos: Position{Offset: 10, Line: 11, Column: 12},
},
}

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

assert.JSONEq(t,
`
{
"Type": "MemberExpression",
"Expression": {
"Type": "BoolExpression",
"Value": true,
"StartPos": {"Offset": 1, "Line": 2, "Column": 3},
"EndPos": {"Offset": 4, "Line": 5, "Column": 6}
},
"Optional": true,
"AccessPos": {"Offset": 7, "Line": 8, "Column": 9},
"Identifier": {
"Identifier": "foobar",
"StartPos": {"Offset": 10, "Line": 11, "Column": 12},
"EndPos": {"Offset": 15, "Line": 11, "Column": 17}
},
"StartPos": {"Offset": 1, "Line": 2, "Column": 3},
"EndPos": {"Offset": 15, "Line": 11, "Column": 17}
}
`,
string(actual),
)
}

func TestIndexExpression_MarshalJSON(t *testing.T) {

expr := &IndexExpression{
TargetExpression: &BoolExpression{
Value: true,
Range: Range{
StartPos: Position{Offset: 1, Line: 2, Column: 3},
EndPos: Position{Offset: 4, Line: 5, Column: 6},
},
},
IndexingExpression: &NilExpression{
Pos: Position{Offset: 7, Line: 8, Column: 9},
},
Range: Range{
StartPos: Position{Offset: 10, Line: 11, Column: 12},
EndPos: Position{Offset: 13, Line: 14, Column: 15},
},
}

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

assert.JSONEq(t,
`
{
"Type": "IndexExpression",
"TargetExpression": {
"Type": "BoolExpression",
"Value": true,
"StartPos": {"Offset": 1, "Line": 2, "Column": 3},
"EndPos": {"Offset": 4, "Line": 5, "Column": 6}
},
"IndexingExpression": {
"Type": "NilExpression",
"StartPos": {"Offset": 7, "Line": 8, "Column": 9},
"EndPos": {"Offset": 9, "Line": 8, "Column": 11}
},
"StartPos": {"Offset": 10, "Line": 11, "Column": 12},
"EndPos": {"Offset": 13, "Line": 14, "Column": 15}
}
`,
string(actual),
)
}
10 changes: 10 additions & 0 deletions runtime/ast/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package ast

import (
"encoding/json"

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

Expand Down Expand Up @@ -54,6 +56,10 @@ const (
OperationBitwiseRightShift
)

func OperationCount() int {
return len(_Operation_index) - 1
}

func (s Operation) Symbol() string {
switch s {
case OperationOr:
Expand Down Expand Up @@ -108,3 +114,7 @@ func (s Operation) Symbol() string {

panic(errors.NewUnreachableError())
}

func (s Operation) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
38 changes: 38 additions & 0 deletions runtime/ast/operation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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"
"fmt"
"testing"

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

func TestOperation_MarshalJSON(t *testing.T) {

for operation := Operation(0); operation < Operation(OperationCount()); operation++ {
actual, err := json.Marshal(operation)
require.NoError(t, err)

assert.JSONEq(t, fmt.Sprintf(`"%s"`, operation), string(actual))
}
}
78 changes: 75 additions & 3 deletions runtime/ast/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ func (s *ReturnStatement) Accept(visitor Visitor) Repr {
return visitor.VisitReturnStatement(s)
}

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

// BreakStatement

type BreakStatement struct {
Expand All @@ -52,6 +63,17 @@ func (s *BreakStatement) Accept(visitor Visitor) Repr {
return visitor.VisitBreakStatement(s)
}

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

// ContinueStatement

type ContinueStatement struct {
Expand All @@ -64,6 +86,17 @@ func (s *ContinueStatement) Accept(visitor Visitor) Repr {
return visitor.VisitContinueStatement(s)
}

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

// IfStatementTest

type IfStatementTest interface {
Expand All @@ -76,7 +109,7 @@ type IfStatement struct {
Test IfStatementTest
Then *Block
Else *Block
StartPos Position
StartPos Position `json:"-"`
}

func (s *IfStatement) StartPosition() Position {
Expand All @@ -96,12 +129,25 @@ func (s *IfStatement) Accept(visitor Visitor) Repr {
return visitor.VisitIfStatement(s)
}

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

// WhileStatement

type WhileStatement struct {
Test Expression
Block *Block
StartPos Position
StartPos Position `json:"-"`
}

func (*WhileStatement) isStatement() {}
Expand All @@ -118,13 +164,26 @@ func (s *WhileStatement) EndPosition() Position {
return s.Block.EndPosition()
}

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

// ForStatement

type ForStatement struct {
Identifier Identifier
Value Expression
Block *Block
StartPos Position
StartPos Position `json:"-"`
}

func (*ForStatement) isStatement() {}
Expand All @@ -141,6 +200,19 @@ func (s *ForStatement) EndPosition() Position {
return s.Block.EndPosition()
}

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

// EmitStatement

type EmitStatement struct {
Expand Down