diff --git a/benchmark_test.go b/benchmark_test.go index 86e9fef7..056268eb 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -90,7 +90,7 @@ func BenchmarkGetterHas_String(b *testing.B) { } func BenchmarkGetterGet_String(b *testing.B) { - var it interface{} + var it any g, err := newTestGetter() // See: getter_test.go if err != nil { @@ -170,7 +170,7 @@ func BenchmarkGetterUnsafePointer(b *testing.B) { } func BenchmarkGetterSlice_StructPtrSlice(b *testing.B) { - var sl []interface{} + var sl []any g, err := newTestGetter() // See: getter_test.go if err != nil { @@ -202,7 +202,7 @@ func BenchmarkGetterGetGetter(b *testing.B) { } func BenchmarkGetterToMap(b *testing.B) { - var m map[string]interface{} + var m map[string]any g, err := newTestGetter() // See: getter_test.go if err != nil { @@ -282,14 +282,14 @@ func BenchmarkGetterIsSlice_StructPtrSlice(b *testing.B) { } func BenchmarkGetterMapGet(b *testing.B) { - var ia []interface{} + var ia []any g, err := newTestGetter() // See: getter_test.go if err != nil { b.Fatalf("NewGetter() occurs unexpected error: %v", err) return } - fn := func(i int, g *Getter) (interface{}, error) { + fn := func(i int, g *Getter) (any, error) { str, _ := g.String("String") str2, _ := g.String("String") return fmt.Sprintf("%s:%s", str, str2), nil @@ -339,7 +339,7 @@ func BenchmarkNewFinder_Ptr(b *testing.B) { } func BenchmarkToMap_1FindOnly(b *testing.B) { - var m map[string]interface{} + var m map[string]any f, err := NewFinder(newFinderTestStructPtr()) // See: finder_test.go if err != nil { @@ -359,7 +359,7 @@ func BenchmarkToMap_1FindOnly(b *testing.B) { } func BenchmarkToMap_2FindOnly(b *testing.B) { - var m map[string]interface{} + var m map[string]any f, err := NewFinder(newFinderTestStructPtr()) // See: finder_test.go if err != nil { @@ -379,7 +379,7 @@ func BenchmarkToMap_2FindOnly(b *testing.B) { } func BenchmarkToMap_1Struct_1Find(b *testing.B) { - var m map[string]interface{} + var m map[string]any f, err := NewFinder(newFinderTestStructPtr()) // See: finder_test.go if err != nil { @@ -399,7 +399,7 @@ func BenchmarkToMap_1Struct_1Find(b *testing.B) { } func BenchmarkToMap_1Struct_1Find_2Pair(b *testing.B) { - var m map[string]interface{} + var m map[string]any f, err := NewFinder(newFinderTestStructPtr()) // See: finder_test.go if err != nil { @@ -419,7 +419,7 @@ func BenchmarkToMap_1Struct_1Find_2Pair(b *testing.B) { } func BenchmarkToMap_2Struct_1Find(b *testing.B) { - var m map[string]interface{} + var m map[string]any f, err := NewFinder(newFinderTestStructPtr()) // See: finder_test.go if err != nil { @@ -439,7 +439,7 @@ func BenchmarkToMap_2Struct_1Find(b *testing.B) { } func BenchmarkToMap_2Struct_2Find(b *testing.B) { - var m map[string]interface{} + var m map[string]any f, err := NewFinder(newFinderTestStructPtr()) // See: finder_test.go if err != nil { diff --git a/cmd/strucmd/main.go b/cmd/strucmd/main.go index aa46a6d5..beca9ac2 100644 --- a/cmd/strucmd/main.go +++ b/cmd/strucmd/main.go @@ -2,8 +2,37 @@ package main import ( "fmt" + "io" + "os" + + "github.com/goldeneggg/structil/dynamicstruct/decoder" ) func main() { - fmt.Println("NOT IMPLEMENTED YET") + // fmt.Println("NOT IMPLEMENTED YET") + + f, err := os.Open("demo.json") + if err != nil { + panic(err) + } + + b, err := io.ReadAll(f) + if err != nil { + panic(err) + } + + dec, err := decoder.FromJSON(b) + if err != nil { + panic(err) + } + + nest := true + useTag := true + ds, err := dec.DynamicStruct(nest, useTag) + if err != nil { + panic(err) + } + + // Print struct definition from DynamicStruct + fmt.Println(ds.Definition()) } diff --git a/dynamicstruct/benchmark_test.go b/dynamicstruct/benchmark_test.go index f7c2b827..001729fa 100644 --- a/dynamicstruct/benchmark_test.go +++ b/dynamicstruct/benchmark_test.go @@ -59,7 +59,7 @@ func BenchmarkAddMap(b *testing.B) { func BenchmarkAddFunc(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - _ = NewBuilder().AddFunc("FuncField", []interface{}{SampleInt, SampleInt}, []interface{}{SampleBool, ErrSample}) + _ = NewBuilder().AddFunc("FuncField", []any{SampleInt, SampleInt}, []any{SampleBool, ErrSample}) } } diff --git a/dynamicstruct/builder.go b/dynamicstruct/builder.go index 2a849352..5de7f998 100644 --- a/dynamicstruct/builder.go +++ b/dynamicstruct/builder.go @@ -199,7 +199,7 @@ func (b *Builder) AddBoolWithTag(name string, tag string) *Builder { // AddMap returns a Builder that was added a map field named by name parameter. // Type of map key is type of ki. // Type of map value is type of vi. -func (b *Builder) AddMap(name string, ki interface{}, vi interface{}) *Builder { +func (b *Builder) AddMap(name string, ki any, vi any) *Builder { b.AddMapWithTag(name, ki, vi, "") return b } @@ -207,11 +207,11 @@ func (b *Builder) AddMap(name string, ki interface{}, vi interface{}) *Builder { // AddMapWithTag returns a Builder that was added a map field with tag named by name parameter. // Type of map key is type of ki. // Type of map value is type of vi. -func (b *Builder) AddMapWithTag(name string, ki interface{}, vi interface{}, tag string) *Builder { +func (b *Builder) AddMapWithTag(name string, ki any, vi any, tag string) *Builder { f := func() reflect.Type { var vt reflect.Type if vi == nil { - vt = reflect.TypeOf((*interface{})(nil)).Elem() + vt = reflect.TypeOf((*any)(nil)).Elem() } else { vt = reflect.TypeOf(vi) } @@ -225,7 +225,7 @@ func (b *Builder) AddMapWithTag(name string, ki interface{}, vi interface{}, tag // AddFunc returns a Builder that was added a func field named by name parameter. // Types of func args are types of in. // Types of func returns are types of out. -func (b *Builder) AddFunc(name string, in []interface{}, out []interface{}) *Builder { +func (b *Builder) AddFunc(name string, in []any, out []any) *Builder { b.AddFuncWithTag(name, in, out, "") return b } @@ -233,7 +233,7 @@ func (b *Builder) AddFunc(name string, in []interface{}, out []interface{}) *Bui // AddFuncWithTag returns a Builder that was added a func field with tag named by name parameter. // Types of func args are types of in. // Types of func returns are types of out. -func (b *Builder) AddFuncWithTag(name string, in []interface{}, out []interface{}, tag string) *Builder { +func (b *Builder) AddFuncWithTag(name string, in []any, out []any, tag string) *Builder { f := func() reflect.Type { it := make([]reflect.Type, len(in)) for i := 0; i < len(in); i++ { @@ -252,14 +252,14 @@ func (b *Builder) AddFuncWithTag(name string, in []interface{}, out []interface{ // AddChanBoth returns a Builder that was added a BothDir chan field named by name parameter. // Type of chan is type of i. -func (b *Builder) AddChanBoth(name string, i interface{}) *Builder { +func (b *Builder) AddChanBoth(name string, i any) *Builder { b.AddChanBothWithTag(name, i, "") return b } // AddChanBothWithTag returns a Builder that was added a BothDir chan field with tag named by name parameter. // Type of chan is type of i. -func (b *Builder) AddChanBothWithTag(name string, i interface{}, tag string) *Builder { +func (b *Builder) AddChanBothWithTag(name string, i any, tag string) *Builder { f := func() reflect.Type { return reflect.ChanOf(reflect.BothDir, reflect.TypeOf(i)) } @@ -270,14 +270,14 @@ func (b *Builder) AddChanBothWithTag(name string, i interface{}, tag string) *Bu // AddChanRecv returns a Builder that was added a RecvDir chan field named by name parameter. // Type of chan is type of i. -func (b *Builder) AddChanRecv(name string, i interface{}) *Builder { +func (b *Builder) AddChanRecv(name string, i any) *Builder { b.AddChanRecvWithTag(name, i, "") return b } // AddChanRecvWithTag returns a Builder that was added a RecvDir chan field with tag named by name parameter. // Type of chan is type of i. -func (b *Builder) AddChanRecvWithTag(name string, i interface{}, tag string) *Builder { +func (b *Builder) AddChanRecvWithTag(name string, i any, tag string) *Builder { f := func() reflect.Type { return reflect.ChanOf(reflect.RecvDir, reflect.TypeOf(i)) } @@ -288,14 +288,14 @@ func (b *Builder) AddChanRecvWithTag(name string, i interface{}, tag string) *Bu // AddChanSend returns a Builder that was added a SendDir chan field named by name parameter. // Type of chan is type of i. -func (b *Builder) AddChanSend(name string, i interface{}) *Builder { +func (b *Builder) AddChanSend(name string, i any) *Builder { b.AddChanSendWithTag(name, i, "") return b } // AddChanSendWithTag returns a Builder that was added a SendDir chan field with tag named by name parameter. // Type of chan is type of i. -func (b *Builder) AddChanSendWithTag(name string, i interface{}, tag string) *Builder { +func (b *Builder) AddChanSendWithTag(name string, i any, tag string) *Builder { f := func() reflect.Type { return reflect.ChanOf(reflect.SendDir, reflect.TypeOf(i)) } @@ -306,14 +306,14 @@ func (b *Builder) AddChanSendWithTag(name string, i interface{}, tag string) *Bu // AddStruct returns a Builder that was added a struct field named by name parameter. // Type of struct is type of i. -func (b *Builder) AddStruct(name string, i interface{}, isPtr bool) *Builder { +func (b *Builder) AddStruct(name string, i any, isPtr bool) *Builder { b.AddStructWithTag(name, i, isPtr, "") return b } // AddStructWithTag returns a Builder that was added a struct field with tag named by name parameter. // Type of struct is type of i. -func (b *Builder) AddStructWithTag(name string, i interface{}, isPtr bool, tag string) *Builder { +func (b *Builder) AddStructWithTag(name string, i any, isPtr bool, tag string) *Builder { f := func() reflect.Type { iType := reflect.TypeOf(i) if iType.Kind() == reflect.Ptr { @@ -332,26 +332,26 @@ func (b *Builder) AddStructWithTag(name string, i interface{}, isPtr bool, tag s // AddStructPtr returns a Builder that was added a struct pointer field named by name parameter. // Type of struct is type of i. -func (b *Builder) AddStructPtr(name string, i interface{}) *Builder { +func (b *Builder) AddStructPtr(name string, i any) *Builder { return b.AddStruct(name, i, true) } // AddStructPtrWithTag returns a Builder that was added a struct pointer field with tag named by name parameter. // Type of struct is type of i. -func (b *Builder) AddStructPtrWithTag(name string, i interface{}, tag string) *Builder { +func (b *Builder) AddStructPtrWithTag(name string, i any, tag string) *Builder { return b.AddStructWithTag(name, i, true, tag) } // AddSlice returns a Builder that was added a slice field named by name parameter. // Type of slice is type of i. -func (b *Builder) AddSlice(name string, i interface{}) *Builder { +func (b *Builder) AddSlice(name string, i any) *Builder { b.AddSliceWithTag(name, i, "") return b } // AddSliceWithTag returns a Builder that was added a slice field with tag named by name parameter. // Type of slice is type of i. -func (b *Builder) AddSliceWithTag(name string, i interface{}, tag string) *Builder { +func (b *Builder) AddSliceWithTag(name string, i any, tag string) *Builder { f := func() reflect.Type { return reflect.SliceOf(reflect.TypeOf(i)) } @@ -360,16 +360,16 @@ func (b *Builder) AddSliceWithTag(name string, i interface{}, tag string) *Build return b } -// AddInterface returns a Builder that was added a interface{} field named by name parameter. +// AddInterface returns a Builder that was added a any field named by name parameter. func (b *Builder) AddInterface(name string, isPtr bool) *Builder { b.AddInterfaceWithTag(name, isPtr, "") return b } -// AddInterfaceWithTag returns a Builder that was added a interface{} field with tag named by name parameter. +// AddInterfaceWithTag returns a Builder that was added a any field with tag named by name parameter. func (b *Builder) AddInterfaceWithTag(name string, isPtr bool, tag string) *Builder { f := func() reflect.Type { - return reflect.TypeOf((*interface{})(nil)).Elem() + return reflect.TypeOf((*any)(nil)).Elem() } b.addFieldFunc(name, isPtr, tag, f) diff --git a/dynamicstruct/decoder/data_type.go b/dynamicstruct/decoder/data_type.go index 6f6ccb3b..6a24e26b 100644 --- a/dynamicstruct/decoder/data_type.go +++ b/dynamicstruct/decoder/data_type.go @@ -44,21 +44,21 @@ func (dt dataType) string() string { return "" } -func (dt dataType) unmarshal(data []byte) (interface{}, error) { - var intf interface{} +func (dt dataType) unmarshal(data []byte) (any, error) { + var intf any err := dt.unmarshalWithIPtr(data, &intf) return intf, err } -func (dt dataType) unmarshalWithIPtr(data []byte, iptr interface{}) error { +func (dt dataType) unmarshalWithIPtr(data []byte, iptr any) error { var err error switch dt { case typeJSON: - // Note: iptr should be "map[string]interface{}" + // Note: iptr should be "map[string]any" err = json.Unmarshal(data, iptr) case typeYAML: - // Note: iptr should be "map[interface{}]interface{}" using gopkg.in/yaml.v2 package + // Note: iptr should be "map[any]any" using gopkg.in/yaml.v2 package err = yaml.Unmarshal(data, iptr) default: err = fmt.Errorf("invalid datatype for Unmarshal: %v", dt) @@ -68,14 +68,14 @@ func (dt dataType) unmarshalWithIPtr(data []byte, iptr interface{}) error { } // TODO: add tests and examples -// func (dt dataType) marshal(v interface{}) (data []byte, err error) { -func (dt dataType) marshal(m map[string]interface{}) (data []byte, err error) { +// func (dt dataType) marshal(v any) (data []byte, err error) { +func (dt dataType) marshal(m map[string]any) (data []byte, err error) { switch dt { case typeJSON: - // Note: v is expected to be "map[string]interface{}" + // Note: v is expected to be "map[string]any" data, err = json.Marshal(m) case typeYAML: - // Note: v is expected to be converted from "map[interface{}]interface{}" to "map[string]interface{}" + // Note: v is expected to be converted from "map[any]any" to "map[string]any" data, err = yaml.Marshal(m) default: err = fmt.Errorf("invalid datatype for Marshal: %v", dt) diff --git a/dynamicstruct/decoder/decoder.go b/dynamicstruct/decoder/decoder.go index 02b5b66b..6638d1df 100644 --- a/dynamicstruct/decoder/decoder.go +++ b/dynamicstruct/decoder/decoder.go @@ -12,11 +12,11 @@ import ( // Decoder is the struct that decodes some marshaled data like JSON and YAML. type Decoder struct { dt dataType - orgData []byte // original data - orgIntf interface{} // unmarshaled interface from original data - strKeyMap map[string]interface{} // string key map for decoding to DymanicStruct + orgData []byte // original data + orgIntf any // unmarshaled interface from original data + strKeyMap map[string]any // string key map for decoding to DymanicStruct ds *dynamicstruct.DynamicStruct - dsi interface{} // unmarshaled result from data to DynamicStruct + dsi any // unmarshaled result from data to DynamicStruct } func newDecoder(data []byte, dt dataType) (*Decoder, error) { @@ -29,26 +29,26 @@ func newDecoder(data []byte, dt dataType) (*Decoder, error) { dt: dt, orgData: data, orgIntf: unm, - strKeyMap: make(map[string]interface{}), + strKeyMap: make(map[string]any), } switch t := dec.orgIntf.(type) { - case map[string]interface{}: + case map[string]any: // JSON dec.strKeyMap = t // Note: this is dead case with gopkg.in/yaml.v3 (but alive with v2) - // case map[interface{}]interface{}: + // case map[any]any: // // YAML // dec.strKeyMap = toStringKeyMap(t) - case []interface{}: + case []any: if len(t) > 0 { // The items in the array must be same for all elements. // So the first element is used to process switch tt := t[0].(type) { - case map[string]interface{}: + case map[string]any: dec.strKeyMap = tt // Note: this is dead case with gopkg.in/yaml.v3 (but alive with v2) - // case map[interface{}]interface{}: + // case map[any]any: // dec.strKeyMap = toStringKeyMap(tt) default: return nil, fmt.Errorf("unexpected type of t[0] [%v]", tt) @@ -111,19 +111,19 @@ func (d *Decoder) dsToGetter(nest bool) (*structil.Getter, error) { return structil.NewGetter(dsi) } -func (d *Decoder) decodeToDynamicStruct(ds *dynamicstruct.DynamicStruct) (interface{}, error) { +func (d *Decoder) decodeToDynamicStruct(ds *dynamicstruct.DynamicStruct) (any, error) { // toDsFromStringMap() method uses "Build()" method and this means that ds is build by pointer-mode // So ds.NewInterface() returns a struct *pointer* d.dsi = ds.NewInterface() - // must use "d.strKeyMap" (not "d.orgIntf"). because key of "d.orgIntf" is not string but interface{} + // must use "d.strKeyMap" (not "d.orgIntf"). because key of "d.orgIntf" is not string but any data, err := d.dt.marshal(d.strKeyMap) if err != nil { return nil, fmt.Errorf("fail to d.dt.marshal: %w", err) } // must use "d.dsi" (not "&d.dsi"). because "d.dsi" is pointer - // if use "&.d.dsi", unmarshal result is not struct but map[interface{}]interface when dt is YAML + // if use "&.d.dsi", unmarshal result is not struct but map[any]interface when dt is YAML if err := d.dt.unmarshalWithIPtr(data, d.dsi); err != nil { return nil, fmt.Errorf("fail to d.dt.unmarshalWithIPtr: %w", err) } @@ -149,16 +149,16 @@ func (d *Decoder) DynamicStruct(nest bool, useTag bool) (*dynamicstruct.DynamicS return d.ds, err } -func (d *Decoder) toDs(i interface{}, nest bool, useTag bool) (*dynamicstruct.DynamicStruct, error) { +func (d *Decoder) toDs(i any, nest bool, useTag bool) (*dynamicstruct.DynamicStruct, error) { switch t := i.(type) { - case map[string]interface{}: + case map[string]any: return d.toDsFromStringMap(t, nest, useTag) } return nil, fmt.Errorf("unsupported type [%T] for toDs", i) } -func (d *Decoder) toDsFromStringMap(m map[string]interface{}, nest bool, useTag bool) (*dynamicstruct.DynamicStruct, error) { +func (d *Decoder) toDsFromStringMap(m map[string]any, nest bool, useTag bool) (*dynamicstruct.DynamicStruct, error) { var tag, name string var err error b := dynamicstruct.NewBuilder() @@ -187,10 +187,10 @@ func (d *Decoder) toDsFromStringMap(m map[string]interface{}, nest bool, useTag b = b.AddFloat64WithTag(name, tag) case string: b = b.AddStringWithTag(name, tag) - case []interface{}: + case []any: if len(value) > 0 { switch vv := value[0].(type) { - case map[string]interface{}: + case map[string]any: b, err = d.addForStringMap(b, vv, true, tag, name, nest, useTag) if err != nil { return nil, err @@ -203,14 +203,14 @@ func (d *Decoder) toDsFromStringMap(m map[string]interface{}, nest bool, useTag } b = b.AddDynamicStructSliceWithTag(name, nds, tag) } else { - b = b.AddSliceWithTag(name, interface{}(vv), tag) + b = b.AddSliceWithTag(name, any(vv), tag) } default: - // FIXME: 配列要素を全て "interface{}" にキャストしているが、型を明示したい - b = b.AddSliceWithTag(name, interface{}(vv), tag) + // FIXME: 配列要素を全て "any" にキャストしているが、型を明示したい + b = b.AddSliceWithTag(name, any(vv), tag) } } - case map[string]interface{}: + case map[string]any: b, err = d.addForStringMap(b, value, false, tag, name, nest, useTag) if err != nil { return nil, err @@ -245,7 +245,7 @@ func (d *Decoder) toDsFromStringMap(m map[string]interface{}, nest bool, useTag func (d *Decoder) addForStringMap( b *dynamicstruct.Builder, - m map[string]interface{}, + m map[string]any, forSlice bool, tag string, name string, @@ -262,7 +262,7 @@ func (d *Decoder) addForStringMap( } b = b.AddDynamicStructWithTag(name, nds, false, tag) } else if forSlice { - b = b.AddSliceWithTag(name, interface{}(m), tag) + b = b.AddSliceWithTag(name, any(m), tag) } else { for kk := range m { b = b.AddMapWithTag(name, kk, nil, tag) @@ -275,15 +275,15 @@ func (d *Decoder) addForStringMap( } // Note: this is dead case with gopkg.in/yaml.v3 (but alive with v2) -// convert map[interface{}]interface{} to map[string]interface{} -// func toStringKeyMap(mapii map[interface{}]interface{}) map[string]interface{} { -// mapsi := make(map[string]interface{}) +// convert map[any]any to map[string]any +// func toStringKeyMap(mapii map[any]any) map[string]any { +// mapsi := make(map[string]any) // for k, v := range mapii { // switch vt := v.(type) { -// case []interface{}: +// case []any: // // for nest array // mapsi[fmt.Sprintf("%v", k)] = fromArrayToMapValue(vt) -// case map[interface{}]interface{}: +// case map[any]any: // // for nest object // mapsi[fmt.Sprintf("%v", k)] = toStringKeyMap(vt) // default: @@ -295,16 +295,16 @@ func (d *Decoder) addForStringMap( // } // Note: this is dead case with gopkg.in/yaml.v3 (but alive with v2) -// func fromArrayToMapValue(ia []interface{}) interface{} { -// resIa := make([]interface{}, 0, len(ia)) +// func fromArrayToMapValue(ia []any) any { +// resIa := make([]any, 0, len(ia)) // for _, iv := range ia { // switch ivt := iv.(type) { -// case []interface{}: +// case []any: // // for nest array // resIa = append(resIa, fromArrayToMapValue(ivt)) -// case map[interface{}]interface{}: +// case map[any]any: // // for nest object -// // !!! this is important process for map[interface{}]interface{} to map[string]interface{} for JSON unmarshaling +// // !!! this is important process for map[any]any to map[string]any for JSON unmarshaling // resIa = append(resIa, toStringKeyMap(ivt)) // default: // resIa = append(resIa, ivt) diff --git a/dynamicstruct/dynamicstruct.go b/dynamicstruct/dynamicstruct.go index c3fb5e01..128c4f0c 100644 --- a/dynamicstruct/dynamicstruct.go +++ b/dynamicstruct/dynamicstruct.go @@ -74,7 +74,7 @@ func (ds *DynamicStruct) IsPtr() bool { } // NewInterface returns the new interface value of built struct. -func (ds *DynamicStruct) NewInterface() interface{} { +func (ds *DynamicStruct) NewInterface() any { rv := reflect.New(ds.rt) if ds.isPtr { return rv.Interface() diff --git a/dynamicstruct/dynamicstruct_test.go b/dynamicstruct/dynamicstruct_test.go index 87fb7a5b..141d5818 100644 --- a/dynamicstruct/dynamicstruct_test.go +++ b/dynamicstruct/dynamicstruct_test.go @@ -25,8 +25,8 @@ type ( Stringptr *string Stringslice []string Bool bool - Map map[string]interface{} - Func func(string) interface{} + Map map[string]any + Func func(string) any // ChInt chan int // Note: type chan is not supported by mapstructure DynamicTestStruct2 DynamicTestStruct2Ptr *DynamicTestStruct2 @@ -327,7 +327,7 @@ const expectedDefinition = `type DynamicStruct struct { var ( dynamicTestString2 = "test name2" - dynamicTestFunc = func(s string) interface{} { return s + "-func" } + dynamicTestFunc = func(s string) any { return s + "-func" } //dynamicTestChan = make(chan int) ) @@ -345,7 +345,7 @@ func newTestDynamicStruct() DynamicTestStruct { Stringptr: &dynamicTestString2, Stringslice: []string{"strslice1", "strslice2"}, Bool: true, - Map: map[string]interface{}{"k1": "v1", "k2": 2}, + Map: map[string]any{"k1": "v1", "k2": 2}, Func: dynamicTestFunc, // ChInt: dynamicTestChan, // Note: type chan is not supported by mapstructure DynamicTestStruct2: DynamicTestStruct2{ @@ -407,8 +407,8 @@ func newTestBuilder() *Builder { AddBoolWithTag("BoolFieldWithTag", boolFieldTag). AddMap("MapField", SampleString, SampleFloat32). AddMapWithTag("MapFieldWithTag", SampleString, SampleFloat32, mapFieldTag). - AddFunc("FuncField", []interface{}{SampleInt, SampleInt}, []interface{}{SampleBool, ErrSample}). - AddFuncWithTag("FuncFieldWithTag", []interface{}{SampleInt, SampleInt}, []interface{}{SampleBool, ErrSample}, funcFieldTag). + AddFunc("FuncField", []any{SampleInt, SampleInt}, []any{SampleBool, ErrSample}). + AddFuncWithTag("FuncFieldWithTag", []any{SampleInt, SampleInt}, []any{SampleBool, ErrSample}, funcFieldTag). AddChanBoth("ChanBothField", SampleInt). AddChanBothWithTag("ChanBothFieldWithTag", SampleInt, chanBothFieldTag). AddChanRecv("ChanRecvField", SampleInt). @@ -618,7 +618,7 @@ func TestBuilderAddFuncWithNilArgs(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - _, err := tt.args.builder.AddFunc("FuncFieldWithNilArgs", nil, []interface{}{SampleBool}).Build() + _, err := tt.args.builder.AddFunc("FuncFieldWithNilArgs", nil, []any{SampleBool}).Build() if err != nil { t.Errorf("unexpected error occurred: args: %+v, %v", tt.args, err) } @@ -647,7 +647,7 @@ func TestBuilderAddFuncWithNilReturns(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - _, err := tt.args.builder.AddFunc("FuncFieldWithNilReturns", []interface{}{SampleInt}, nil).Build() + _, err := tt.args.builder.AddFunc("FuncFieldWithNilReturns", []any{SampleInt}, nil).Build() if err != nil { t.Errorf("unexpected error occurred: args: %+v, %v", tt.args, err) } diff --git a/dynamicstruct/example_test.go b/dynamicstruct/example_test.go index 19918df2..abdd23a9 100644 --- a/dynamicstruct/example_test.go +++ b/dynamicstruct/example_test.go @@ -10,7 +10,7 @@ import ( func Example() { type Hoge struct { Key string - Value interface{} + Value any } hogePtr := &Hoge{ @@ -63,8 +63,8 @@ func Example() { func Example_unmarshalJSON() { type Hoge struct { - Key string `json:"key"` - Value interface{} `json:"value"` + Key string `json:"key"` + Value any `json:"value"` } var hogePtr *Hoge diff --git a/example_test.go b/example_test.go index 6c3f93f4..601178a4 100644 --- a/example_test.go +++ b/example_test.go @@ -35,7 +35,7 @@ func ExampleGetter() { name, _ := getter.String("Name") // get as string age, _ := getter.Int("Age") // get as int - company, _ := getter.Get("Company") // get as interface{} + company, _ := getter.Get("Company") // get as any fmt.Printf( "num of fields=%d\nfield names=%v\n'Name'=%s\n'Age'=%d\n'Company'=%+v", @@ -90,7 +90,7 @@ func ExampleGetter_MapGet() { } // Each of "Companies" field are applied map function as follows. - fn := func(i int, g *Getter) (interface{}, error) { + fn := func(i int, g *Getter) (any, error) { period, _ := g.Int("Period") name, _ := g.String("Name") diff --git a/finder.go b/finder.go index 94ea9d65..24d60e33 100644 --- a/finder.go +++ b/finder.go @@ -25,13 +25,13 @@ type Finder struct { // NewFinder returns a concrete Finder that uses and obtains from i. // i must be a struct or struct pointer. -func NewFinder(i interface{}) (*Finder, error) { +func NewFinder(i any) (*Finder, error) { return NewFinderWithSep(i, defaultSep) } // NewFinderWithSep returns a concrete Finder that uses and obtains from i using the separator string. // i must be a struct or struct pointer. -func NewFinderWithSep(i interface{}, sep string) (*Finder, error) { +func NewFinderWithSep(i any, sep string) (*Finder, error) { g, err := NewGetter(i) if err != nil { return nil, err @@ -119,7 +119,7 @@ func (f *Finder) Into(names ...string) *Finder { var nextGetter *Getter var ok bool var err error - var intf interface{} + var intf any nextKey := "" for _, name := range names { @@ -199,12 +199,12 @@ func (f *Finder) FromKeys(fks *FinderKeys) *Finder { // ToMap returns a map converted from struct. // Map keys are lookup field names by "Into" method and "Find". // Map values are lookup field values by "Into" method and "Find". -func (f *Finder) ToMap() (map[string]interface{}, error) { +func (f *Finder) ToMap() (map[string]any, error) { if f.HasError() { return nil, f } - res := map[string]interface{}{} + res := map[string]any{} var key string // kg is separated by f.sep step-by-step @@ -235,15 +235,15 @@ func (f *Finder) ToMap() (map[string]interface{}, error) { // ToNestedMap preturns a map converted from struct with nested keys. // FIXME: EXPERIMENTAL (this method has a bug) /* -func (f *Finder) ToNestedMap() (map[string]interface{}, error) { +func (f *Finder) ToNestedMap() (map[string]any, error) { if f.HasError() { return nil, f } - res := map[string]interface{}{} + res := map[string]any{} for kg, getter := range f.getterMap { - m := map[string]interface{}{} + m := map[string]any{} for _, name := range f.namesMap[kg] { i, ok := getter.Get(name) @@ -309,7 +309,7 @@ type FinderKeys struct { } type confKeys struct { - Keys []interface{} + Keys []any } // NewFinderKeys returns a FinderKeys object @@ -348,7 +348,7 @@ func newFinderKeysFromConf(ck confKeys) (*FinderKeys, error) { return fks, nil } -func (fks *FinderKeys) addRecursive(key interface{}, prefix string) error { +func (fks *FinderKeys) addRecursive(key any, prefix string) error { var res string switch t := key.(type) { @@ -358,9 +358,9 @@ func (fks *FinderKeys) addRecursive(key interface{}, prefix string) error { res = prefix + defaultSep + res } fks.keys = append(fks.keys, res) // set here - case map[string]interface{}: + case map[string]any: var nk string - var nd interface{} + var nd any for key, value := range t { nk = key if prefix != "" { @@ -373,9 +373,9 @@ func (fks *FinderKeys) addRecursive(key interface{}, prefix string) error { if err != nil { return err } - case map[interface{}]interface{}: + case map[any]any: var nk string - var nd interface{} + var nd any for key, value := range t { // FIXME: Should this code be changed to 'nk = fmt.Sprintf("%v", key)' ? nk = key.(string) @@ -389,7 +389,7 @@ func (fks *FinderKeys) addRecursive(key interface{}, prefix string) error { if err != nil { return err } - case []interface{}: + case []any: var err error for _, value := range t { err = fks.addRecursive(value, prefix) diff --git a/finder_test.go b/finder_test.go index 09a8bc38..186cbf21 100644 --- a/finder_test.go +++ b/finder_test.go @@ -24,8 +24,8 @@ type ( Stringptr *string Stringslice []string Bool bool - Map map[string]interface{} - Func func(string) interface{} + Map map[string]any + Func func(string) any ChInt chan int privateString string FinderTestStruct2 @@ -52,7 +52,7 @@ type ( var ( finderTestString2 = "test name2" - finderTestFunc = func(s string) interface{} { return s + "-func" } + finderTestFunc = func(s string) any { return s + "-func" } finderTestChan = make(chan int) ) @@ -70,7 +70,7 @@ func newFinderTestStruct() FinderTestStruct { Stringptr: &finderTestString2, Stringslice: []string{"strslice1", "strslice2"}, Bool: true, - Map: map[string]interface{}{"k1": "v1", "k2": 2}, + Map: map[string]any{"k1": "v1", "k2": 2}, Func: finderTestFunc, ChInt: finderTestChan, privateString: "unexported string", @@ -120,7 +120,7 @@ func TestNewFinder(t *testing.T) { t.Parallel() type args struct { - i interface{} + i any } tests := []struct { name string @@ -245,8 +245,8 @@ func TestFinderToMap(t *testing.T) { args args wantError bool wantErrorString string - wantMap map[string]interface{} - wantNestedMap map[string]interface{} // FIXME: not implemented ToNestedMap method yet + wantMap map[string]any + wantNestedMap map[string]any // FIXME: not implemented ToNestedMap method yet cmpopts []cmp.Option }{ { @@ -270,14 +270,14 @@ func TestFinderToMap(t *testing.T) { "FinderTestStruct4PtrSlice", ), }, - wantMap: map[string]interface{}{ + wantMap: map[string]any{ "Int64": int64(-1), "Float64": float64(-3.45), "String": "test name", "Stringptr": finderTestString2, "Stringslice": []string{"strslice1", "strslice2"}, "Bool": true, - "Map": map[string]interface{}{"k1": "v1", "k2": 2}, + "Map": map[string]any{"k1": "v1", "k2": 2}, //"Func": finderTestFunc, // TODO: func is fail "ChInt": finderTestChan, "privateString": nil, // unexported field is nil @@ -305,11 +305,11 @@ func TestFinderToMap(t *testing.T) { chain: fs[1]. Into("FinderTestStruct2").Find("String"), }, - wantMap: map[string]interface{}{ + wantMap: map[string]any{ "FinderTestStruct2.String": "struct2 string", }, - wantNestedMap: map[string]interface{}{ - "FinderTestStruct2": map[string]interface{}{ + wantNestedMap: map[string]any{ + "FinderTestStruct2": map[string]any{ "String": "struct2 string", }, }, @@ -320,13 +320,13 @@ func TestFinderToMap(t *testing.T) { chain: fs[2]. Into("FinderTestStruct2Ptr", "FinderTestStruct3").Find("String", "Int"), }, - wantMap: map[string]interface{}{ + wantMap: map[string]any{ "FinderTestStruct2Ptr.FinderTestStruct3.String": "struct3 string ptr", "FinderTestStruct2Ptr.FinderTestStruct3.Int": int(-456), }, - wantNestedMap: map[string]interface{}{ - "FinderTestStruct2Ptr": map[string]interface{}{ - "FinderTestStruFinderTestStruct3ct2Ptr": map[string]interface{}{ + wantNestedMap: map[string]any{ + "FinderTestStruct2Ptr": map[string]any{ + "FinderTestStruFinderTestStruct3ct2Ptr": map[string]any{ "String": "struct3 string ptr", "Int": int(-456), }, @@ -341,18 +341,18 @@ func TestFinderToMap(t *testing.T) { Into("FinderTestStruct2Ptr").Find("String"). Into("FinderTestStruct2Ptr", "FinderTestStruct3").Find("String", "Int"), }, - wantMap: map[string]interface{}{ + wantMap: map[string]any{ "FinderTestStruct2.String": "struct2 string", "FinderTestStruct2Ptr.String": "struct2 string ptr", "FinderTestStruct2Ptr.FinderTestStruct3.String": "struct3 string ptr", "FinderTestStruct2Ptr.FinderTestStruct3.Int": int(-456), }, - wantNestedMap: map[string]interface{}{ - "FinderTestStruct2": map[string]interface{}{ + wantNestedMap: map[string]any{ + "FinderTestStruct2": map[string]any{ "String": "struct2 string", - "FinderTestStruct2Ptr": map[string]interface{}{ + "FinderTestStruct2Ptr": map[string]any{ "String": "struct2 string ptr", - "FinderTestStruct3": map[string]interface{}{ + "FinderTestStruct3": map[string]any{ "String": "struct3 string ptr", "Int": int(-456), }, @@ -410,7 +410,7 @@ func TestFinderToMap(t *testing.T) { Into("FinderTestStruct2Ptr").Find("String"). Into("FinderTestStruct2Ptr", "FinderTestStruct3").Find("String", "Int"), }, - wantMap: map[string]interface{}{ + wantMap: map[string]any{ "FinderTestStruct2:String": "struct2 string", "FinderTestStruct2Ptr:String": "struct2 string ptr", "FinderTestStruct2Ptr:FinderTestStruct3:String": "struct3 string ptr", @@ -440,14 +440,14 @@ func TestFinderToMap(t *testing.T) { Into("FinderTestStruct2Ptr").Find("String"). Into("FinderTestStruct2Ptr", "FinderTestStruct3").Find("String", "Int"), }, - wantMap: map[string]interface{}{ + wantMap: map[string]any{ "Int64": int64(-1), "Float64": float64(-3.45), "String": "test name", "Stringptr": finderTestString2, "Stringslice": []string{"strslice1", "strslice2"}, "Bool": true, - "Map": map[string]interface{}{"k1": "v1", "k2": 2}, + "Map": map[string]any{"k1": "v1", "k2": 2}, //"Func": finderTestFunc, // TODO: func is fail "ChInt": finderTestChan, "privateString": nil, // unexported field is nil @@ -558,7 +558,7 @@ func TestFromKeys(t *testing.T) { args args wantError bool wantErrorString string - wantMap map[string]interface{} + wantMap map[string]any cmpopts []cmp.Option }{ { @@ -566,14 +566,14 @@ func TestFromKeys(t *testing.T) { args: args{ chain: fs[0].FromKeys(fks[0]), }, - wantMap: map[string]interface{}{ + wantMap: map[string]any{ "Int64": int64(-1), "Float64": float64(-3.45), "String": "test name", "Stringptr": finderTestString2, "Stringslice": []string{"strslice1", "strslice2"}, "Bool": true, - "Map": map[string]interface{}{"k1": "v1", "k2": 2}, + "Map": map[string]any{"k1": "v1", "k2": 2}, "ChInt": finderTestChan, "privateString": nil, // unexported field is nil "FinderTestStruct2": FinderTestStruct2{ diff --git a/getter.go b/getter.go index ba164fae..1ff4b368 100644 --- a/getter.go +++ b/getter.go @@ -18,7 +18,7 @@ type Getter struct { // NewGetter returns a concrete Getter that uses and obtains from i. // i must be a struct or struct pointer. -func NewGetter(i interface{}) (*Getter, error) { +func NewGetter(i any) (*Getter, error) { stVal, err := toStructValue(i) if err != nil { return nil, err @@ -42,7 +42,7 @@ func NewGetter(i interface{}) (*Getter, error) { // toStructValue returns a reflect.Value that can generate to Getter. // i must be a struct or struct pointer. -func toStructValue(i interface{}) (reflect.Value, error) { +func toStructValue(i any) (reflect.Value, error) { rv := reflect.ValueOf(i) kind := rv.Kind() if kind != reflect.Ptr && kind != reflect.Struct { @@ -63,7 +63,7 @@ type getterField struct { sFld reflect.StructField typ reflect.Type indirect reflect.Value // is Value via reflect.Indirect(v) - intf interface{} + intf any } func (gf *getterField) isKind(kind reflect.Kind) bool { @@ -135,7 +135,7 @@ func (g *Getter) GetValue(name string) (reflect.Value, bool) { // Get returns the interface of the original struct field named name. // 2nd return value will be false if the original struct does not have a "name" field. -func (g *Getter) Get(name string) (interface{}, bool) { +func (g *Getter) Get(name string) (any, bool) { gf, ok := g.getSafely(name) if ok { return gf.intf, true @@ -145,8 +145,8 @@ func (g *Getter) Get(name string) (interface{}, bool) { } // ToMap returns a map converted from this Getter. -func (g *Getter) ToMap() map[string]interface{} { - m := make(map[string]interface{}) +func (g *Getter) ToMap() map[string]any { + m := make(map[string]any) for name, gf := range g.fields { m[name] = gf.intf } @@ -163,7 +163,7 @@ func (g *Getter) IsSlice(name string) bool { // Slice returns the slice of interface of the original struct field named name. // 2nd return value will be false if the original struct does not have a "name" field. // 2nd return value will be false if type of the original struct "name" field is not slice of interface. -func (g *Getter) Slice(name string) ([]interface{}, bool) { +func (g *Getter) Slice(name string) ([]any, bool) { gf, ok := g.getSafelyKindly(name, reflect.Slice) if !ok { return nil, false @@ -172,7 +172,7 @@ func (g *Getter) Slice(name string) ([]interface{}, bool) { len := gf.indirect.Len() // See: https://golang.org/doc/faq#convert_slice_of_interface - iSlice := make([]interface{}, len) + iSlice := make([]any, len) for i := 0; i < len; i++ { iSlice[i] = gf.indirect.Index(i).Interface() } @@ -607,7 +607,7 @@ func (g *Getter) GetGetter(name string) (*Getter, bool) { } // MapGet returns the interface slice of mapped values of the original struct field named name. -func (g *Getter) MapGet(name string, f func(int, *Getter) (interface{}, error)) ([]interface{}, error) { +func (g *Getter) MapGet(name string, f func(int, *Getter) (any, error)) ([]any, error) { gf, ok := g.getSafelyKindly(name, reflect.Slice) if !ok { return nil, fmt.Errorf("field %s does not exist or is not slice type", name) @@ -616,9 +616,9 @@ func (g *Getter) MapGet(name string, f func(int, *Getter) (interface{}, error)) var vi reflect.Value var eg *Getter var err error - var r interface{} + var r any - res := make([]interface{}, gf.indirect.Len()) + res := make([]any, gf.indirect.Len()) for i := 0; i < gf.indirect.Len(); i++ { vi = gf.indirect.Index(i) diff --git a/getter_test.go b/getter_test.go index 5643f1e6..72ce203f 100644 --- a/getter_test.go +++ b/getter_test.go @@ -36,8 +36,8 @@ type ( Unsafeptr unsafe.Pointer Stringslice []string Stringarray [2]string - Map map[string]interface{} - Func func(string) interface{} + Map map[string]any + Func func(string) any ChInt chan int privateString string GetterTestStruct2 @@ -64,7 +64,7 @@ type ( var ( getterTestString2 = "test name2" - getterTestFunc = func(s string) interface{} { return s + "-func" } + getterTestFunc = func(s string) any { return s + "-func" } getterTestChan = make(chan int) ) @@ -94,7 +94,7 @@ func newGetterTestStruct() GetterTestStruct { Unsafeptr: unsafe.Pointer(new(int)), Stringslice: []string{"strslice1", "strslice2"}, Stringarray: [2]string{"strarray1", "strarray2"}, - Map: map[string]interface{}{"k1": "v1", "k2": 2}, + Map: map[string]any{"k1": "v1", "k2": 2}, Func: getterTestFunc, ChInt: getterTestChan, privateString: "unexported string", @@ -146,14 +146,14 @@ func newTestGetter() (*Getter, error) { type getterTestArgs struct { name string - mapfn func(int, *Getter) (interface{}, error) + mapfn func(int, *Getter) (any, error) } type getterTest struct { name string args *getterTestArgs wantBool bool - wantIntf interface{} + wantIntf any wantType reflect.Type wantValue reflect.Value wantError bool @@ -292,7 +292,7 @@ func TestNewGetter(t *testing.T) { testStructPtr := newGetterTestStructPtr() type args struct { - i interface{} + i any } tests := []struct { name string @@ -349,7 +349,7 @@ func TestNumField(t *testing.T) { t.Parallel() type args struct { - i interface{} + i any } tests := []struct { name string @@ -420,7 +420,7 @@ func TestNames(t *testing.T) { t.Parallel() type args struct { - i interface{} + i any } tests := []struct { name string @@ -1501,11 +1501,11 @@ func TestSlice(t *testing.T) { switch tt.name { case "Bytes": - tt.wantIntf = []interface{}{uint8(0), testStructPtr.Byte} + tt.wantIntf = []any{uint8(0), testStructPtr.Byte} case "GetterTestStruct4Slice": - tt.wantIntf = []interface{}{testStructPtr.GetterTestStruct4Slice[0], testStructPtr.GetterTestStruct4Slice[1]} + tt.wantIntf = []any{testStructPtr.GetterTestStruct4Slice[0], testStructPtr.GetterTestStruct4Slice[1]} case "GetterTestStruct4PtrSlice": - tt.wantIntf = []interface{}{testStructPtr.GetterTestStruct4PtrSlice[0], testStructPtr.GetterTestStruct4PtrSlice[1]} + tt.wantIntf = []any{testStructPtr.GetterTestStruct4PtrSlice[0], testStructPtr.GetterTestStruct4PtrSlice[1]} default: tt.wantNotOK = true } @@ -2291,19 +2291,19 @@ func TestMapGet(t *testing.T) { switch tt.name { case "GetterTestStruct4Slice": - tt.args.mapfn = func(i int, g *Getter) (interface{}, error) { + tt.args.mapfn = func(i int, g *Getter) (any, error) { str, _ := g.String("String") str2, _ := g.String("String2") return fmt.Sprintf("%s=%s", str, str2), nil } - tt.wantIntf = []interface{}{string("key100=value100"), string("key200=value200")} + tt.wantIntf = []any{string("key100=value100"), string("key200=value200")} case "GetterTestStruct4PtrSlice": - tt.args.mapfn = func(i int, g *Getter) (interface{}, error) { + tt.args.mapfn = func(i int, g *Getter) (any, error) { str, _ := g.String("String") str2, _ := g.String("String2") return fmt.Sprintf("%s:%s", str, str2), nil } - tt.wantIntf = []interface{}{string("key991:value991"), string("key992:value992")} + tt.wantIntf = []any{string("key991:value991"), string("key992:value992")} default: tt.wantError = true } diff --git a/internal/dumpwriter.go b/internal/dumpwriter.go index fec0ea54..6d7f265e 100644 --- a/internal/dumpwriter.go +++ b/internal/dumpwriter.go @@ -70,9 +70,9 @@ func (dw *dwImpl) Flush() error { // Dump writes reflection values to a dump target with automation Flush. func (dw *dwImpl) Dump(rvs ...reflect.Value) error { - var t interface{} + var t any - ds := make([][]interface{}, len(rvs)) + ds := make([][]any, len(rvs)) for i, rv := range rvs { if rv.IsValid() { @@ -80,7 +80,7 @@ func (dw *dwImpl) Dump(rvs ...reflect.Value) error { } else { t = rv.Kind() } - ds[i] = []interface{}{ + ds[i] = []any{ t, // Type rv, // Value } diff --git a/util/benchmark_test.go b/util/benchmark_test.go index e2721a1f..1dfdf52e 100644 --- a/util/benchmark_test.go +++ b/util/benchmark_test.go @@ -37,7 +37,7 @@ func BenchmarkToI_Nil(b *testing.B) { } func benchmarkToI(v reflect.Value, b *testing.B) { - var intf interface{} + var intf any b.ResetTimer() for i := 0; i < b.N; i++ { @@ -78,7 +78,7 @@ func BenchmarkElemTypeOf_Nil(b *testing.B) { benchmarkElemTypeOf(nil, b) } -func benchmarkElemTypeOf(i interface{}, b *testing.B) { +func benchmarkElemTypeOf(i any, b *testing.B) { var typ reflect.Type b.ResetTimer() diff --git a/util/reflect_util.go b/util/reflect_util.go index 6f8399c1..adbf99f6 100644 --- a/util/reflect_util.go +++ b/util/reflect_util.go @@ -8,7 +8,7 @@ import ( ) // ToI returns a converted interface from rv. -func ToI(rv reflect.Value) interface{} { +func ToI(rv reflect.Value) any { if rv.IsValid() && rv.CanInterface() { return rv.Interface() } @@ -18,7 +18,7 @@ func ToI(rv reflect.Value) interface{} { // ElemTypeOf returns a element Type from i. // If the i's type's Kind is Array, Chan, Map, Ptr, or Slice then this returns the element Type. // Otherwise this returns i's original Type. -func ElemTypeOf(i interface{}) reflect.Type { +func ElemTypeOf(i any) reflect.Type { if i == nil { return nil } @@ -35,7 +35,7 @@ func ElemTypeOf(i interface{}) reflect.Type { } // RecoverToError returns an error converted from recoverd panic information. -func RecoverToError(r interface{}) (err error) { +func RecoverToError(r any) (err error) { if r != nil { msg := fmt.Sprintf("\n%v\n", r) + stackTrace() err = fmt.Errorf("unexpected panic occurred: %s", msg) @@ -60,7 +60,7 @@ func stackTrace() string { /* // Note: Publicize candidate -func elemOf(i interface{}) reflect.Value { +func elemOf(i any) reflect.Value { v := reflect.Indirect(reflect.ValueOf(i)) k := v.Kind() @@ -76,13 +76,13 @@ func elemOf(i interface{}) reflect.Value { } // Note: Publicize candidate -func settableOf(i interface{}) reflect.Value { +func settableOf(i any) reflect.Value { // i's Kind must be Interface or Ptr(if else, occur panic) return reflect.ValueOf(i).Elem() } // Note: Publicize candidate -func clone(i interface{}) interface{} { +func clone(i any) any { return reflect.Indirect(reflect.ValueOf(i)).Interface() } @@ -92,25 +92,25 @@ func newSettable(typ reflect.Type) reflect.Value { } // Note: Publicize candidate -func isImplements(i interface{}, t interface{}) bool { +func isImplements(i any, t any) bool { typ := reflect.TypeOf(t).Elem() return isImplementsType(i, typ) } -func isImplementsType(i interface{}, typ reflect.Type) bool { +func isImplementsType(i any, typ reflect.Type) bool { v := reflect.ValueOf(i) return typ.Implements(v.Type()) } func genericsTypeOf() reflect.Type { - return reflect.TypeOf((*interface{})(nil)).Elem() + return reflect.TypeOf((*any)(nil)).Elem() } func newGenericsSettable() reflect.Value { return newSettable(genericsTypeOf()) } -func privateFieldValueOf(i interface{}, name string) reflect.Value { +func privateFieldValueOf(i any, name string) reflect.Value { sv := settableOf(i) f := sv.FieldByName(name) return reflect.NewAt(f.Type(), unsafe.Pointer(f.UnsafeAddr())).Elem() diff --git a/util/reflect_util_test.go b/util/reflect_util_test.go index cf591111..c98e980e 100644 --- a/util/reflect_util_test.go +++ b/util/reflect_util_test.go @@ -17,8 +17,8 @@ type tStruct struct { var ( testTstrPtr = &tStruct{10, "Name10"} - testMap = map[string]interface{}{"key1": "value1", "key2": 2} - testFunc = func(s string) interface{} { return s + "-func" } + testMap = map[string]any{"key1": "value1", "key2": 2} + testFunc = func(s string) any { return s + "-func" } testChan = make(chan int) ) @@ -31,7 +31,7 @@ func TestToI(t *testing.T) { tests := []struct { name string args args - want interface{} + want any }{ { name: "string", @@ -110,7 +110,7 @@ func TestElemTypeOf(t *testing.T) { t.Parallel() type args struct { - i interface{} + i any } tests := []struct { name string