diff --git a/adapters.go b/adapters.go index 82b7cda..56add96 100644 --- a/adapters.go +++ b/adapters.go @@ -19,10 +19,10 @@ import ( // // The simples way to create new adapters is via the NewAdapter function. type Adapter interface { - FromString(s string) (interface{}, error) + FromString(s string) (any, error) MarshalText() (text []byte, err error) Type() reflect.Type - Wrap(v interface{}) Adapter + Wrap(v any) Adapter } // DefaultTypeAdapters contains the default set of type adapters. @@ -31,18 +31,18 @@ var DefaultTypeAdapters = []Adapter{ NewAdapter(time.Now(), nil, nil), NewAdapter( 3*time.Hour, - func(s string) (interface{}, error) { return time.ParseDuration(s) }, - func(v interface{}) (string, error) { return v.(time.Duration).String(), nil }, + func(s string) (any, error) { return time.ParseDuration(s) }, + func(v any) (string, error) { return v.(time.Duration).String(), nil }, ), // Numbers NewAdapter(big.NewRat(1, 2), nil, nil), NewAdapter( int(32), - func(s string) (interface{}, error) { + func(s string) (any, error) { return strconv.Atoi(s) }, - func(v interface{}) (string, error) { + func(v any) (string, error) { return strconv.Itoa(v.(int)), nil }, ), @@ -55,9 +55,9 @@ var DefaultTypeAdapters = []Adapter{ // // It will panic if it can not be created. func NewAdapter( - target interface{}, - fromString func(s string) (interface{}, error), - toString func(v interface{}) (string, error), + target any, + fromString func(s string) (any, error), + toString func(v any) (string, error), ) Adapter { targetValue := reflect.ValueOf(target) targetType := targetValue.Type() @@ -72,7 +72,7 @@ func NewAdapter( if fromString == nil { if _, ok := targetValue.Interface().(encoding.TextUnmarshaler); ok { - fromString = func(s string) (interface{}, error) { + fromString = func(s string) (any, error) { typ := targetType if typ.Kind() == reflect.Ptr { typ = typ.Elem() @@ -94,15 +94,15 @@ func NewAdapter( } } - var marshalText func(v interface{}) ([]byte, error) + var marshalText func(v any) ([]byte, error) if toString != nil { - marshalText = func(v interface{}) ([]byte, error) { + marshalText = func(v any) ([]byte, error) { s, err := toString(v) return []byte(s), err } } else if _, ok := target.(encoding.TextMarshaler); ok { - marshalText = func(v interface{}) ([]byte, error) { + marshalText = func(v any) ([]byte, error) { return v.(encoding.TextMarshaler).MarshalText() } } else { @@ -119,14 +119,14 @@ func NewAdapter( var _ Adapter = (*adapter)(nil) type adapter struct { - fromString func(s string) (interface{}, error) - marshalText func(v interface{}) (text []byte, err error) + fromString func(s string) (any, error) + marshalText func(v any) (text []byte, err error) targetType reflect.Type - target interface{} + target any } -func (a *adapter) FromString(s string) (interface{}, error) { +func (a *adapter) FromString(s string) (any, error) { return a.fromString(s) } @@ -138,7 +138,7 @@ func (a adapter) Type() reflect.Type { return a.targetType } -func (a adapter) Wrap(v interface{}) Adapter { +func (a adapter) Wrap(v any) Adapter { a.target = v return &a } diff --git a/codec.go b/codec.go index 07369c7..1002fcf 100644 --- a/codec.go +++ b/codec.go @@ -92,7 +92,7 @@ type Codec struct { // Marshal accepts a Go map and marshals it to the configured marshaler // anntated with type information. -func (c *Codec) Marshal(v interface{}) ([]byte, error) { +func (c *Codec) Marshal(v any) ([]byte, error) { m, err := c.toTypedMap(v) if err != nil { return nil, err @@ -103,7 +103,7 @@ func (c *Codec) Marshal(v interface{}) ([]byte, error) { // Unmarshal unmarshals the given data to the given Go map, using // any annotated type information found to preserve the type information // stored in Marshal. -func (c *Codec) Unmarshal(data []byte, v interface{}) error { +func (c *Codec) Unmarshal(data []byte, v any) error { if err := c.marshaler.Unmarshal(data, v); err != nil { return err } @@ -115,7 +115,7 @@ func (c *Codec) newKey(key reflect.Value, a Adapter) reflect.Value { return reflect.ValueOf(fmt.Sprintf("%s%s%s", key, c.typeSep, a.Type())) } -func (c *Codec) fromTypedMap(mi interface{}) (reflect.Value, error) { +func (c *Codec) fromTypedMap(mi any) (reflect.Value, error) { m := reflect.ValueOf(mi) if m.Kind() == reflect.Ptr { m = m.Elem() @@ -207,12 +207,12 @@ func (c *Codec) fromTypedMap(mi interface{}) (reflect.Value, error) { } var ( - interfaceMapType = reflect.TypeOf(make(map[string]interface{})) - interfaceSliceType = reflect.TypeOf([]interface{}{}) - stringType = reflect.TypeOf("") + interfaceMapType = reflect.TypeOf(make(map[string]any)) + interfaceSliceType = reflect.TypeOf([]any{}) + stringType = reflect.TypeFor[string]() ) -func (c *Codec) toTypedMap(mi interface{}) (interface{}, error) { +func (c *Codec) toTypedMap(mi any) (any, error) { mv := reflect.ValueOf(mi) if mv.Kind() != reflect.Map || mv.Type().Key().Kind() != reflect.String { @@ -281,17 +281,17 @@ func (c *Codec) toStringMap(mi reflect.Value) (reflect.Value, error) { // MarshalUnmarshaler is the interface that must be implemented if you want to // add support for more than JSON to this codec. type MarshalUnmarshaler interface { - Marshal(v interface{}) ([]byte, error) - Unmarshal(b []byte, v interface{}) error + Marshal(v any) ([]byte, error) + Unmarshal(b []byte, v any) error } type jsonMarshaler int -func (jsonMarshaler) Marshal(v interface{}) ([]byte, error) { +func (jsonMarshaler) Marshal(v any) ([]byte, error) { return json.Marshal(v) } -func (jsonMarshaler) Unmarshal(b []byte, v interface{}) error { +func (jsonMarshaler) Unmarshal(b []byte, v any) error { return json.Unmarshal(b, v) } diff --git a/codec_test.go b/codec_test.go index ef835f6..0750ac1 100644 --- a/codec_test.go +++ b/codec_test.go @@ -55,7 +55,6 @@ func TestRoundtrip(t *testing.T) { }, } { - test := test c.Run(test.name, func(c *qt.C) { c.Parallel() @@ -69,7 +68,7 @@ func TestRoundtrip(t *testing.T) { test.dataAssert(c, string(data)) } - dst := make(map[string]interface{}) + dst := make(map[string]any) c.Assert(codec.Unmarshal(data, &dst), qt.IsNil) c.Assert(dst, eq, src) @@ -82,19 +81,19 @@ func TestErrors(t *testing.T) { c := qt.New(t) codec, err := New() c.Assert(err, qt.IsNil) - marshal := func(v interface{}) error { + marshal := func(v any) error { _, err := codec.Marshal(v) return err } // OK - c.Assert(marshal(map[string]interface{}{"32": "a"}), qt.IsNil) + c.Assert(marshal(map[string]any{"32": "a"}), qt.IsNil) c.Assert(marshal(map[string]int{"32": 32}), qt.IsNil) // Should fail c.Assert(marshal([]string{"a"}), qt.Not(qt.IsNil)) - c.Assert(marshal(map[int]interface{}{32: "a"}), qt.Not(qt.IsNil)) - c.Assert(marshal(map[string]interface{}{"a": map[int]string{32: "32"}}), qt.Not(qt.IsNil)) + c.Assert(marshal(map[int]any{32: "a"}), qt.Not(qt.IsNil)) + c.Assert(marshal(map[string]any{"a": map[int]string{32: "32"}}), qt.Not(qt.IsNil)) } func BenchmarkCodec(b *testing.B) { @@ -107,7 +106,7 @@ func BenchmarkCodec(b *testing.B) { if err != nil { b.Fatal(err) } - m := make(map[string]interface{}) + m := make(map[string]any) if err := json.Unmarshal(data, &m); err != nil { b.Fatal(err) } @@ -127,7 +126,7 @@ func BenchmarkCodec(b *testing.B) { if err != nil { b.Fatal(err) } - m := make(map[string]interface{}) + m := make(map[string]any) if err := c.Unmarshal(data, &m); err != nil { b.Fatal(err) } @@ -135,8 +134,8 @@ func BenchmarkCodec(b *testing.B) { }) } -func newTestMap() map[string]interface{} { - return map[string]interface{}{ +func newTestMap() map[string]any { + return map[string]any{ "vstring": "Hello1", "vstring|": "Hello2", "vstring|foo": "Hello3", @@ -148,7 +147,7 @@ func newTestMap() map[string]interface{} { "vtime": time.Now(), "vduration": 3 * time.Second, "vsliceint": []int{1, 3, 4}, - "nested": map[string]interface{}{ + "nested": map[string]any{ "vint": 55, "vduration": 5 * time.Second, }, @@ -178,21 +177,21 @@ var eq = qt.CmpEquals( type yamlMarshaler int -func (yamlMarshaler) Marshal(v interface{}) ([]byte, error) { +func (yamlMarshaler) Marshal(v any) ([]byte, error) { return yaml.Marshal(v) } -func (yamlMarshaler) Unmarshal(b []byte, v interface{}) error { +func (yamlMarshaler) Unmarshal(b []byte, v any) error { return yaml.Unmarshal(b, v) } // Useful for debugging type jsonMarshalerIndent int -func (jsonMarshalerIndent) Marshal(v interface{}) ([]byte, error) { +func (jsonMarshalerIndent) Marshal(v any) ([]byte, error) { return json.MarshalIndent(v, "", " ") } -func (jsonMarshalerIndent) Unmarshal(b []byte, v interface{}) error { +func (jsonMarshalerIndent) Unmarshal(b []byte, v any) error { return json.Unmarshal(b, v) } diff --git a/examples_test.go b/examples_test.go index 6896a81..81d0f1c 100644 --- a/examples_test.go +++ b/examples_test.go @@ -14,7 +14,7 @@ import ( ) func Example() { - m1 := map[string]interface{}{"num": 42} + m1 := map[string]any{"num": 42} c, err := tmc.New() if err != nil { log.Fatal(err) @@ -25,7 +25,7 @@ func Example() { log.Fatal(err) } - m2 := make(map[string]interface{}) + m2 := make(map[string]any) err = c.Unmarshal(data, &m2) if err != nil { log.Fatal(err) @@ -37,7 +37,7 @@ func Example() { } func ExampleWithMarshalUnmarshaler() { - m1 := map[string]interface{}{"num": 42} + m1 := map[string]any{"num": 42} c, err := tmc.New(tmc.WithMarshalUnmarshaler(new(yamlMarshaler))) if err != nil { log.Fatal(err) @@ -48,7 +48,7 @@ func ExampleWithMarshalUnmarshaler() { log.Fatal(err) } - m2 := make(map[string]interface{}) + m2 := make(map[string]any) err = c.Unmarshal(data, &m2) if err != nil { log.Fatal(err) @@ -61,10 +61,10 @@ func ExampleWithMarshalUnmarshaler() { type yamlMarshaler int -func (yamlMarshaler) Marshal(v interface{}) ([]byte, error) { +func (yamlMarshaler) Marshal(v any) ([]byte, error) { return yaml.Marshal(v) } -func (yamlMarshaler) Unmarshal(b []byte, v interface{}) error { +func (yamlMarshaler) Unmarshal(b []byte, v any) error { return yaml.Unmarshal(b, v) }