英文:
How do I assign value to a repeated oneof field in a protobuf message?
问题
我有一个名为CoverageReport的protobuf消息,结构如下:
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消息。但是似乎无法给oneof字段赋值!
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)
	}
}
以上是你提供的代码的翻译。
英文:
I have a protobuf message CoverageReport like this:
 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;
}
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!
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)
}
}
答案1
得分: 1
请看这里生成的代码这里。
你不直接给Warranty或Insurance子字段赋值,而是直接给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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论