为什么私有成员不能从Cadence活动中传递?

huangapple go评论68阅读模式
英文:

Why private members are not passed from Cadence activities?

问题

我注意到当我使用一个包含公共和私有成员的结构体时,Cadence活动似乎没有复制私有成员。例如,我有一个结构体:

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)
}

我还使用另一个结构体,注册为活动结构体:

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
}

我的工作流以以下方式调用这两个活动:

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

ShowLists 函数打印的结果是:

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

为什么私有列表没有按预期初始化?这是一个 bug 还是特性?我在 Cadence 文档中找不到这个问题的答案。

英文:

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.

答案1

得分: 1

Cadence(和Temporal)默认使用json.Marshal进行序列化和json.Unmarshall进行反序列化活动参数。它不会序列化私有字段。

这里是一个可能的解决方法。

英文:

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.

答案2

得分: 0

我认为这是由于反射无法复制未导出字段引起的。

英文:

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

huangapple
  • 本文由 发表于 2022年7月19日 08:22:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/73029861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定