【问题标题】:How do I assign value to a repeated oneof field in a protobuf message?如何为 protobuf 消息中的重复 oneof 字段赋值?
【发布时间】:2021-10-05 11:41:46
【问题描述】:

我有一个这样的 protobuf 消息 CoverageReport:

 message Deductible {
  double amount = 1;
  string currency = 2;
}

message Insurance{
  string type = 1;
  string policyNumber = 2;
  string agreementName = 3;
  string appliedTo = 4;
  string validFrom = 5;
  string validTo = 6;
  Deductible deductible = 7;
}

message Warranty {
  string type = 1;
  string warrantyType = 2;
  string agreementName = 3;
  string appliedTo = 4;
  string validFrom = 5;
  string validTo = 6;
}

message Coverage {
  oneof item {
    Insurance insurance = 1;
    Warranty warranty = 2;
  }
}

message CoverageReport {
  bool coverageAppliance = 1;
  repeated Coverage coverages = 2;
}

在我的程序中,我试图将值分配给消息实例,但我不确定如何分配给 oneof 字段 Coverage,然后应将其附加到 Coverages,如下所示 我得到了一个 api 的响应,我将其解组为与 protobuf 具有相似结构的结构 CoverageReport。然后我需要使用结构中的值创建一个 protobuf 消息。但是分配给字段之一似乎不起作用!

type CoverageReport struct {
    
    CoverageAppliance string `json:"coverageAppliance"`
    
    Coverages         []struct {
        Type          string `json:"type"`
        PolicyNumber  string `json:"policyNumber,omitempty"`
        AgreementName string `json:"agreementName"`
        AppliedTo     string `json:"appliedTo"`
        ValidFrom     string `json:"validFrom"`
        ValidTo       string `json:"validTo"`
        
        Deductible    struct {
            Amount   float64 `json:"amount"`
            Currency string  `json:"currency"`
        } `json:"deductible,omitempty"`
        
        WarrantyType string `json:"warrantyType,omitempty"`
    
    } `json:"coverages"`
}

var coveragereport *CoverageReport


err = json.Unmarshal(body, &coveragereport)

var cr *vcsproto.CoverageReport

    if coveragereport.CoverageAppliance == "Yes" {
        cr.CoverageAppliance = true
    }

    for _, item := range coveragereport.Coverages {
        
        if item.Type == "Warranty"{

            var warranty *vcsproto.Warranty 
            
            warranty.Type = item.Type
            warranty.WarrantyType = item.WarrantyType
            warranty.AgreementName = item.AgreementName
            warranty.AppliedTo = item.AppliedTo
            warranty.ValidFrom = item.ValidFrom
            warranty.ValidTo = item.ValidTo

            var coverage *vcsproto.Coverage
            coverage.Item.Warranty = warranty

            cr.Coverages = append(cr.Coverages, coverage)

        } else {

            var insurance *vcsproto.Insurance
             
            insurance.Type = item.Type
            insurance.PolicyNumber = item.PolicyNumber
            insurance.AgreementName = item.AgreementName
            insurance.AppliedTo = item.AppliedTo
            insurance.ValidFrom = item.ValidFrom
            insurance.ValidTo = item.ValidTo. 
            insurance.Deductible.Currency = item.Deductible.Currency 
            insurance.Deductible.Amount = item.Deductible.Amount 

            var coverage *vcsproto.Coverage
            coverage.Item.Insurance = Insurance

            cr.Coverages = append(cr.Coverages, coverage)

        }
    }

【问题讨论】:

  • 使用 Go protobuf 生成器为您的 protobuf 类型生成 Go 类型。您会看到它为Coverage 生成了一个接口类型,其中InsuranceWarranty 作为可能的实现,您可以将其附加到Coverages 字段。

标签: go protocol-buffers


【解决方案1】:

查看生成的代码here

您不直接分配给WarrantyInsurance 子字段,而是直接分配给Item 字段。所以coverage.Item = {insurance/warranty} 应该可以工作。

另请注意,在您的代码中,您正在分配 coverage.Item.Insurance = Insurance - 您可能意味着第二个 Insurance 是变量 insurance

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多