gpt4 book ai didi

处理不同类型的 Golang 处理程序

转载 作者:IT王子 更新时间:2023-10-29 01:19:35 25 4
gpt4 key购买 nike

这些是我在研究 gorilla/mux 时在网上找到的模式中的 AppHandler。它们是满足 http.Handler 的结构的一部分。如果您注意到,以下两个 block 完全相同。实际上,它们可以作为字符串传递“变体”(“流”或“过程”)。

func CreateFlow(a *AppContext, w http.ResponseWriter, r *http.Request) (int, error) {

highest, code, err := a.Create("flow", r)
if code != 200 || err != nil {
return code, err
}

b := new(bytes.Buffer)
json.NewEncoder(b).Encode(struct {
Highest int `json:"id"`
}{highest})
w.Header().Set("Content-Type", "application/json")
w.Write(b.Bytes())
return 200, nil
}

func CreateProcess(a *AppContext, w http.ResponseWriter, r *http.Request) (int, error) {

highest, code, err := a.Create("process", r)
if code != 200 || err != nil {
return code, err
}

b := new(bytes.Buffer)
json.NewEncoder(b).Encode(struct {
Highest int `json:"id"`
}{highest})
w.Header().Set("Content-Type", "application/json")
w.Write(b.Bytes())
return 200, nil
}

但是,以下两个 block 不仅需要字符串,而且还需要关联类型的变量(“Flow”和“Process”)才能成功解码我从 ElasticSearch 获得的命中。除此之外,它们是相同的代码。

func GetFlow(a *AppContext, w http.ResponseWriter, r *http.Request) (int, error) {

hit, code, err := a.GetByID("flow", mux.Vars(r)["id"], r)
if code != 200 {
return code, err
}

var flow Flow

err = json.Unmarshal(*hit.Source, &flow)
if err != nil {
return 500, err
}

flow.ESID = hit.Id

b := new(bytes.Buffer)
json.NewEncoder(b).Encode(flow)
w.Header().Set("Content-Type", "application/json")
w.Write(b.Bytes())
return 200, nil
}

func GetProcess(a *AppContext, w http.ResponseWriter, r *http.Request) (int, error) {

hit, code, err := a.GetByID("process", mux.Vars(r)["id"], r)
if code != 200 {
return code, err
}

var process Process

err = json.Unmarshal(*hit.Source, &process)
if err != nil {
return 500, err
}

process.ESID = hit.Id

b := new(bytes.Buffer)
json.NewEncoder(b).Encode(process)
w.Header().Set("Content-Type", "application/json")
w.Write(b.Bytes())
return 200, nil
}

当涉及声明的类型时,我不确定如何在 golang 中概括此行为。这些处理程序也都在同一个包中,因为我认为它们都在完成类似的任务。我在代码中非常清楚地重复自己,但我需要有关如何改进的建议。我已经过了“一点点复制胜过一点点依赖”。但我害怕因为“反射永远不会清晰”。

这是使用其中一个函数在 main 中声明的示例。

api.Handle("/flow/{id:[0-9]+}", handlers.AppHandler{context, handlers.GetFlow}).Methods("GET")

最佳答案

您可以通过传入必要类型的范例来完成此操作,方法与 Unmarshal 相同:

func GetFlow(a *AppContext, w http.ResponseWriter, r *http.Request) (int, error) {
return GetThing(a,w,r,"flow",new(Flow))
}

func GetProcess(a *AppContext, w http.ResponseWriter, r *http.Request) (int, error) {
return GetThing(a,w,r,"process",new(Process))
}

func GetThing(a *AppContext, w http.ResponseWriter, r *http.Request, t string, ob Elastible{}) (int, error) {
hit, code, err := a.GetByID(t, mux.Vars(r)["id"], r)
if code != 200 {
return code, err
}

err = json.Unmarshal(*hit.Source, ob)
if err != nil {
return 500, err
}

ob.SetESID(hit.Id)

b := new(bytes.Buffer)
json.NewEncoder(b).Encode(ob)
w.Header().Set("Content-Type", "application/json")
w.Write(b.Bytes())
return 200, nil
}

type Elastible interface {
SetESID(id ESIDType) // whatever type ESID is, not clear from example
}

func (f *Flow) SetESID(id ESIDType) {
f.ESID = id
}

此代码未经测试(因为我没有您的结构定义或其他相关代码)但我希望它能理解这个想法。

关于处理不同类型的 Golang 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45845181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com