Skip to content

Commit

Permalink
fix nlreturn lint
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Nov 17, 2024
1 parent de38630 commit 2861dd4
Show file tree
Hide file tree
Showing 35 changed files with 162 additions and 3 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ linters:
- wsl
- wrapcheck
- varnamelen
- nlreturn
- exhaustive
- exhaustruct
- gocyclo
Expand Down
1 change: 1 addition & 0 deletions connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (c *HTTPConnector) ParseConfiguration(ctx context.Context, configurationDir
schemas, errs = configuration.BuildSchemaFromConfig(config, configurationDir, logger)
if len(errs) > 0 {
printSchemaValidationError(logger, errs)

return nil, errBuildSchemaFailed
}
}
Expand Down
6 changes: 6 additions & 0 deletions connector/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,14 @@ func evalHTTPResponse(ctx context.Context, span trace.Span, resp *http.Response,
if len(respBody) == 0 {
return nil, resp.Header, nil
}

return string(respBody), resp.Header, nil
case "text/plain", "text/html":
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, schema.NewConnectorError(http.StatusInternalServerError, err.Error(), nil)
}

return string(respBody), resp.Header, nil
case rest.ContentTypeJSON:
if len(resultType) > 0 {
Expand All @@ -388,6 +390,7 @@ func evalHTTPResponse(ctx context.Context, span trace.Span, resp *http.Response,
// fallback to raw string response if the result type is String
return string(respBytes), resp.Header, nil
}

return strResult, resp.Header, nil
}
}
Expand All @@ -405,6 +408,7 @@ func evalHTTPResponse(ctx context.Context, span trace.Span, resp *http.Response,
if err != nil {
return nil, nil, schema.InternalServerError(err.Error(), nil)
}

return result, resp.Header, nil
case rest.ContentTypeNdJSON:
var results []any
Expand All @@ -425,6 +429,7 @@ func evalHTTPResponse(ctx context.Context, span trace.Span, resp *http.Response,
if err != nil {
return nil, nil, schema.InternalServerError(err.Error(), nil)
}

return result, resp.Header, nil
default:
return nil, nil, schema.NewConnectorError(http.StatusInternalServerError, "failed to evaluate response", map[string]any{
Expand All @@ -438,5 +443,6 @@ func parseContentType(input string) string {
return ""
}
parts := strings.Split(input, ";")

return strings.TrimSpace(parts[0])
}
1 change: 1 addition & 0 deletions connector/internal/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func DecodeDataURI(input string) (*DataURI, error) {
if err != nil {
return nil, err
}

return &DataURI{
Data: string(rawDecodedBytes),
}, nil
Expand Down
2 changes: 2 additions & 0 deletions connector/internal/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func (rms MetadataCollection) GetFunction(name string) (*rest.OperationInfo, con
return fn, rm, nil
}
}

return nil, configuration.NDCHttpRuntimeSchema{}, schema.UnprocessableContentError("unsupported query: "+name, nil)
}

Expand All @@ -28,5 +29,6 @@ func (rms MetadataCollection) GetProcedure(name string) (*rest.OperationInfo, co
return fn, rm, nil
}
}

return nil, configuration.NDCHttpRuntimeSchema{}, schema.UnprocessableContentError("unsupported mutation: "+name, nil)
}
3 changes: 3 additions & 0 deletions connector/internal/multipart.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (w *MultipartWriter) WriteJSON(fieldName string, value any, headers http.He
}

_, err = p.Write(bs)

return err
}

Expand All @@ -90,6 +91,7 @@ func (w *MultipartWriter) WriteField(fieldName, value string, headers http.Heade
return err
}
_, err = p.Write([]byte(value))

return err
}

Expand All @@ -100,5 +102,6 @@ func createFieldMIMEHeader(fieldName string, headers http.Header) textproto.MIME
}
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"`, escapeQuotes(fieldName)))

return h
}
10 changes: 10 additions & 0 deletions connector/internal/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ func (ssp ParameterItems) String() string {
str := item.String()
results = append(results, str)
}

return strings.Join(results, "&")
}

