-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Go] Conform to Golang idioms and best practices #3518
Comments
@neilotoole sounds good to me as long as it conforms to the "Go Code Review Comments" cc @guohuang |
discovered a small bit on swagger-codegen-cli-2.2.3.jar and fixed in #6669 But after building with master, it looks like the refactor for 2.3.0 introduced new issues.
@@ -12,22 +12,69 @@ package api
import (
"bytes"
+ "encoding/json"
+ "encoding/xml"
"fmt"
- "io/ioutil"
+ "errors"
+ "io"
+ "mime/multipart"
+ "golang.org/x/oauth2"
+ "golang.org/x/net/context"
+ "net/http"
"net/url"
+ "time"
+ "os"
"path/filepath"
"reflect"
+ "regexp"
"strings"
+ "unicode/utf8"
+ "strconv"
+) it ought to be like import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"unicode/utf8"
"golang.org/x/net/context"
"golang.org/x/oauth2"
)
+var (
+ jsonCheck = regexp.MustCompile("(?i:[application|text]/json)")
+ xmlCheck = regexp.MustCompile("(?i:[application|text]/xml)")
) it ought to be like jsonCheck = regexp.MustCompile("(?i:[application|text]/json)")
xmlCheck = regexp.MustCompile("(?i:[application|text]/xml)") This is true for fields of a struct as well, they align to the type, as well as the tag on the field. type Configuration struct {
- BasePath string `json:"basePath,omitempty"`
- Host string `json:"host,omitempty"`
- Scheme string `json:"scheme,omitempty"`
- DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
- UserAgent string `json:"userAgent,omitempty"`
- HTTPClient *http.Client
+ BasePath string `json:"basePath,omitempty"`
+ Host string `json:"host,omitempty"`
+ Scheme string `json:"scheme,omitempty"`
+ DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
+ UserAgent string `json:"userAgent,omitempty"`
+ HTTPClient *http.Client
}
// Change base path to allow switching to mocks
-func (c *APIClient) ChangeBasePath (path string) {
+func (c *APIClient) ChangeBasePath(path string) {
c.cfg.BasePath = path
// Prevent trying to import "fmt"
-func reportError(format string, a ...interface{}) (error) {
+func reportError(format string, a ...interface{}) error {
return fmt.Errorf(format, a...)
// to determine the Content-Type header
- localVarHttpContentTypes := []string{ }
+ localVarHttpContentTypes := []string{}
// set Content-Type header
localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes)
@@ -69,8 +69,7 @@ func (a *GrafeasApiService) CreateNote(projectsId string, localVarOptionals map[
}
// to determine the Accept header
- localVarHttpHeaderAccepts := []string{
- }
+ localVarHttpHeaderAccepts := []string{} |
Description
There are several locations where the generated Go code does not conform to standard Go idioms / conventions / best practices. Taking CodeReviewComments as a primary source, and generally trying to follow the standards in the Go std lib and in popular OSS libraries.
For example:
ProjectApi
should beProjectAPI
.Configuration
object should be namedConfig
.APIClient
at all? What's the use case for having this public?Swagger-codegen version
master
The text was updated successfully, but these errors were encountered: