英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论