Interface/Struct "Does not implement X, Wrong type or Method, not sure why I am getting this error

huangapple go评论87阅读模式
英文:

Interface/Struct "Does not implement X, Wrong type or Method, not sure why I am getting this error

问题

大家好,我对Golang还比较新手。我了解到接口(interfaces)有点像是一种合约,保证了某些事物以特定方式运作,这很酷,如果我创建一个它的本地副本,我基本上可以重新编写它的运作方式(根据我理解的情况,如果我理解错了,请纠正我)。

以下是我目前的代码:

package register

import (
	"log"
	"net/http"

	"github.com/yohcop/openid-go"
)

var nonceStore = &openid.SimpleNonceStore{
	Store: make(map[string][]*openid.Nonce)}
var discoveryCache = &SimpleDiscoveryCache{}

type DiscoveredInfo interface {
	OpEndpoint() string
	OPLocalID() string
	ClaimedID() string
}

type SimpleDiscoveredInfo struct {
	opEndpoint, opLocalID, claimedID string
}

type SimpleDiscoveryCache map[string]DiscoveredInfo

func (s *SimpleDiscoveryCache) Put(id string, info DiscoveredInfo) {
	db := common.ConnectDB()

	rows, err := db.Query("INSERT INTO discovery_cache SET id=?, opendpoint=?, oplocalid=?, claimedid=?",
		id, info.OpEndpoint(), info.OPLocalID(), info.ClaimedID())

	if err != nil {
		panic("Error: " + err.Error())
	}

	log.Println(rows)
}

func (s *SimpleDiscoveryCache) Get(id string) DiscoveredInfo {
	db := common.ConnectDB()

	rows, err := db.Query("SELECT FROM discovery_cache WHERE id=?", id)
	if err != nil {
		panic("Error: " + err.Error())
	}

	log.Println(rows)

	var opEndpoint, opLocalID, claimedID string

	for rows.Next() {
		err := rows.Scan(&opEndpoint, &opLocalID, &claimedID)
		if err != nil {
			panic("Help!")
		}
	}

	return &SimpleDiscoveredInfo{
		opEndpoint, opLocalID, claimedID,
	}

}

func DiscoverHandler(w http.ResponseWriter, r *http.Request) {
	url, err := openid.RedirectURL("http://steamcommunity.com/openid", "http://localhost:1337/login/return", "http://localhost")

	if err != nil {
		http.Error(w, "Failed to login", 500)
	}

	http.Redirect(w, r, url, 303)
}

func CallbackHandler(w http.ResponseWriter, r *http.Request) {

	fullUrl := "http://localhost:1337" + r.URL.String()
	id, err := openid.Verify(fullUrl, discoveryCache, nonceStore)
	if err != nil {
		http.Error(w, "Failed", 500)
	}
	log.Println(id)

}

