Go – Enforce that an interface is only satisfied by types with a pointer receiver on a method?

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

Go - Enforce that an interface is only satisfied by types with a pointer receiver on a method?

问题

我正在进行一些关于类型参数的实验,以找到一种通用的方法来连接生成响应JSON HTTP请求的结构体。

Method 接口是结构体必须实现的,它有一个 SetParams 方法。只要实现使用了指针接收器,它就会按预期工作。

我的问题是:如果 SetParams 使用值接收器,有没有办法在编译时将其作为一个错误?

下面是一个示例,演示了具有值接收器的 SetParams 的问题:

package main

import (
	"encoding/json"
	"fmt"
	"log"
)

type PingParams struct {
	Name string
}

type PingResponse struct {
	Message string
}

func (p PingParams) Greeting() string {
	if p.Name != "" {
		return fmt.Sprintf("Hello, %s", p.Name)
	}

	return fmt.Sprintf("Hello, nobody!")
}

type GoodPing struct {
	Params PingParams
}

// SetParams 使用了指针接收器。
func (m *GoodPing) SetParams(p PingParams) {
	fmt.Printf("assign %v with pointer receiver, Good!\n", p)
	m.Params = p
}
func (m GoodPing) Run() (*PingResponse, error) {
	return &PingResponse{Message: fmt.Sprintf("%T %s", m, m.Params.Greeting())}, nil
}

type BadPing struct {
	Params PingParams
}

// SetParams 使用了值接收器。
func (m BadPing) SetParams(p PingParams) {
	fmt.Printf("assign %v with value receiver, Bad!\n", p)
	m.Params = p
}
func (m BadPing) Run() (*PingResponse, error) {
	return &PingResponse{Message: fmt.Sprintf("%T %s", m, m.Params.Greeting())}, nil
}

type Method[M, RQ, RS any] interface {
	// Run 构建 RPC 结果。
	Run() (*RS, error)
	// SetParams 用于在实现 RPC 方法的结构体中设置请求参数。
	// 这样可以使请求参数在 Method 结构体的所有方法中轻松获取。
	// 该方法必须有一个指针接收器。这在编译时不被强制执行。
	SetParams(p RQ)
	// 下面的行要求实现类型是 M 的指针。
	*M
	// https://stackoverflow.com/a/72090807
}

func HandlerMethod[M, RQ, RS any, T Method[M, RQ, RS]](in json.RawMessage) (*RS, error) {
	// 这个函数的真实实现将返回一个用于连接到请求路由器的函数

	var req RQ

	err := json.Unmarshal(in, &req)

	if err != nil {
		return nil, err
	}

	var m T = new(M)

	m.SetParams(req)

	return m.Run()
}

func main() {

	payload := []byte(`{"Name": "Mark"}`)

	bad, err := HandlerMethod[BadPing, PingParams, PingResponse](payload)

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(bad.Message)

	good, err := HandlerMethod[GoodPing, PingParams, PingResponse](payload)

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(good.Message)
}

https://go.dev/play/p/Eii8ADkmDxE

英文:

I'm doing some experimentation with type parameters to come up with a generic way of wiring up structs that generate a response to JSON HTTP requests.

The Method interface which the structs must implement has a SetParams method. This will work as expected as long as the implementation uses a pointer receiver.

My question: Is there any way of making this a compile time error if SetParams has a value receiver?

Here is an example demonstrating the problem with a SetParams that has a value receiver:

package main
import (
"encoding/json"
"fmt"
"log"
)
type PingParams struct {
Name string
}
type PingResponse struct {
Message string
}
func (p PingParams) Greeting() string {
if p.Name != "" {
return fmt.Sprintf("Hello, %s", p.Name)
}
return fmt.Sprintf("Hello, nobody!")
}
type GoodPing struct {
Params PingParams
}
// SetParams has a pointer receiver.
func (m *GoodPing) SetParams(p PingParams) {
fmt.Printf("assign %v with pointer receiver, Good!\n", p)
m.Params = p
}
func (m GoodPing) Run() (*PingResponse, error) {
return &PingResponse{Message: fmt.Sprintf("%T %s", m, m.Params.Greeting())}, nil
}
type BadPing struct {
Params PingParams
}
// SetParams has a value receiver.
func (m BadPing) SetParams(p PingParams) {
fmt.Printf("assign %v with value receiver, Bad!\n", p)
m.Params = p
}
func (m BadPing) Run() (*PingResponse, error) {
return &PingResponse{Message: fmt.Sprintf("%T %s", m, m.Params.Greeting())}, nil
}
type Method[M, RQ, RS any] interface {
// Run builds the RPC result.
Run() (*RS, error)
// SetParams is intended to set the request parameters in the struct implementing the RPC method.
// This then allows the request parameters to be easily available to all methods of the Method struct.
// The method MUST have a pointer receiver. This is NOT enforced at compile time.
SetParams(p RQ)
// The following line requires the implementing type is a pointer to M.
*M
// https://stackoverflow.com/a/72090807
}
func HandlerMethod[M, RQ, RS any, T Method[M, RQ, RS]](in json.RawMessage) (*RS, error) {
// A real implementation of this would return a func for wiring into a request router
var req RQ
err := json.Unmarshal(in, &req)
if err != nil {
return nil, err
}
var m T = new(M)
m.SetParams(req)
return m.Run()
}
func main() {
payload := []byte(`{"Name": "Mark"}`)
bad, err := HandlerMethod[BadPing, PingParams, PingResponse](payload)
if err != nil {
log.Fatal(err)
}
fmt.Println(bad.Message)
good, err := HandlerMethod[GoodPing, PingParams, PingResponse](payload)
if err != nil {
log.Fatal(err)
}
fmt.Println(good.Message)
}

https://go.dev/play/p/Eii8ADkmDxE

答案1

得分: 3

你不能这样做。

当你在代码中这样写:

var m T = new(M)

即使 T 的类型集只包含 *M 作为类型项,*M 的方法集中包含在 M 上声明的方法。编译器无法为你检查方法如何出现在 *M 的方法集中。

在声明 BadPing 上的方法 SetParam 时,你有责任确保该方法不会试图无效地修改接收器。

英文:

You can't do that.

When in your code you do this:

var m T = new(M)

even if T's type set includes only *M as a type term, *M's method set includes methods declared on M. The compiler can't check for you how the method ends up in *M's method set.

It is your responsibility when declaring the method SetParam on BadPing to make sure that the method doesn't attempt to unfruitfully modify the receiver.

huangapple
  • 本文由 发表于 2022年11月17日 23:36:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/74478173.html
匿名

发表评论

匿名网友

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

确定