Skip to content

Commit 0279842

Browse files
authored
Minor linting/formatting (#936)
1 parent f30901c commit 0279842

13 files changed

Lines changed: 101 additions & 94 deletions

File tree

api.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,19 @@ type Context interface {
126126
BodyWriter() io.Writer
127127
}
128128

129-
// Represent http protocol version
129+
// ProtoVersion represents the http protocol version.
130130
type ProtoVersion struct {
131131
Proto string
132132
ProtoMajor int
133133
ProtoMinor int
134134
}
135135

136-
type (
137-
humaContext Context
138-
subContext struct {
139-
humaContext
140-
override context.Context
141-
}
142-
)
136+
type humaContext Context
137+
138+
type subContext struct {
139+
humaContext
140+
override context.Context
141+
}
143142

144143
func (c subContext) Context() context.Context {
145144
return c.override
@@ -183,7 +182,7 @@ type Config struct {
183182
// `/openapi.yaml`, for example.
184183
OpenAPIPath string
185184

186-
// DocsPath is the path to the API documentation. If set to `/docs` it will
185+
// DocsPath is the path to the API documentation. If set to `/docs `, it will
187186
// allow clients to get `/docs` to view the documentation in a browser. If
188187
// you wish to provide your own documentation renderer, you can leave this
189188
// blank and attach it directly to the router or adapter.
@@ -295,15 +294,18 @@ func (a *api) Unmarshal(contentType string, data []byte, v any) error {
295294
if end == -1 {
296295
end = len(contentType)
297296
}
297+
298298
ct := contentType[start:end]
299299
if ct == "" {
300300
// Default to assume JSON since this is an API.
301301
ct = "application/json"
302302
}
303+
303304
f, ok := a.formats[ct]
304305
if !ok {
305306
return fmt.Errorf("%w: %s", ErrUnknownContentType, contentType)
306307
}
308+
307309
return f.Unmarshal(data, v)
308310
}
309311

@@ -325,12 +327,14 @@ func (a *api) Negotiate(accept string) (string, error) {
325327

326328
func (a *api) Transform(ctx Context, status string, v any) (any, error) {
327329
var err error
330+
328331
for _, t := range a.transformers {
329332
v, err = t(ctx, status, v)
330333
if err != nil {
331334
return nil, err
332335
}
333336
}
337+
334338
return v, nil
335339
}
336340

@@ -412,9 +416,11 @@ func NewAPI(config Config, a Adapter) API {
412416
config.DefaultFormat = "application/json"
413417
}
414418
}
419+
415420
if config.DefaultFormat != "" {
416421
newAPI.formatKeys = append(newAPI.formatKeys, config.DefaultFormat)
417422
}
423+
418424
for k, v := range config.Formats {
419425
newAPI.formats[k] = v
420426
newAPI.formatKeys = append(newAPI.formatKeys, k)
@@ -432,6 +438,7 @@ func NewAPI(config Config, a Adapter) API {
432438
}
433439
ctx.BodyWriter().Write(specJSON)
434440
})
441+
435442
var specJSON30 []byte
436443
a.Handle(&Operation{
437444
Method: http.MethodGet,
@@ -443,6 +450,7 @@ func NewAPI(config Config, a Adapter) API {
443450
}
444451
ctx.BodyWriter().Write(specJSON30)
445452
})
453+
446454
var specYAML []byte
447455
a.Handle(&Operation{
448456
Method: http.MethodGet,
@@ -454,6 +462,7 @@ func NewAPI(config Config, a Adapter) API {
454462
}
455463
ctx.BodyWriter().Write(specYAML)
456464
})
465+
457466
var specYAML30 []byte
458467
a.Handle(&Operation{
459468
Method: http.MethodGet,

autoconfig.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package huma
22

33
// AutoConfigVar represents a variable given by the user when prompted during
4-
// auto-configuration setup of an API.
4+
// autoconfiguration setup of an API.
55
type AutoConfigVar struct {
66
Description string `json:"description,omitempty"`
77
Example string `json:"example,omitempty"`

casing/casing.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ func Split(value string) []string {
206206
return results
207207
}
208208

209-
// Join will combine split parts back together with the given separator and
210-
// optional transform functions.
209+
// Join will combine split parts with the given separator and optional
210+
// transform functions.
211211
func Join(parts []string, sep string, transform ...TransformFunc) string {
212212
for i := 0; i < len(parts); i++ {
213213
for _, t := range transform {
@@ -257,7 +257,7 @@ func MergeNumbers(parts []string, suffixes ...string) []string {
257257

258258
if !prevNum {
259259
if i == 0 {
260-
// First item must always append.
260+
// The first item must always append.
261261
results = append(results, part)
262262
} else {
263263
// Concatenate the number to the previous non-number piece.
@@ -269,7 +269,7 @@ func MergeNumbers(parts []string, suffixes ...string) []string {
269269

270270
prevNum = true
271271
} else {
272-
// Special case: first part is a number, second part is not.
272+
// Special case: the first part is a number, the second part is not.
273273
if i == 1 && prevNum {
274274
results[0] += part
275275
prevNum = false

cookie.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// ReadCookie reads a single cookie from the request headers by name. If
1111
// multiple cookies with the same name exist, the first is returned.
1212
func ReadCookie(ctx Context, name string) (*http.Cookie, error) {
13-
headers := []string{}
13+
var headers []string
1414
ctx.EachHeader(func(name, value string) {
1515
if strings.EqualFold(name, "cookie") {
1616
headers = append(headers, value)
@@ -24,7 +24,7 @@ func ReadCookie(ctx Context, name string) (*http.Cookie, error) {
2424

2525
// ReadCookies reads all cookies from the request headers.
2626
func ReadCookies(ctx Context) []*http.Cookie {
27-
headers := []string{}
27+
var headers []string
2828
ctx.EachHeader(func(name, value string) {
2929
if strings.EqualFold(name, "cookie") {
3030
headers = append(headers, value)

docs/docs/features/test-utilities.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestMyAPI(t *testing.T) {
5959
}
6060
```
6161

62-
The request convenience methods take a URL path followed by any number of optional arguments. If the argument is a string, it is treated as a header, if it is an `io.Reader` is is treated as the raw body, otherwise it is marshalled as JSON and used as the request body.
62+
The request convenience methods take a URL path followed by any number of optional arguments. If the argument is a string, it is treated as a header, if it is an `io.Reader` is is treated as the raw body, otherwise it is marshaled as JSON and used as the request body.
6363

6464
## Assertions
6565

error.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,15 +259,16 @@ func WriteErr(api API, ctx Context, status int, msg string, errs ...error) error
259259
var err = NewErrorWithContext(ctx, status, msg, errs...)
260260

261261
// NewError may have modified the status code, so update it here if needed.
262-
// If it was not modified then this is a no-op.
262+
// If it was not modified, then this is a no-op.
263263
status = err.GetStatus()
264264

265-
writeErr := writeResponse(api, ctx, status, "", err)
266-
if writeErr != nil {
265+
writtenErr := writeResponse(api, ctx, status, "", err)
266+
if writtenErr != nil {
267267
// If we can't write the error, log it so we know what happened.
268-
fmt.Fprintf(os.Stderr, "could not write error: %v\n", writeErr)
268+
fmt.Fprintf(os.Stderr, "could not write error: %v\n", writtenErr)
269269
}
270-
return writeErr
270+
271+
return writtenErr
271272
}
272273

273274
// Status304NotModified returns a 304. This is not really an error, but

formdata.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (v MimeTypeValidator) Validate(fh *multipart.FileHeader, location string) (
5252
mimeType := fh.Header.Get("Content-Type")
5353
if mimeType == "" {
5454
var buffer = make([]byte, 1000)
55-
if _, err := file.Read(buffer); err != nil {
55+
if _, err = file.Read(buffer); err != nil {
5656
return "", &ErrorDetail{Message: "Failed to infer file media type", Location: location}
5757
}
5858
file.Seek(int64(0), io.SeekStart)
@@ -89,7 +89,7 @@ func (v MimeTypeValidator) Validate(fh *multipart.FileHeader, location string) (
8989
}
9090
}
9191

92-
// Decodes multipart.Form data into *T, returning []*ErrorDetail if any
92+
// Decode decodes multipart.Form data into *T, returning []*ErrorDetail if any
9393
// Schema is used to check for validation constraints
9494
func (m *MultipartFormFiles[T]) Decode(opMediaType *MediaType, formValueParser func(val reflect.Value)) []error {
9595
var (
@@ -131,9 +131,8 @@ func readSingleFile(fileHeaders []*multipart.FileHeader, key string, opMediaType
131131
if len(fileHeaders) == 0 {
132132
if opMediaType.Schema.requiredMap[key] {
133133
return FormFile{}, &ErrorDetail{Message: "File required", Location: key}
134-
} else {
135-
return FormFile{}, nil
136134
}
135+
return FormFile{}, nil
137136
} else if len(fileHeaders) == 1 {
138137
validator := NewMimeTypeValidator(opMediaType.Encoding[key])
139138
return readFile(fileHeaders[0], key, validator)
@@ -221,7 +220,7 @@ func multiPartFormFileSchema(r Registry, t reflect.Type) *Schema {
221220
default:
222221
schema.Properties[name] = SchemaFromField(r, f, name)
223222

224-
// Should we panic if [T] struct defines fields with unsupported types ?
223+
// Should we panic if [T] struct defines fields with unsupported types?
225224
}
226225

227226
if _, ok := f.Tag.Lookup("required"); ok && boolTag(f, "required", false) {

group.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (g *Group) DocumentOperation(op *Operation) {
107107
// on all operations in the group. Use this to modify the operation before it is
108108
// registered with the router or OpenAPI document. This behaves similar to
109109
// middleware in that you should invoke `next` to continue the chain. Skip it
110-
// to prevent the operation from being registered, and call multiple times for
110+
// to prevent the operation from being registered and call multiple times for
111111
// a fan-out effect.
112112
func (g *Group) UseModifier(modifier func(o *Operation, next func(*Operation))) {
113113
g.modifiers = append(g.modifiers, modifier)
@@ -166,7 +166,7 @@ func (g *Group) ModifyOperation(op *Operation, next func(*Operation)) {
166166

167167
// UseMiddleware adds one or more middleware functions to the group that will be
168168
// run on all operations in the group. Use this to add common functionality to
169-
// all operations in the group, e.g. authentication/authorization.
169+
// all operations in the group, e.g., authentication/authorization.
170170
func (g *Group) UseMiddleware(middlewares ...func(ctx Context, next func(Context))) {
171171
g.middlewares = append(g.middlewares, middlewares...)
172172
}

huma_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (UUID) Schema(r huma.Registry) *huma.Schema {
6363
return &huma.Schema{Type: huma.TypeString, Format: "uuid"}
6464
}
6565

66-
// BodyContainer is an embed request body struct to test request body unmarshalling
66+
// BodyContainer is an embed request body struct to test request body unmarshaling
6767
type BodyContainer struct {
6868
Body struct {
6969
Name string `json:"name"`

0 commit comments

Comments
 (0)