我如何让这个 Go 代码更 DRY?

Ass*_*vie 4 dry go

我正在为 REST API 实现一个 Go 包装器。它基本上解析 JSON 并应该返回适​​当的结构类型。我发现自己做了很多这样的事情:

// GetBlueprintDetails returns details about a blueprint
func (c *Client) GetBlueprintDetails(projectID string, blueprintID string) (*BlueprintDetails, *APIError) {
    path := fmt.Sprintf("projects/%s/blueprints/%s", projectID, blueprintID)
    res, err := c.Request("GET", path, nil, nil)
    if err != nil {
        return nil, err
    }
    var ret BlueprintDetails
    e := json.Unmarshal(res.Body, &ret)
    if e != nil {
        return nil, &APIError{Error: &e}
    }
    return &ret, nil
}

// GetProjects returns a list of projects for the user
func (c *Client) GetProjects() (*[]Project, *APIError) {
    res, err := c.Request("GET", "projects", nil, nil)
    if err != nil {
        return nil, err
    }
    var ret []Project
    e := json.Unmarshal(res.Body, &ret)
    if e != nil {
        return nil, &APIError{Error: &e}
    }
    return &ret, nil
}
Run Code Online (Sandbox Code Playgroud)

这两个函数之间的唯一区别基本上是未编组结构的类型。我知道 Go 中没有泛型,但必须有一种模式来使其更加 DRY。

有任何想法吗?

Sar*_*lai 5

您可以创建一个MakeRequest函数来执行 http 请求部分并将 json 解组为 struct

这是你可以做到的,看看MakeRequest功能

// GetBlueprintDetails returns details about a blueprint
func (c *Client) GetBlueprintDetails(projectID string, blueprintID string) (*BlueprintDetails, *APIError) {
    path := fmt.Sprintf("projects/%s/blueprints/%s", projectID, blueprintID)
    bluePrintDetails = new(BlueprintDetails)
    err := c.MakeRequest("GET", path, bluePrintDetails)
    return bluePrintDetails, err
}

// GetProjects returns a list of projects for the user
func (c *Client) GetProjects() (*[]Project, *APIError) {
    projects = make([]Project, 0)
    err := c.MakeRequest("GET", "project", &projects)
    return &projects, err
}

func (c *Client) MakeRequest(method string, path string, response interface{}) *APIError {
    res, err := c.Request(method, path, nil, nil)
    if err != nil {
        return nil, err
    }
    e := json.Unmarshal(res.Body, response)
    if e != nil {
        return &APIError{Error: &e}
    }
    return nil
}
Run Code Online (Sandbox Code Playgroud)