如何让这个Go代码更干?

时间:2016-12-30 05:31:44

标签: go dry

我正在为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
}

两个函数之间的唯一区别是基本上是unmarshaled结构的类型。我知道Go中没有泛型,但必须有一种模式可以让它更干燥。

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

您可以创建一个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
}