Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
},
),
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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)
}

Expand All @@ -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
}
22 changes: 11 additions & 11 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
29 changes: 14 additions & 15 deletions codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func TestRoundtrip(t *testing.T) {
},
} {

test := test
c.Run(test.name, func(c *qt.C) {
c.Parallel()

Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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)
}
Expand All @@ -127,16 +126,16 @@ 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)
}
}
})
}

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",
Expand All @@ -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,
},
Expand Down Expand Up @@ -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)
}
12 changes: 6 additions & 6 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}