如何为 Protobuf 消息中的重复 oneof 字段分配值?

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

How do I assign value to a repeated oneof field in a protobuf message?

问题

我有一个名为CoverageReport的protobuf消息,结构如下:

  1. message Deductible {
  2. double amount = 1;
  3. string currency = 2;
  4. }
  5. message Insurance {
  6. string type = 1;
  7. string policyNumber = 2;
  8. string agreementName = 3;
  9. string appliedTo = 4;
  10. string validFrom = 5;
  11. string validTo = 6;
  12. Deductible deductible = 7;
  13. }
  14. message Warranty {
  15. string type = 1;
  16. string warrantyType = 2;
  17. string agreementName = 3;
  18. string appliedTo = 4;
  19. string validFrom = 5;
  20. string validTo = 6;
  21. }
  22. message Coverage {
  23. oneof item {
  24. Insurance insurance = 1;
  25. Warranty warranty = 2;
  26. }
  27. }
  28. message CoverageReport {
  29. bool coverageAppliance = 1;
  30. repeated Coverage coverages = 2;
  31. }

在我的程序中,我试图给一个消息实例赋值,但我不确定如何给oneof字段Coverage赋值,然后将其附加到Coverages中,就像这样:

我从一个API中获取响应,将其解组成与protobuf类似结构的CoverageReport结构体。然后,我需要使用结构体中的值创建一个protobuf消息。但是似乎无法给oneof字段赋值!

  1. type CoverageReport struct {
  2. CoverageAppliance string `json:"coverageAppliance"`
  3. Coverages []struct {
  4. Type string `json:"type"`
  5. PolicyNumber string `json:"policyNumber,omitempty"`
  6. AgreementName string `json:"agreementName"`
  7. AppliedTo string `json:"appliedTo"`
  8. ValidFrom string `json:"validFrom"`
  9. ValidTo string `json:"validTo"`
  10. Deductible struct {
  11. Amount float64 `json:"amount"`
  12. Currency string `json:"currency"`
  13. } `json:"deductible,omitempty"`
  14. WarrantyType string `json:"warrantyType,omitempty"`
  15. } `json:"coverages"`
  16. }
  17. var coveragereport *CoverageReport
  18. err = json.Unmarshal(body, &coveragereport)
  19. var cr *vcsproto.CoverageReport
  20. if coveragereport.CoverageAppliance == "Yes" {
  21. cr.CoverageAppliance = true
  22. }
  23. for _, item := range coveragereport.Coverages {
  24. if item.Type == "Warranty" {
  25. var warranty *vcsproto.Warranty
  26. warranty.Type = item.Type
  27. warranty.WarrantyType = item.WarrantyType
  28. warranty.AgreementName = item.AgreementName
  29. warranty.AppliedTo = item.AppliedTo
  30. warranty.ValidFrom = item.ValidFrom
  31. warranty.ValidTo = item.ValidTo
  32. var coverage *vcsproto.Coverage
  33. coverage.Item.Warranty = warranty
  34. cr.Coverages = append(cr.Coverages, coverage)
  35. } else {
  36. var insurance *vcsproto.Insurance
  37. insurance.Type = item.Type
  38. insurance.PolicyNumber = item.PolicyNumber
  39. insurance.AgreementName = item.AgreementName
  40. insurance.AppliedTo = item.AppliedTo
  41. insurance.ValidFrom = item.ValidFrom
  42. insurance.ValidTo = item.ValidTo
  43. insurance.Deductible.Currency = item.Deductible.Currency
  44. insurance.Deductible.Amount = item.Deductible.Amount
  45. var coverage *vcsproto.Coverage
  46. coverage.Item.Insurance = insurance
  47. cr.Coverages = append(cr.Coverages, coverage)
  48. }
  49. }

以上是你提供的代码的翻译。

英文:

