Skip to content

Commit 7ac4c43

Browse files
stainless-app[bot]yjp20
authored andcommitted
chore: code cleanup for interface{}
1 parent bb96e42 commit 7ac4c43

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

pkg/jsonflag/json_flag.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type JSONConfig struct {
1212
Kind MutationKind
1313
Path string
1414
// For boolean flags that set a specific value when present
15-
SetValue interface{}
15+
SetValue any
1616
}
1717

1818
type JSONValueCreator[T any] struct{}
@@ -63,11 +63,9 @@ func (v *jsonValue[T]) Set(val string) error {
6363
return nil
6464
}
6565
// For any flags with SetValue, register the configured value
66-
if _, isAny := any(parsed).(interface{}); isAny {
67-
globalRegistry.Mutate(v.config.Kind, v.config.Path, v.config.SetValue)
68-
*v.destination = any(v.config.SetValue).(T)
69-
return nil
70-
}
66+
globalRegistry.Mutate(v.config.Kind, v.config.Path, v.config.SetValue)
67+
*v.destination = any(v.config.SetValue).(T)
68+
return nil
7169
}
7270

7371
switch any(parsed).(type) {
@@ -114,8 +112,8 @@ func (v *jsonValue[T]) Set(val string) error {
114112
return fmt.Errorf("invalid datetime value %q: %w", val, parseErr)
115113
}
116114
parsed = any(timeVal).(T)
117-
case interface{}:
118-
// For interface{}, store the string value directly
115+
case any:
116+
// For `any`, store the string value directly
119117
parsed = any(val).(T)
120118
default:
121119
return fmt.Errorf("unsupported type for JSON flag")
@@ -247,4 +245,4 @@ type JSONIntFlag = cli.FlagBase[int, JSONConfig, JSONValueCreator[int]]
247245
type JSONFloatFlag = cli.FlagBase[float64, JSONConfig, JSONValueCreator[float64]]
248246
type JSONDatetimeFlag = cli.FlagBase[time.Time, JSONConfig, JSONValueCreator[time.Time]]
249247
type JSONDateFlag = cli.FlagBase[time.Time, JSONConfig, JSONDateValueCreator]
250-
type JSONAnyFlag = cli.FlagBase[interface{}, JSONConfig, JSONValueCreator[interface{}]]
248+
type JSONAnyFlag = cli.FlagBase[any, JSONConfig, JSONValueCreator[any]]

pkg/jsonflag/mutation.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020
type Mutation struct {
2121
Kind MutationKind
2222
Path string
23-
Value interface{}
23+
Value any
2424
}
2525

2626
type registry struct {
@@ -29,7 +29,7 @@ type registry struct {
2929

3030
var globalRegistry = &registry{}
3131

32-
func (r *registry) Mutate(kind MutationKind, path string, value interface{}) {
32+
func (r *registry) Mutate(kind MutationKind, path string, value any) {
3333
r.mutations = append(r.mutations, Mutation{
3434
Kind: kind,
3535
Path: path,
@@ -68,7 +68,7 @@ func (r *registry) List() []Mutation {
6868
}
6969

7070
// Mutate adds a mutation that will be applied to the specified kind of data
71-
func Mutate(kind MutationKind, path string, value interface{}) {
71+
func Mutate(kind MutationKind, path string, value any) {
7272
globalRegistry.Mutate(kind, path, value)
7373
}
7474

@@ -87,11 +87,10 @@ func ListMutations() []Mutation {
8787
return globalRegistry.List()
8888
}
8989

90-
func jsonSet(json []byte, path string, value interface{}) ([]byte, error) {
90+
func jsonSet(json []byte, path string, value any) ([]byte, error) {
9191
keys := strings.Split(path, ".")
9292
path = ""
93-
for i := 0; i < len(keys); i++ {
94-
key := keys[i]
93+
for _, key := range keys {
9594
if key == "#" {
9695
key = strconv.Itoa(len(gjson.GetBytes(json, path).Array()) - 1)
9796
}

0 commit comments

Comments
 (0)