Home > Software design >  Why private members are not passed from Cadence activities?
Why private members are not passed from Cadence activities?

Time:07-20

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.

CodePudding user response:

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

CodePudding user response:

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.

  • Related