Skip to content

Commit 3643c9b

Browse files
committed
chore: storybook check int conversion bounds to remove lint warning
1 parent b15d514 commit 3643c9b

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

storybook/storybook.go

+27-23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"math"
89
"net/http"
910
"net/url"
1011
"os"
@@ -98,7 +99,7 @@ func New(conf ...StorybookConfig) *Storybook {
9899
return sh
99100
}
100101

101-
func (sh *Storybook) AddComponent(name string, componentConstructor interface{}, args ...Arg) *Conf {
102+
func (sh *Storybook) AddComponent(name string, componentConstructor any, args ...Arg) *Conf {
102103
//TODO: Check that the component constructor is a function that returns a templ.Component.
103104
c := NewConf(name, args...)
104105
sh.Config[name] = c
@@ -313,9 +314,9 @@ func (sh *Storybook) buildStorybook() (err error) {
313314
return cmd.Run()
314315
}
315316

316-
func NewHandler(name string, f interface{}, args ...Arg) http.Handler {
317+
func NewHandler(name string, f any, args ...Arg) http.Handler {
317318
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
318-
argv := make([]interface{}, len(args))
319+
argv := make([]any, len(args))
319320
q := r.URL.Query()
320321
for i, arg := range args {
321322
argv[i] = arg.Get(q)
@@ -329,7 +330,7 @@ func NewHandler(name string, f interface{}, args ...Arg) http.Handler {
329330
})
330331
}
331332

332-
func executeTemplate(name string, fn interface{}, values []interface{}) (output templ.Component, err error) {
333+
func executeTemplate(name string, fn any, values []any) (output templ.Component, err error) {
333334
v := reflect.ValueOf(fn)
334335
t := v.Type()
335336
argv := make([]reflect.Value, t.NumIn())
@@ -357,7 +358,7 @@ func NewConf(title string, args ...Arg) *Conf {
357358
c := &Conf{
358359
Title: title,
359360
Parameters: StoryParameters{
360-
Server: map[string]interface{}{
361+
Server: map[string]any{
361362
"id": title,
362363
},
363364
},
@@ -367,7 +368,7 @@ func NewConf(title string, args ...Arg) *Conf {
367368
}
368369
for _, arg := range args {
369370
c.Args.Add(arg.Name, arg.Value)
370-
c.ArgTypes.Add(arg.Name, map[string]interface{}{
371+
c.ArgTypes.Add(arg.Name, map[string]any{
371372
"control": arg.Control,
372373
})
373374
}
@@ -390,17 +391,17 @@ func (c *Conf) AddStory(name string, args ...Arg) {
390391
// See https://storybook.js.org/docs/react/essentials/controls
391392
type Arg struct {
392393
Name string
393-
Value interface{}
394-
Control interface{}
395-
Get func(q url.Values) interface{}
394+
Value any
395+
Control any
396+
Get func(q url.Values) any
396397
}
397398

398-
func ObjectArg(name string, value interface{}, valuePtr interface{}) Arg {
399+
func ObjectArg(name string, value any, valuePtr any) Arg {
399400
return Arg{
400401
Name: name,
401402
Value: value,
402403
Control: "object",
403-
Get: func(q url.Values) interface{} {
404+
Get: func(q url.Values) any {
404405
err := json.Unmarshal([]byte(q.Get(name)), valuePtr)
405406
if err != nil {
406407
return err
@@ -415,7 +416,7 @@ func TextArg(name, value string) Arg {
415416
Name: name,
416417
Value: value,
417418
Control: "text",
418-
Get: func(q url.Values) interface{} {
419+
Get: func(q url.Values) any {
419420
return q.Get(name)
420421
},
421422
}
@@ -426,7 +427,7 @@ func BooleanArg(name string, value bool) Arg {
426427
Name: name,
427428
Value: value,
428429
Control: "boolean",
429-
Get: func(q url.Values) interface{} {
430+
Get: func(q url.Values) any {
430431
return q.Get(name) == "true"
431432
},
432433
}
@@ -435,7 +436,7 @@ func BooleanArg(name string, value bool) Arg {
435436
type IntArgConf struct{ Min, Max, Step *int }
436437

437438
func IntArg(name string, value int, conf IntArgConf) Arg {
438-
control := map[string]interface{}{
439+
control := map[string]any{
439440
"type": "number",
440441
}
441442
if conf.Min != nil {
@@ -451,9 +452,12 @@ func IntArg(name string, value int, conf IntArgConf) Arg {
451452
Name: name,
452453
Value: value,
453454
Control: control,
454-
Get: func(q url.Values) interface{} {
455-
i, _ := strconv.ParseInt(q.Get(name), 10, 64)
456-
return int(i)
455+
Get: func(q url.Values) any {
456+
i64, err := strconv.ParseInt(q.Get(name), 10, 64)
457+
if err != nil || i64 < math.MinInt || i64 > math.MaxInt {
458+
return 0
459+
}
460+
return int(i64)
457461
},
458462
}
459463
return arg
@@ -463,13 +467,13 @@ func FloatArg(name string, value float64, min, max, step float64) Arg {
463467
return Arg{
464468
Name: name,
465469
Value: value,
466-
Control: map[string]interface{}{
470+
Control: map[string]any{
467471
"type": "number",
468472
"min": min,
469473
"max": max,
470474
"step": step,
471475
},
472-
Get: func(q url.Values) interface{} {
476+
Get: func(q url.Values) any {
473477
i, _ := strconv.ParseFloat(q.Get(name), 64)
474478
return i
475479
},
@@ -485,24 +489,24 @@ type Conf struct {
485489
}
486490

487491
type StoryParameters struct {
488-
Server map[string]interface{} `json:"server"`
492+
Server map[string]any `json:"server"`
489493
}
490494

491495
func NewSortedMap() *SortedMap {
492496
return &SortedMap{
493497
m: new(sync.Mutex),
494-
internal: map[string]interface{}{},
498+
internal: map[string]any{},
495499
keys: []string{},
496500
}
497501
}
498502

499503
type SortedMap struct {
500504
m *sync.Mutex
501-
internal map[string]interface{}
505+
internal map[string]any
502506
keys []string
503507
}
504508

505-
func (sm *SortedMap) Add(key string, value interface{}) {
509+
func (sm *SortedMap) Add(key string, value any) {
506510
sm.m.Lock()
507511
defer sm.m.Unlock()
508512
sm.keys = append(sm.keys, key)

0 commit comments

Comments
 (0)