1

我只是在尝试一些基本的 Uber Cadence 示例。我遇到如下问题:我有一个活动,它接收一个结构作为参数并更改值并返回修改后的结构。执行时我无法获得修改后的值工作流。它显示默认值。我尝试如下代码片段:

  type Product struct {
    vendor      string // "Arrows"
    productCategory string // "Clothing"
    productName string // "Shirt"
    description string // "Shirt for Gents"
    size        int    // 42    
    cost        float32// 2.00
    color       string // "Red"
    auctionOption   string // "Buy it Now"
}

// populateProduct
func newProduct() *Product {  
    p := Product{vendor:  "Arrows"}
    p.productCategory = "Clothing"
    p.productName = "Shirt"
    p.description = "Shirt for Gents"
    p.size = 42 
    p.cost = 2.00
    p.color = "Red"
    p.auctionOption = "Buy it Now"
    return &p
}

// Activity
func NewProductActivity(ctx context.Context) (*Product, error) {
    logger := activity.GetLogger(ctx)
    logger.Info("NewProductActivity started.................")
    np := newProduct()
    return np, nil
    }

    // Workflow
    func MarketPlaceWorkflow(ctx workflow.Context, name string) (string, error) {
        ctx = workflow.WithActivityOptions(ctx, activityOptions)

        logger := workflow.GetLogger(ctx)
    logger.Info("MarketPlace workflow started!................")

    // Invoking NewProductActivity
    var npActivityResult Product
    err := workflow.ExecuteActivity(ctx, NewProductActivity).Get(ctx, &npActivityResult)
    if err != nil {
        logger.Error("MarketPlace workflow NewProductActivity failed!.............", zap.Error(err))
        return "", err
    }
        logger.Info("Vendor: ", zap.String("Vendor", npActivityResult.vendor)) // It is showing default values
   return "", nil
}
4

1 回答 1

3

Cadence 和 Temporal 默认使用encoding/json包来序列化和反序列化活动输入和输出。这个包不序列化私有字段。因此,在您的情况下,由于所有字段都是私有的,因此在从活动传递到工作流时它们会被忽略。将您的代码更改为:

type Product struct {
    Vendor      string // "Arrows"
    ProductCategory string // "Clothing"
    ProductName string // "Shirt"
    Description string // "Shirt for Gents"
    Size        int    // 42    
    Cost        float32// 2.00
    Color       string // "Red"
    AuctionOption   string // "Buy it Now"
}

// populateProduct
func newProduct() *Product {  
    p := Product{Vendor:  "Arrows"}
    p.ProductCategory = "Clothing"
    p.ProductName = "Shirt"
    p.Description = "Shirt for Gents"
    p.Size = 42 
    p.Cost = 2.00
    p.Color = "Red"
    p.AuctionOption = "Buy it Now"
    return &p
}

// Activity
func NewProductActivity(ctx context.Context) (*Product, error) {
    logger := activity.GetLogger(ctx)
    logger.Info("NewProductActivity started.................")
    np := newProduct()
    return np, nil
}

// Workflow
func MarketPlaceWorkflow(ctx workflow.Context, name string) (string, error) {
    ctx = workflow.WithActivityOptions(ctx, activityOptions)

    logger := workflow.GetLogger(ctx)
    logger.Info("MarketPlace workflow started!................")

    // Invoking NewProductActivity
    var npActivityResult Product
    err := workflow.ExecuteActivity(ctx, NewProductActivity).Get(ctx, &npActivityResult)
    if err != nil {
        logger.Error("MarketPlace workflow NewProductActivity failed!.............", zap.Error(err))
        return "", err
    }
    logger.Info("Vendor: ", zap.String("Vendor", npActivityResult.Vendor))
    return "", nil
}
于 2020-06-15T17:08:35.783 回答