I have a protobuf message CoverageReport like this:

  1. message Deductible {
  2. double amount = 1;
  3. string currency = 2;
  4. }
  5. message Insurance{
  6. string type = 1;
  7. string policyNumber = 2;
  8. string agreementName = 3;
  9. string appliedTo = 4;
  10. string validFrom = 5;
  11. string validTo = 6;
  12. Deductible deductible = 7;
  13. }
  14. message Warranty {
  15. string type = 1;
  16. string warrantyType = 2;
  17. string agreementName = 3;
  18. string appliedTo = 4;
  19. string validFrom = 5;
  20. string validTo = 6;
  21. }
  22. message Coverage {
  23. oneof item {
  24. Insurance insurance = 1;
  25. Warranty warranty = 2;
  26. }
  27. }
  28. message CoverageReport {
  29. bool coverageAppliance = 1;
  30. repeated Coverage coverages = 2;
  31. }

In my program I'm trying to assign values to a message instance, but I'm not sure how to assign to the oneof field Coverage which should then be appended to Coverages, like this
I get a response from an api, which i unmarshal into the struct CoverageReport which has similar structure to the protobuf. Then i need to create a protobuf message using the values from the struct. But it doesn't seem to work to asssign to the one of field!

  1. type CoverageReport struct {
  2. CoverageAppliance string `json:"coverageAppliance"`
  3. Coverages []struct {
  4. Type string `json:"type"`
  5. PolicyNumber string `json:"policyNumber,omitempty"`
  6. AgreementName string `json:"agreementName"`
  7. AppliedTo string `json:"appliedTo"`
  8. ValidFrom string `json:"validFrom"`
  9. ValidTo string `json:"validTo"`
  10. Deductible struct {
  11. Amount float64 `json:"amount"`
  12. Currency string `json:"currency"`
  13. } `json:"deductible,omitempty"`
  14. WarrantyType string `json:"warrantyType,omitempty"`
  15. } `json:"coverages"`
  16. }
  17. var coveragereport *CoverageReport
  18. err = json.Unmarshal(body, &coveragereport)
  19. var cr *vcsproto.CoverageReport
  20. if coveragereport.CoverageAppliance == "Yes" {
  21. cr.CoverageAppliance = true
  22. }
  23. for _, item := range coveragereport.Coverages {
  24. if item.Type == "Warranty"{
  25. var warranty *vcsproto.Warranty
  26. warranty.Type = item.Type
  27. warranty.WarrantyType = item.WarrantyType
  28. warranty.AgreementName = item.AgreementName
  29. warranty.AppliedTo = item.AppliedTo
  30. warranty.ValidFrom = item.ValidFrom
  31. warranty.ValidTo = item.ValidTo
  32. var coverage *vcsproto.Coverage
  33. coverage.Item.Warranty = warranty
  34. cr.Coverages = append(cr.Coverages, coverage)
  35. } else {
  36. var insurance *vcsproto.Insurance
  37. insurance.Type = item.Type
  38. insurance.PolicyNumber = item.PolicyNumber
  39. insurance.AgreementName = item.AgreementName
  40. insurance.AppliedTo = item.AppliedTo
  41. insurance.ValidFrom = item.ValidFrom
  42. insurance.ValidTo = item.ValidTo.
  43. insurance.Deductible.Currency = item.Deductible.Currency
  44. insurance.Deductible.Amount = item.Deductible.Amount
  45. var coverage *vcsproto.Coverage
  46. coverage.Item.Insurance = Insurance
  47. cr.Coverages = append(cr.Coverages, coverage)
  48. }
  49. }

答案1

得分: 1

请看这里生成的代码这里

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

还要注意,在你的代码中,你赋值了coverage.Item.Insurance = Insurance - 你可能想要将第二个Insurance改为变量insurance

英文:

Take a look in the generated code here.

You do not assign directly to the Warranty nor Insurance subfields, but directly to the Item field. so coverage.Item = {insurance/warranty} should work.

Also note that in your code you're assigning coverage.Item.Insurance = Insurance - you probably meant the second Insurance to be the variable insurance instead.

huangapple
  • 本文由 发表于 2021年10月5日 19:41:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/69449795.html
匿名

发表评论

匿名网友

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

确定