Skip to content

Commit

Permalink
refactor: resolves linter issues (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
Integralist committed Dec 8, 2023
1 parent cb1be5e commit dfe1fa0
Show file tree
Hide file tree
Showing 41 changed files with 279 additions and 208 deletions.
35 changes: 19 additions & 16 deletions fastly/account_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import (

// Event represents an event_logs item response from the Fastly API.
type Event struct {
Admin bool `jsonapi:"attr,admin"`
CreatedAt *time.Time `jsonapi:"attr,created_at,iso8601"`
CustomerID string `jsonapi:"attr,customer_id"`
Description string `jsonapi:"attr,description"`
EventType string `jsonapi:"attr,event_type"`
ID string `jsonapi:"primary,event"`
IP string `jsonapi:"attr,ip"`
Metadata map[string]interface{} `jsonapi:"attr,metadata,omitempty"`
ServiceID string `jsonapi:"attr,service_id"`
UserID string `jsonapi:"attr,user_id"`
Admin bool `jsonapi:"attr,admin"`
CreatedAt *time.Time `jsonapi:"attr,created_at,iso8601"`
CustomerID string `jsonapi:"attr,customer_id"`
Description string `jsonapi:"attr,description"`
EventType string `jsonapi:"attr,event_type"`
ID string `jsonapi:"primary,event"`
IP string `jsonapi:"attr,ip"`
Metadata map[string]any `jsonapi:"attr,metadata,omitempty"`
ServiceID string `jsonapi:"attr,service_id"`
UserID string `jsonapi:"attr,user_id"`
}

// GetAPIEventsFilterInput is used as input to the GetAPIEvents function.
Expand All @@ -44,20 +44,21 @@ type GetAPIEventsFilterInput struct {
}

// EventsPaginationInfo stores links to searches related to the current one, showing
// any information about additional results being stored on another page
// any information about additional results being stored on another page.
type EventsPaginationInfo struct {
First string `json:"first,omitempty"`
Last string `json:"last,omitempty"`
Next string `json:"next,omitempty"`
}

// GetAPIEventsResponse is the data returned to the user from a GetAPIEvents call
// GetAPIEventsResponse is the data returned to the user from a GetAPIEvents
// call.
type GetAPIEventsResponse struct {
Events []*Event
Links EventsPaginationInfo `json:"links"`
}

// GetAPIEvents lists all the events for a particular customer
// GetAPIEvents lists all the events for a particular customer.
func (c *Client) GetAPIEvents(i *GetAPIEventsFilterInput) (GetAPIEventsResponse, error) {
eventsResponse := GetAPIEventsResponse{
Events: []*Event{},
Expand Down Expand Up @@ -171,7 +172,7 @@ func getEventsPages(body io.Reader) (EventsPaginationInfo, io.Reader, error) {
// Fastly events.
func (i *GetAPIEventsFilterInput) formatEventFilters() map[string]string {
result := map[string]string{}
pairings := map[string]interface{}{
pairings := map[string]any{
"filter[customer_id]": i.CustomerID,
"filter[service_id]": i.ServiceID,
"filter[event_type]": i.EventType,
Expand All @@ -186,11 +187,13 @@ func (i *GetAPIEventsFilterInput) formatEventFilters() map[string]string {
switch t := reflect.TypeOf(value).String(); t {
case "string":
if value != "" {
result[key] = value.(string)
v, _ := value.(string) // type assert to avoid runtime panic (v will have zero value for its type)
result[key] = v
}
case "int":
if value != 0 {
result[key] = strconv.Itoa(value.(int))
v, _ := value.(int) // type assert to avoid runtime panic (v will have zero value for its type)
result[key] = strconv.Itoa(v)
}
}
}
Expand Down
57 changes: 29 additions & 28 deletions fastly/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import (

// APIKeyEnvVar is the name of the environment variable where the Fastly API
// key should be read from.
const APIKeyEnvVar = "FASTLY_API_KEY"
const APIKeyEnvVar = "FASTLY_API_KEY" // #nosec G101

// APIKeyHeader is the name of the header that contains the Fastly API key.
const APIKeyHeader = "Fastly-Key"
const APIKeyHeader = "Fastly-Key" // #nosec G101

// EndpointEnvVar is the name of an environment variable that can be used
// to change the URL of API requests.
Expand Down Expand Up @@ -211,17 +211,17 @@ func (c *Client) Patch(p string, ro *RequestOptions) (*http.Response, error) {
}

// PatchForm issues an HTTP PUT request with the given interface form-encoded.
func (c *Client) PatchForm(p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
func (c *Client) PatchForm(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestForm("PATCH", p, i, ro)
}

// PatchJSON issues an HTTP PUT request with the given interface json-encoded.
func (c *Client) PatchJSON(p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
func (c *Client) PatchJSON(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSON("PATCH", p, i, ro)
}

// PatchJSONAPI issues an HTTP PUT request with the given interface json-encoded.
func (c *Client) PatchJSONAPI(p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
func (c *Client) PatchJSONAPI(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSONAPI("PATCH", p, i, ro)
}

Expand All @@ -231,22 +231,22 @@ func (c *Client) Post(p string, ro *RequestOptions) (*http.Response, error) {
}

// PostForm issues an HTTP POST request with the given interface form-encoded.
func (c *Client) PostForm(p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
func (c *Client) PostForm(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestForm("POST", p, i, ro)
}

// PostJSON issues an HTTP POST request with the given interface json-encoded.
func (c *Client) PostJSON(p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
func (c *Client) PostJSON(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSON("POST", p, i, ro)
}

// PostJSONAPI issues an HTTP POST request with the given interface json-encoded.
func (c *Client) PostJSONAPI(p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
func (c *Client) PostJSONAPI(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSONAPI("POST", p, i, ro)
}

// PostJSONAPIBulk issues an HTTP POST request with the given interface json-encoded and bulk requests.
func (c *Client) PostJSONAPIBulk(p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
func (c *Client) PostJSONAPIBulk(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSONAPIBulk("POST", p, i, ro)
}

Expand All @@ -256,7 +256,7 @@ func (c *Client) Put(p string, ro *RequestOptions) (*http.Response, error) {
}

// PutForm issues an HTTP PUT request with the given interface form-encoded.
func (c *Client) PutForm(p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
func (c *Client) PutForm(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestForm("PUT", p, i, ro)
}

Expand All @@ -271,12 +271,12 @@ func (c *Client) PutFormFileFromReader(urlPath string, fileName string, fileByte
}

// PutJSON issues an HTTP PUT request with the given interface json-encoded.
func (c *Client) PutJSON(p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
func (c *Client) PutJSON(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSON("PUT", p, i, ro)
}

// PutJSONAPI issues an HTTP PUT request with the given interface json-encoded.
func (c *Client) PutJSONAPI(p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
func (c *Client) PutJSONAPI(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSONAPI("PUT", p, i, ro)
}

Expand All @@ -286,12 +286,12 @@ func (c *Client) Delete(p string, ro *RequestOptions) (*http.Response, error) {
}

// DeleteJSONAPI issues an HTTP DELETE request with the given interface json-encoded.
func (c *Client) DeleteJSONAPI(p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
func (c *Client) DeleteJSONAPI(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSONAPI("DELETE", p, i, ro)
}

// DeleteJSONAPIBulk issues an HTTP DELETE request with the given interface json-encoded and bulk requests.
func (c *Client) DeleteJSONAPIBulk(p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
func (c *Client) DeleteJSONAPIBulk(p string, i any, ro *RequestOptions) (*http.Response, error) {
return c.RequestJSONAPIBulk("DELETE", p, i, ro)
}

Expand Down Expand Up @@ -367,7 +367,7 @@ type RequestOptions struct {
}

// RawRequest accepts a verb, URL, and RequestOptions struct and returns the
// constructed http.Request and any errors that occurred
// constructed http.Request and any errors that occurred.
func (c *Client) RawRequest(verb, p string, ro *RequestOptions) (*http.Request, error) {
// Ensure we have request options.
if ro == nil {
Expand Down Expand Up @@ -465,7 +465,7 @@ func parseHealthCheckHeaders(s string) string {

// RequestForm makes an HTTP request with the given interface being encoded as
// form data.
func (c *Client) RequestForm(verb, p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
func (c *Client) RequestForm(verb, p string, i any, ro *RequestOptions) (*http.Response, error) {
if ro == nil {
ro = new(RequestOptions)
}
Expand Down Expand Up @@ -536,7 +536,7 @@ func (c *Client) RequestFormFileFromReader(verb, urlPath string, fileName string
}

// RequestJSON constructs JSON HTTP request.
func (c *Client) RequestJSON(verb, p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
func (c *Client) RequestJSON(verb, p string, i any, ro *RequestOptions) (*http.Response, error) {
if ro == nil {
ro = new(RequestOptions)
}
Expand All @@ -559,7 +559,7 @@ func (c *Client) RequestJSON(verb, p string, i interface{}, ro *RequestOptions)
}

// RequestJSONAPI constructs JSON API HTTP request.
func (c *Client) RequestJSONAPI(verb, p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
func (c *Client) RequestJSONAPI(verb, p string, i any, ro *RequestOptions) (*http.Response, error) {
if ro == nil {
ro = new(RequestOptions)
}
Expand All @@ -583,7 +583,7 @@ func (c *Client) RequestJSONAPI(verb, p string, i interface{}, ro *RequestOption
}

// RequestJSONAPIBulk constructs bulk JSON API HTTP request.
func (c *Client) RequestJSONAPIBulk(verb, p string, i interface{}, ro *RequestOptions) (*http.Response, error) {
func (c *Client) RequestJSONAPIBulk(verb, p string, i any, ro *RequestOptions) (*http.Response, error) {
if ro == nil {
ro = new(RequestOptions)
}
Expand Down Expand Up @@ -624,8 +624,8 @@ func checkResp(resp *http.Response, err error) (*http.Response, error) {
}

// decodeBodyMap is used to decode an HTTP response body into a mapstructure struct.
func decodeBodyMap(body io.Reader, out interface{}) error {
var parsed interface{}
func decodeBodyMap(body io.Reader, out any) error {
var parsed any
dec := json.NewDecoder(body)
if err := dec.Decode(&parsed); err != nil {
return err
Expand All @@ -637,7 +637,7 @@ func decodeBodyMap(body io.Reader, out interface{}) error {
// decodeMap decodes an `in` struct or map to a mapstructure tagged `out`.
// It applies the decoder defaults used throughout go-fastly.
// Note that this uses opposite argument order from Go's copy().
func decodeMap(in interface{}, out interface{}) error {
func decodeMap(in, out any) error {
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapToHTTPHeaderHookFunc(),
Expand All @@ -658,16 +658,16 @@ func mapToHTTPHeaderHookFunc() mapstructure.DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{},
) (interface{}, error) {
data any,
) (any, error) {
if f.Kind() != reflect.Map {
return data, nil
}
if t != reflect.TypeOf(new(http.Header)) {
return data, nil
}

typed, ok := data.(map[string]interface{})
typed, ok := data.(map[string]any)
if !ok {
return nil, fmt.Errorf("cannot convert %T to http.Header", data)
}
Expand Down Expand Up @@ -698,8 +698,8 @@ func stringToTimeHookFunc() mapstructure.DecodeHookFunc {
return func(
f reflect.Type,
t reflect.Type,
data interface{},
) (interface{}, error) {
data any,
) (any, error) {
if f.Kind() != reflect.String {
return data, nil
}
Expand All @@ -711,7 +711,8 @@ func stringToTimeHookFunc() mapstructure.DecodeHookFunc {
v, err := time.Parse(time.RFC3339, data.(string))
if err != nil {
// DictionaryInfo#get uses it's own special time format for now.
return time.Parse("2006-01-02 15:04:05", data.(string))
v, _ := data.(string) // type assert to avoid runtime panic (v will have zero value for its type)
return time.Parse("2006-01-02 15:04:05", v)
}
return v, err
}
Expand Down
1 change: 0 additions & 1 deletion fastly/config_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ func TestClient_ListConfigStores(t *testing.T) {
if got, want := css[0].ID, stores[0].ID; got != want {
t.Errorf("ListConfigStores: index %d: ID: got %q, want %q", 0, got, want)
}

}

func TestClient_ListConfigStoreServices(t *testing.T) {
Expand Down
20 changes: 10 additions & 10 deletions fastly/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ var ErrMissingServiceAuthorizationsUser = NewFieldError("User").Message("SAUser
var ErrMissingStoreID = NewFieldError("StoreID")

// ErrMissingUserID is an error that is returned when an input struct
// requires a "UserID" key, but one was not set
// requires a "UserID" key, but one was not set.
var ErrMissingUserID = NewFieldError("UserID")

// ErrMissingPermission is an error that is returned when an input struct
// requires a "Permission" key, but one was not set
// requires a "Permission" key, but one was not set.
var ErrMissingPermission = NewFieldError("Permission")

// ErrMissingServiceVersion is an error that is returned when an input struct
Expand All @@ -230,7 +230,7 @@ var ErrMissingTLSConfiguration = NewFieldError("TLSConfiguration")
var ErrMissingTLSDomain = NewFieldError("TLSDomain")

// ErrCommonNameNotInDomains is an error that is returned when an input struct
// requires that the domain in "CommonName" is also in "Domains"
// requires that the domain in "CommonName" is also in "Domains".
var ErrCommonNameNotInDomains = NewFieldError("CommonName").Message("CommonName must be in Domains")

// ErrMissingTo is an error that is returned when an input struct
Expand Down Expand Up @@ -286,7 +286,7 @@ var ErrMissingOptionalNameComment = NewFieldError("Name, Comment").Message("at l
var ErrMissingTokensValue = NewFieldError("Tokens").Message("expect at least one token")

// ErrStatusNotOk is an error that indicates the response body returned by the
// Fastly API was not `{"status": "ok"}`
// Fastly API was not `{"status": "ok"}`.
var ErrStatusNotOk = errors.New("unexpected 'status' field in API response body")

// ErrNotOK is a generic error indicating that something is not okay.
Expand Down Expand Up @@ -332,12 +332,12 @@ type HTTPError struct {

// ErrorObject is a single error.
type ErrorObject struct {
Code string `mapstructure:"code" json:"code,omitempty"`
Detail string `mapstructure:"detail" json:"detail,omitempty"`
ID string `mapstructure:"id" json:"id,omitempty"`
Meta *map[string]interface{} `mapstructure:"meta" json:"meta,omitempty"`
Status string `mapstructure:"status" json:"status,omitempty"`
Title string `mapstructure:"title" json:"title,omitempty"`
Code string `mapstructure:"code" json:"code,omitempty"`
Detail string `mapstructure:"detail" json:"detail,omitempty"`
ID string `mapstructure:"id" json:"id,omitempty"`
Meta *map[string]any `mapstructure:"meta" json:"meta,omitempty"`
Status string `mapstructure:"status" json:"status,omitempty"`
Title string `mapstructure:"title" json:"title,omitempty"`
}

// legacyError represents the older-style errors from Fastly.
Expand Down
13 changes: 5 additions & 8 deletions fastly/fastly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,11 @@ func createTestPool(t *testing.T, createFixture string, serviceID string, versio
return pool
}

func createTestLogging(t *testing.T, fixture, serviceID string, serviceNumber int) *Syslog {
func createTestLogging(t *testing.T, fixture, serviceID string, serviceNumber int) {
var err error
var log *Syslog

record(t, fixture, func(c *Client) {
log, err = c.CreateSyslog(&CreateSyslogInput{
_, err = c.CreateSyslog(&CreateSyslogInput{
ServiceID: serviceID,
ServiceVersion: serviceNumber,
Name: ToPointer("test-syslog"),
Expand All @@ -290,8 +289,6 @@ func createTestLogging(t *testing.T, fixture, serviceID string, serviceNumber in
if err != nil {
t.Fatal(err)
}

return log
}

func deleteTestPool(t *testing.T, pool *Pool, deleteFixture string) {
Expand Down Expand Up @@ -410,13 +407,13 @@ func createWAF(t *testing.T, fixture, serviceID, condition, response string, ser
return waf
}

func deleteWAF(t *testing.T, fixture, wafID string, wafVersion int) {
func deleteWAF(t *testing.T, fixture, wafID string) {
var err error

record(t, fixture, func(c *Client) {
err = c.DeleteWAF(&DeleteWAFInput{
ID: wafID,
ServiceVersion: wafVersion,
ServiceVersion: 1,
})
})
if err != nil {
Expand Down Expand Up @@ -625,7 +622,7 @@ Zfq6/HA3phy85qyj
`
}

// caCert returns a CA certificate suitable for testing
// caCert returns a CA certificate suitable for testing.
func caCert() string {
return `-----BEGIN CERTIFICATE-----
MIICUTCCAfugAwIBAgIBADANBgkqhkiG9w0BAQQFADBXMQswCQYDVQQGEwJDTjEL
Expand Down
1 change: 1 addition & 0 deletions fastly/health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (c *Client) ListHealthChecks(i *ListHealthChecksInput) ([]*HealthCheck, err
if err != nil {
return nil, err
}
defer resp.Body.Close()

var hcs []*HealthCheck
if err := decodeBodyMap(resp.Body, &hcs); err != nil {
Expand Down
Loading

0 comments on commit dfe1fa0

Please sign in to comment.