0

I noticed that when I use a struct consisting of both public and private members, then the private ones are not copied(?) by Cadence activities.

For example I have a struct:

package foo

type Foo struct {
    Name        string
    PublicList  []string
    privateList []string
}

func NewFoo() *Foo {
    return &Foo{
        Name:        "Test",
        PublicList:  []string{"A", "B", "C"},
        privateList: []string{"one", "two"},
    }
}

func (f *Foo) ShowLists() {
    fmt.Println("PublicList: ", f.PublicList, ", privateList: ", f.privateList)
}

I also use other struct, registered as activities struct:

package activities 

type FooActivities struct{}

func (a *FooActivities) NewFoo(ctx context.Context) (*foo.Foo, error) {
    return foo.NewFoo(), nil
}

func (a *FooActivities) ShowLists(ctx context.Context, f *foo.Foo) error {
    f.ShowLists()
    return nil
}

My workflow calls these two activities in a following way:

var f *foo.Foo
workflow.ExecuteActivity(ctx, fooActivities.NewFoo).Get(ctx, &f)
workflow.ExecuteActivity(ctx, fooActivities.ShowLists, f).Get(ctx, nil)

The result, printed by ShowLists function:

PublicList: [A B C] , privateList: []

Why is the private list not initialized as expected? Is this a bug or feature? I couldn't find answer for this question in the Cadence documentation.

Maxim Fateev
  • 6,458
  • 3
  • 20
  • 35
trivelt
  • 1,913
  • 3
  • 22
  • 44
  • As you said, these are private, or unexported fields which aren’t accessible from other packages. – JimB Jul 19 '22 at 11:00

2 Answers2

1

Cadence (and Temporal) by default use json.Marshal to serialize and json.Unmarshall to deserialize activity arguments. It doesn't serialize private fields.

Here is a possible workaround.

Maxim Fateev
  • 6,458
  • 3
  • 20
  • 35
  • Thanks, it solves the problem! BTW I don't think that my question is a duplicate. It's about Cadence, not about JSON marshalling. Information about usage of json.Marshal in Cadence is part of the answer, not question. (@JimB, @blackgreen) – trivelt Jul 19 '22 at 21:07
  • 1
    Next time don't put Go tag on a question :). – Maxim Fateev Jul 21 '22 at 16:28
0

I think it's cause by reflect cann't copy unexported field

xie cui
  • 55
  • 5