Skip to content

Commit

Permalink
support a structtag hint to specify that a String should be an address (
Browse files Browse the repository at this point in the history
#93)

* nested type

* added option to structtag to say that this String field should be address in cadence
  • Loading branch information
bjartek authored Dec 15, 2022
1 parent 9e364c7 commit 18f1b84
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 0 deletions.
18 changes: 18 additions & 0 deletions account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ func TestCheckContractUpdate(t *testing.T) {
g, _ := NewTestingEmulator().StartE()

code := []byte(`pub contract Debug{
pub struct FooListBar {
pub let foo:[Foo2]
pub let bar:String
init(foo:[Foo2], bar:String) {
self.foo=foo
self.bar=bar
}
}
pub struct Foo2{
pub let bar: Address
init(bar: Address) {
self.bar=bar
}
}
pub struct FooBar {
pub let foo:Foo
pub let bar:String
Expand Down
12 changes: 12 additions & 0 deletions cadence.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/fatih/structtag"
"github.com/onflow/cadence"
"golang.org/x/exp/slices"
)

// CadenceValueToJsonString converts a cadence.Value into a json pretty printed string
Expand Down Expand Up @@ -168,6 +169,17 @@ func ReflectToCadence(value reflect.Value, resolver InputResolver) (cadence.Valu
name = strings.ToLower(field.Name)
}

if tag != nil && slices.Contains(tag.Options, "cadenceAddress") {
stringVal := getAndUnquoteString(cadenceVal)
adr, err := hexToAddress(stringVal)
if err != nil {
return nil, err
}
cadenceAddress := cadence.BytesToAddress(adr.Bytes())
cadenceType = cadence.AddressType{}
cadenceVal = cadenceAddress
}

fields = append(fields, cadence.Field{
Identifier: name,
Type: cadenceType,
Expand Down
9 changes: 9 additions & 0 deletions cadence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ type Foo struct {
Bar string
}

type Debug_FooListBar struct {
Bar string
Foo []Debug_Foo2
}

type Debug_FooBar struct {
Bar string
Foo Debug_Foo
Expand All @@ -176,6 +181,10 @@ type Debug_Foo_Skip struct {
Skip string `cadence:"-"`
}

type Debug_Foo2 struct {
Bar string `cadence:"bar,cadenceAddress"`
}

type Debug_Foo struct {
Bar string
}
Expand Down
18 changes: 18 additions & 0 deletions contracts/Debug.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import NonFungibleToken from "./NonFungibleToken.cdc"

pub contract Debug {

pub struct FooListBar {
pub let foo:[Foo2]
pub let bar:String

init(foo:[Foo2], bar:String) {
self.foo=foo
self.bar=bar
}
}
pub struct FooBar {
pub let foo:Foo
pub let bar:String
Expand All @@ -12,6 +21,15 @@ pub contract Debug {
}
}


pub struct Foo2{
pub let bar: Address

init(bar: Address) {
self.bar=bar
}
}

pub struct Foo{
pub let bar: String

Expand Down
54 changes: 54 additions & 0 deletions testdata/TestParseConfig/parse.golden
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ transaction(test:String) {

pub contract Debug {

pub struct FooListBar {
pub let foo:[Foo2]
pub let bar:String

init(foo:[Foo2], bar:String) {
self.foo=foo
self.bar=bar
}
}
pub struct FooBar {
pub let foo:Foo
pub let bar:String
Expand All @@ -205,6 +214,15 @@ pub struct FooBar {
}
}


pub struct Foo2{
pub let bar: Address

init(bar: Address) {
self.bar=bar
}
}

pub struct Foo{
pub let bar: String

Expand Down Expand Up @@ -357,6 +375,15 @@ transaction(test:String) {

pub contract Debug {

pub struct FooListBar {
pub let foo:[Foo2]
pub let bar:String

init(foo:[Foo2], bar:String) {
self.foo=foo
self.bar=bar
}
}
pub struct FooBar {
pub let foo:Foo
pub let bar:String
Expand All @@ -367,6 +394,15 @@ pub struct FooBar {
}
}


pub struct Foo2{
pub let bar: Address

init(bar: Address) {
self.bar=bar
}
}

pub struct Foo{
pub let bar: String

Expand Down Expand Up @@ -516,6 +552,15 @@ transaction(test:String) {

pub contract Debug {

pub struct FooListBar {
pub let foo:[Foo2]
pub let bar:String

init(foo:[Foo2], bar:String) {
self.foo=foo
self.bar=bar
}
}
pub struct FooBar {
pub let foo:Foo
pub let bar:String
Expand All @@ -526,6 +571,15 @@ pub struct FooBar {
}
}


pub struct Foo2{
pub let bar: Address

init(bar: Address) {
self.bar=bar
}
}

pub struct Foo{
pub let bar: String

Expand Down
54 changes: 54 additions & 0 deletions testdata/TestParseConfig/parse_and_merge.golden
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,15 @@ transaction(test:String) {

pub contract Debug {

pub struct FooListBar {
pub let foo:[Foo2]
pub let bar:String

init(foo:[Foo2], bar:String) {
self.foo=foo
self.bar=bar
}
}
pub struct FooBar {
pub let foo:Foo
pub let bar:String
Expand All @@ -186,6 +195,15 @@ pub struct FooBar {
}
}


pub struct Foo2{
pub let bar: Address

init(bar: Address) {
self.bar=bar
}
}

pub struct Foo{
pub let bar: String

Expand Down Expand Up @@ -388,6 +406,15 @@ transaction(test:String) {

pub contract Debug {

pub struct FooListBar {
pub let foo:[Foo2]
pub let bar:String

init(foo:[Foo2], bar:String) {
self.foo=foo
self.bar=bar
}
}
pub struct FooBar {
pub let foo:Foo
pub let bar:String
Expand All @@ -398,6 +425,15 @@ pub struct FooBar {
}
}


pub struct Foo2{
pub let bar: Address

init(bar: Address) {
self.bar=bar
}
}

pub struct Foo{
pub let bar: String

Expand Down Expand Up @@ -597,6 +633,15 @@ transaction(test:String) {

pub contract Debug {

pub struct FooListBar {
pub let foo:[Foo2]
pub let bar:String

init(foo:[Foo2], bar:String) {
self.foo=foo
self.bar=bar
}
}
pub struct FooBar {
pub let foo:Foo
pub let bar:String
Expand All @@ -607,6 +652,15 @@ pub struct FooBar {
}
}


pub struct Foo2{
pub let bar: Address

init(bar: Address) {
self.bar=bar
}
}

pub struct Foo{
pub let bar: String

Expand Down
14 changes: 14 additions & 0 deletions transaction_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,20 @@ func TestTransactionIntegration(t *testing.T) {

})

t.Run("Send nestedstruct with array to transaction", func(t *testing.T) {

o.Tx(`
import Debug from "../contracts/Debug.cdc"
transaction(foo: Debug.FooListBar) {
prepare(acct: AuthAccount) {
}
}`,
WithSigner("first"),
WithArg("foo", Debug_FooListBar{Bar: "bar", Foo: []Debug_Foo2{{Bar: "0xf8d6e0586b0a20c7"}}}),
).AssertSuccess(t)

})

t.Run("Send HttpFile to transaction", func(t *testing.T) {

o.Tx(`
Expand Down

0 comments on commit 18f1b84

Please sign in to comment.