2017-04-10 134 views
1

我想在下面的代码中改进getCustomerFromDTO方法,我需要从接口{}创建一个结构,并且当前我需要将该接口编组为byte [],然后解组该数组到我的结构 - 必须有更好的方法。Golang convert interface {} to struct

我的用例是,我通过rabbitmq发送结构并发送它们,我使用这个通用的DTO包装器,它具有关于它们的其他特定于域的数据。 当我收到来自rabbit mq的DTO时,消息下面的一层被解组到我的DTO,然后我需要从该DTO获取我的结构。

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 
} 

回答

7

解编DTO之前,请将Data字段设置为您所期望的类型。

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) 
    receivedCustomer := &Customer{} 
    receivedDTO := UniversalDTO{Data: receivedCustomer} 
    json.Unmarshal(byteData, &receivedDTO) 

    //done 
    fmt.Println(receivedCustomer) 
} 

如果你没有初始化的DTO的Data领域的能力,这是取消封送之前,你可以在拆封后使用类型断言。包装encoding/json unamrshals interface{}类型值转换为map[string]interface{},让您的代码会是这个样子:

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 { 
    m := data.(map[string]interface{}) 
    customer := Customer{} 
    if name, ok := m["name"].(string); ok { 
     customer.Name = name 
    } 
    return customer 
} 
+0

的DTO拆封由包我用做它是所有接收到的消息是相同的。在我的应用程序中,我收到了一个已组装的DTO – Tomas

+0

而您无法修改此软件包?这是第三方吗? – mkopriva

+0

我可以修改它,但由于它是一个通用包,在解组DTO时,我不知道它携带的结构类型是数据字段 – Tomas