func (ssp *ParameterItems) Add(keys []Key, values []string) {
index := ssp.FindIndex(keys)
if index == -1 {
*ssp = append(*ssp, NewParameterItem(keys, values))

return
}
(*ssp)[index].AddValues(values)
Expand All @@ -41,16 +43,19 @@ func (ssp ParameterItems) FindDefault() *ParameterItem {
return item
}
item, _ = ssp.find([]Key{})

return item
}

func (ssp ParameterItems) Find(keys []Key) *ParameterItem {
item, _ := ssp.find(keys)

return item
}

func (ssp ParameterItems) FindIndex(keys []Key) int {
_, i := ssp.find(keys)

return i
}

Expand All @@ -73,6 +78,7 @@ func (ssp ParameterItems) find(keys []Key) (*ParameterItem, int) {
return &item, i
}
}

return nil, -1
}

Expand All @@ -89,6 +95,7 @@ func (ks Keys) String() string {
for i, k := range ks {
if k.index != nil {
sb.WriteString(fmt.Sprintf("[%d]", *k.index))

continue
}
if k.key != "" {
Expand All @@ -98,6 +105,7 @@ func (ks Keys) String() string {
sb.WriteString(k.key)
}
}

return sb.String()
}

Expand Down Expand Up @@ -137,6 +145,7 @@ func (k Key) String() string {
if k.index != nil {
return strconv.Itoa(*k.index)
}

return k.key
}

Expand All @@ -161,6 +170,7 @@ func (ssp ParameterItem) String() string {
if key == "" {
return value
}

return fmt.Sprintf("%s=%s", key, value)
}

Expand Down
4 changes: 4 additions & 0 deletions connector/internal/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ func getBaseURLFromServers(servers []rest.ServerConfig, serverIDs []string) (*ur
return nil, ""
case 1:
result := results[0]

return &result, selectedServerIDs[0]
default:
index := rand.IntN(len(results) - 1)
host := results[index]

return &host, selectedServerIDs[index]
}
}
Expand All @@ -99,6 +101,7 @@ func BuildDistributedRequestsWithOptions(request *RetryableRequest, httpOptions
if err := request.applySettings(httpOptions.Settings, httpOptions.Explain); err != nil {
return nil, err
}

return []RetryableRequest{*request}, nil
}

Expand Down Expand Up @@ -142,6 +145,7 @@ func BuildDistributedRequestsWithOptions(request *RetryableRequest, httpOptions
}
requests = append(requests, req)
}

return requests, nil
}

Expand Down
5 changes: 5 additions & 0 deletions connector/internal/request_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ func (c *RequestBuilder) evalMultipartForm(w *MultipartWriter, bodyInfo *rest.Ar
return err
}
}

return nil
case reflect.Struct:
reflectType := bodyData.Type()
Expand All @@ -272,6 +273,7 @@ func (c *RequestBuilder) evalMultipartForm(w *MultipartWriter, bodyInfo *rest.Ar
return err
}
}

return nil
}
}
Expand All @@ -296,8 +298,10 @@ func (c *RequestBuilder) evalMultipartFieldValueRecursive(w *MultipartWriter, na
return err
}
}

return w.WriteJSON(name, value.Interface(), headers)
}

if !slices.Contains([]reflect.Kind{reflect.Slice, reflect.Array}, value.Kind()) {
return fmt.Errorf("%s: expected array type, got %v", name, value.Kind())
}
Expand All @@ -314,6 +318,7 @@ func (c *RequestBuilder) evalMultipartFieldValueRecursive(w *MultipartWriter, na
return err
}
}

return nil
case *schema.NullableType:
if !notNull {
Expand Down
2 changes: 2 additions & 0 deletions connector/internal/request_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ func TestCreateFormURLEncoded(t *testing.T) {
parseQueryAndSort := func(input string) []string {
items := strings.Split(input, "&")
slices.Sort(items)

return items
}

Expand Down Expand Up @@ -593,5 +594,6 @@ func createMockSchema(t *testing.T) *rest.NDCHttpSchema {
rawSchemaBytes, err := os.ReadFile("../../ndc-http-schema/openapi/testdata/petstore3/expected.json")
assert.NilError(t, err)
assert.NilError(t, json.Unmarshal(rawSchemaBytes, &ndcSchema))

return &ndcSchema
}
Loading

0 comments on commit 2861dd4

Please sign in to comment.