基本上,我正在尝试创建自己的DiscoveryCache,以便它使用数据库而不是内存进行存储(根据Go-OpenID包的指示进行操作,该包位于此处:https://github.com/yohcop/openid-go)。

我正在尝试重新创建的部分位于此处:https://github.com/yohcop/openid-go/blob/master/discovery_cache.go

现在,我已经做了(我认为)应该做的一切来使其工作,但我一直收到以下错误:

controllers/register/register.go:60: cannot use SimpleDiscoveredInfo literal (type *SimpleDiscoveredInfo) as type openid.DiscoveredInfo in return argument:
*SimpleDiscoveredInfo does not implement openid.DiscoveredInfo (missing ClaimedID method)
controllers/register/register.go:78: cannot use discoveryCache (type *SimpleDiscoveryCache) as type openid.DiscoveryCache in argument to openid.Verify:
*SimpleDiscoveryCache does not implement openid.DiscoveryCache (wrong type for Put method)
have Put(string, DiscoveredInfo)
want Put(string, openid.DiscoveredInfo)

如果有人能告诉我我做错了什么,我将不胜感激。谢谢!如果您需要更多信息,请告诉我。

英文:

Hi guys fairly new to Golang, I understand that interfaces are kind of like contracts that guarantee that certain things will operate a certain way, thats cool and all, and if I make a local copy of it I can basically re-write how it operates (From what I understand, please correct me if I'm wrong)

Here is what I have so far

package register
import (
"log"
"net/http"
"github.com/yohcop/openid-go"
)
var nonceStore = &openid.SimpleNonceStore{
Store: make(map[string][]*openid.Nonce)}
var discoveryCache = &SimpleDiscoveryCache{}
type DiscoveredInfo interface {
OpEndpoint() string
OPLocalID() string
ClaimedID() string
}
type SimpleDiscoveredInfo struct {
opEndpoint, opLocalID, claimedID string
}
type SimpleDiscoveryCache map[string]DiscoveredInfo
func (s *SimpleDiscoveryCache) Put(id string, info DiscoveredInfo) {
db := common.ConnectDB()
rows, err := db.Query("INSERT INTO discovery_cache SET id=?, opendpoint=?, oplocalid=?, claimedid=?",
id, info.OpEndpoint(), info.OPLocalID(), info.ClaimedID())
if err != nil {
panic("Error: " + err.Error())
}
log.Println(rows)
}
func (s *SimpleDiscoveryCache) Get(id string) DiscoveredInfo {
db := common.ConnectDB()
rows, err := db.Query("SELECT FROM discovery_cache WHERE id=?", id)
if err != nil {
panic("Error: " + err.Error())
}
log.Println(rows)
var opEndpoint, opLocalID, claimedID string
for rows.Next() {
err := rows.Scan(&opEndpoint, &opLocalID, &claimedID)
if err != nil {
panic("Help!")
}
}
return &SimpleDiscoveredInfo{
opEndpoint, opLocalID, claimedID,
}
}
func DiscoverHandler(w http.ResponseWriter, r *http.Request) {
url, err := openid.RedirectURL("http://steamcommunity.com/openid", "http://localhost:1337/login/return", "http://localhost")
if err != nil {
http.Error(w, "Failed to login", 500)
}
http.Redirect(w, r, url, 303)
}
func CallbackHandler(w http.ResponseWriter, r *http.Request) {
fullUrl := "http://localhost:1337" + r.URL.String()
id, err := openid.Verify(fullUrl, discoveryCache, nonceStore)
if err != nil {
http.Error(w, "Failed", 500)
}
log.Println(id)
}

Basically I am trying to make my own DiscoveryCache so that it uses a database instead of memory for storage (as instructed to do by the Go-OpenID package located here: https://github.com/yohcop/openid-go

The part I'm trying to recreate is located here: https://github.com/yohcop/openid-go/blob/master/discovery_cache.go

Now I have done (what I assume) everything that should need doing to make this work, but I keep getting this error:

controllers/register/register.go:60: cannot use SimpleDiscoveredInfo literal (type *SimpleDiscoveredInfo) as type openid.DiscoveredInfo in return argument:
*SimpleDiscoveredInfo does not implement openid.DiscoveredInfo (missing ClaimedID method)
controllers/register/register.go:78: cannot use discoveryCache (type *SimpleDiscoveryCache) as type openid.DiscoveryCache in argument to openid.Verify:
*SimpleDiscoveryCache does not implement openid.DiscoveryCache (wrong type for Put method)
have Put(string, DiscoveredInfo)
want Put(string, openid.DiscoveredInfo)

If anybody could inform me on what I have done wrong that would be much appreciated. Thanks! If you need any more information please let me know.

答案1

得分: 3

SimpleDiscoveredInfo没有实现接口的方法,你需要像这样做:

func (sdi *SimpleDiscoveredInfo) OpEndpoint() string { return sdi.opEndpoint }
func (sdi *SimpleDiscoveredInfo) OpLocalID() string  { return sdi.opLocalID }
func (sdi *SimpleDiscoveredInfo) ClaimedID() string  { return sdi.claimedID }

var _ openid.DiscoveredInfo = (*SimpleDiscoveredInfo)(nil)

链接:http://play.golang.org/p/qVTTKfhNHu

对于以下错误:

controllers/register/register.go:78: cannot use discoveryCache (type *SimpleDiscoveryCache) as type openid.DiscoveryCache in argument to openid.Verify:
    *SimpleDiscoveryCache does not implement openid.DiscoveryCache (wrong type for Put method)
        have Put(string, DiscoveredInfo)
        want Put(string, openid.DiscoveredInfo)

你的类型需要返回 openid.DiscoveredInfo 而不是 DiscoveredInfo

英文:

SimpleDiscoveredInfo doesn't implement the interface's methods, you need something like this:

func (sdi *SimpleDiscoveredInfo) OpEndpoint() string { return sdi.opEndpoint }
func (sdi *SimpleDiscoveredInfo) OpLocalID() string  { return sdi.opLocalID }
func (sdi *SimpleDiscoveredInfo) ClaimedID() string  { return sdi.claimedID }
var _ openid.DiscoveredInfo = (*SimpleDiscoveredInfo)(nil)

http://play.golang.org/p/qVTTKfhNHu

For

controllers/register/register.go:78: cannot use discoveryCache (type *SimpleDiscoveryCache) as type openid.DiscoveryCache in argument to openid.Verify:
*SimpleDiscoveryCache does not implement openid.DiscoveryCache (wrong type for Put method)
have Put(string, DiscoveredInfo)
want Put(string, openid.DiscoveredInfo)

Your types need to return openid.DiscoveredInfo not DiscoveredInfo.

huangapple
  • 本文由 发表于 2015年8月13日 20:06:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/31987769.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定