doulu4203
2016-11-06 19:02
浏览 307
已采纳

如何在Golang中解组reflect.Value

I got this test

func (t *DeviceTests) CreatePublicDevice() {
    registerRegularDevice := tester.TableTest{
        Method:      "POST",
        Path:        "/iot/devices",
        Status:      http.StatusOK,
        Name:        "CreatePublicDevice",
        Description: "register Regular Device has to return 200",
        Body:        PublicDevice,
    }
    resp := registerRegularDevice.DoubleSpin(t.Test)
    log.Println(resp.(types.Device).ID)

}

I want to keep this in a separate package so I can reuse on diferent projects.

func (test TableTest) DoubleSpin(t *testing.T) interface{} {
    actualBody := test.innnerSpin(t)
    log.Print(string(actualBody))
    thetype := reflect.TypeOf(test.Body)
    log.Println(thetype)
    receivedev := reflect.Zero(thetype)
    err := json.Unmarshal(actualBody, receivedev.Interface())
    assert.NoError(t, err)
    log.Println(receivedev)
    return receivedev.Interface()
}

Logs say:

2016/11/06 16:54:01 {"id":"581f7c49b2c79a543c627474","hostname":"Shriekersolar","alias":"my PublicDevice","channels":8,"owner":"public","location":{}}
2016/11/06 16:54:01 types.Device
2016/11/06 16:54:01 {ObjectIdHex("")   0  map[]}
2016/11/06 16:54:01 ObjectIdHex("")

Where a device looks like:

type Device struct {
    ID       bson.ObjectId     `json:"id" bson:"_id,omitempty"`
    Hostname string            `json:"hostname"`
    Alias    string            `json:"alias"`
    Channels int               `json:"channels"`
    Owner    string            `json:"owner"`
    Location map[string]string `json:"location"`
}

图片转代码服务由CSDN问答提供 功能建议

我得到了此测试

  func(t * DeviceTests)  CreatePublicDevice(){
 registerRegularDevice:= tester.TableTest {
方法:“ POST”,
路径:“ / iot / devices”,
状态:http.StatusOK,
名称:“ CreatePublicDevice”,
 说明:“注册常规设备必须返回200”,
正文:PublicDevice,
} 
 resp:= registerRegularDevice.DoubleSpin(t.Test)
 log.Println(resp。(types.Device).ID)  
 
} 
   
 
 

我想将其保存在单独的程序包中,以便可以在不同的项目上重复使用。

   func(测试TableTest)DoubleSpin(t * testing.T)接口{} {
 actualBody:= test.innnerSpin(t)
 log.Print(string(actualBody))
主题类型:= reflect.TypeOf  (test.Body)
 log.Println(thetype)
 receiveev:= reflect.Zero(thetype)
 err:= json.Unmarshal(actualBody,receiveevevInterface())
 assert.NoError(t,err  )
 log.Pri  ntln(receivedev)
返回receiveev.Interface()
} 
   
 
 

日志说:

  2016  / 11/06 16:54:01 {“ id”:“ 581f7c49b2c79a543c627474”,“主机名”:“ Shriekersolar”,“别名”:“我的PublicDevice”,“频道”:8,“所有者”:“公共”,“  location“:{}} 
2016 / 11/06 16:54:01 types.Device 
2016 / 11/06 16:54:01 {ObjectIdHex(”“)0 map []} 
2016 / 11/06 16:  54:01 ObjectIdHex(“”)
   
 
 

设备外观如下:

  type设备结构{\  n ID bson.ObjectId`json:“ id” bson:“ _ id,omitempty”`
主机名字符串`json:“主机名”`
别名字符串`json:“ alias”`
 Channels int`json:“ channels  “`
所有者字符串`json:” owner“`
位置图[string]字符串`json:” location“`
} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqzd92796 2016-11-06 20:30
    已采纳

    Use reflect.New rather than reflect.Zero to get a pointer:

    thetype := reflect.TypeOf(i)
    receivedev := reflect.New(thetype)
    err := json.Unmarshal(actualBody, receivedev.Interface())
    if err != nil {
        panic(err)
    }
    

    Playground: https://play.golang.org/p/pGXRWpBFiF.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题