doudou20080720 2017-07-17 10:58
浏览 131
已采纳

Golang JSON解码器无法识别类型

Still a Golang beginner, I am trying to code a generic function to serve ReST requests. I pass a function to create a new resource (struct) with an interface implemented on it, because I would also invoke methods on the struct. When decoding the JSON, logging the type shows the correct (struct) type, but the JSON decoder seems to only recognize the interface, which it cannot decode to.

package main

import (
    "encoding/json"
    "github.com/julienschmidt/httprouter"
    "log"
    "net/http"
    "strings"
)

// general resource interface
type resource interface {
    // check semantics and return an array of errors or nil if no error found
    check() []string
    // update the resource in backend
    update() error
}

// specific resource named "anchor"
type anchor struct {
    ID   string `json:"id"`
    Name string `json:"name"`
}

func newAnchor() resource {
    return anchor{}
}

func (a anchor) check() []string {
    return nil
}

func (a anchor) update() error {
    return nil
}

// generic function to create (POST) a new resource
func restCreate(newResource func() resource) httprouter.Handle {
    return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
        const F = "restCreate"
        var checkErrs []string

        res := newResource()
        log.Printf("%s res type %T
", F, res)
        dcdr := json.NewDecoder(r.Body)
        err := dcdr.Decode(&res)
        log.Printf("%s Unmarshalled into %T: %+v
", F, res, res)
        if err == nil {
            checkErrs = res.check()
        }
        switch {
        case err != nil:
            w.WriteHeader(http.StatusInternalServerError)
            log.Printf("[ERR] %s: %v
", F, err)
        case checkErrs != nil:
            w.WriteHeader(http.StatusBadRequest)
            w.Write([]byte(strings.Join(checkErrs, "
")))
            log.Printf("%s: %v
", F, err)
        default:
            res.update()
            bs, _ := json.Marshal(res)
            w.Write(bs)
        }
    }
}

func main() {
    r := httprouter.New()
    r.POST("/anchors", restCreate(newAnchor))
    http.ListenAndServe(":8080", r)
}

The execution log shows:

restCreate res type main.anchor
restCreate Unmarshalled into main.anchor: {ID: Name:}
[ERR] restCreate: json: cannot unmarshal object into Go value of type main.resource

Why does Printf show the struct type and json.Decoder the interface?
I'd appreciate any indicator on what's going wrong and how to solve this in a generic way...

  • 写回答

2条回答 默认 最新

  • dongwuge6201 2017-07-17 19:14
    关注

    It is because you try to use a pointer to the interface to unmarshal into. You need return a pointer in a function

    func newAnchor() resource {
        return &anchor{}
    }
    

    And you don't need to get address in this line: err := dcdr.Decode(&res)

    Here is small working example: https://play.golang.org/p/3E0RmGTURO

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3