我希望改进下面代码中的getCustomerFromDTO方法,需要从interface{}创建一个struct,目前需要将该interface序列化为byte[],然后再将数组反序列化到我的struct中 - 必须有更好的方法。
我的使用情况是通过rabbitmq发送structs,并使用此通用DTO包装器发送它们,该包装器具有有关它们的附加特定于域的数据。当我从rabbit mq接收DTO时,消息的下一层解组为我的DTO,然后我需要从该DTO获取我的struct。
type Customer struct {
Name string `json:"name"`
}
type UniversalDTO struct {
Data interface{} `json:"data"`
// more fields with important meta-data about the message...
}
func main() {
// create a customer, add it to DTO object and marshal it
customer := Customer{Name: "Ben"}
dtoToSend := UniversalDTO{customer}
byteData, _ := json.Marshal(dtoToSend)
// unmarshal it (usually after receiving bytes from somewhere)
receivedDTO := UniversalDTO{}
json.Unmarshal(byteData, &receivedDTO)
//Attempt to unmarshall our customer
receivedCustomer := getCustomerFromDTO(receivedDTO.Data)
fmt.Println(receivedCustomer)
}
func getCustomerFromDTO(data interface{}) Customer {
customer := Customer{}
bodyBytes, _ := json.Marshal(data)
json.Unmarshal(bodyBytes, &customer)
return customer
}
Data
字段类型保留为interface{}
。您需要做的就是将该值预初始化为您希望返回的类型。这并不会使其变得不够通用。如果您不想预初始化DTO,则可以将初始化后的值传递给包中的函数。例如mypackage.UnmarshalDTO(byteData []byte, data interface{}) ...
,然后在该函数中将数据设置为数据字段,在解组完成后,您的数据变量将被正确解组。 - mkoprivajson.RawMessage
而不是interface{}
,并在已知结构体类型时进行反序列化。 - JUSHJUSH