mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 12:42:34 +02:00
replace Converter interface with string parser interface
This commit is contained in:
parent
c5d96f96e1
commit
0aa00ab226
3 changed files with 41 additions and 39 deletions
|
@ -18,12 +18,7 @@ import (
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type SerializedObject = map[string]any
|
||||||
SerializedObject = map[string]any
|
|
||||||
Converter interface {
|
|
||||||
ConvertFrom(value any) E.Error
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrInvalidType = E.New("invalid type")
|
ErrInvalidType = E.New("invalid type")
|
||||||
|
@ -349,14 +344,11 @@ func Convert(src reflect.Value, dst reflect.Value) E.Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var converter Converter
|
|
||||||
var ok bool
|
|
||||||
// check if (*T).Convertor is implemented
|
// check if (*T).Convertor is implemented
|
||||||
if converter, ok = dst.Addr().Interface().(Converter); !ok {
|
if parser, ok := dst.Addr().Interface().(strutils.Parser); ok {
|
||||||
return ErrUnsupportedConversion.Subjectf("%s to %s", srcT, dstT)
|
return E.From(parser.Parse(src.String()))
|
||||||
}
|
}
|
||||||
|
return ErrUnsupportedConversion.Subjectf("%s to %s", srcT, dstT)
|
||||||
return converter.ConvertFrom(src.Interface())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConvertString(src string, dst reflect.Value) (convertible bool, convErr E.Error) {
|
func ConvertString(src string, dst reflect.Value) (convertible bool, convErr E.Error) {
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
E "github.com/yusing/go-proxy/internal/error"
|
|
||||||
. "github.com/yusing/go-proxy/internal/utils/testing"
|
. "github.com/yusing/go-proxy/internal/utils/testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -132,40 +131,23 @@ type testType struct {
|
||||||
bar string
|
bar string
|
||||||
}
|
}
|
||||||
|
|
||||||
var errInvalid = errors.New("invalid input type")
|
func (c *testType) Parse(v string) (err error) {
|
||||||
|
c.bar = v
|
||||||
func (c *testType) ConvertFrom(v any) E.Error {
|
c.foo, err = strconv.Atoi(v)
|
||||||
switch v := v.(type) {
|
return
|
||||||
case string:
|
|
||||||
c.bar = v
|
|
||||||
return nil
|
|
||||||
case int:
|
|
||||||
c.foo = v
|
|
||||||
return nil
|
|
||||||
default:
|
|
||||||
return E.Errorf("%w %T", errInvalid, v)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConvertor(t *testing.T) {
|
func TestConvertor(t *testing.T) {
|
||||||
t.Run("string", func(t *testing.T) {
|
t.Run("valid", func(t *testing.T) {
|
||||||
m := new(testModel)
|
m := new(testModel)
|
||||||
ExpectNoError(t, Deserialize(map[string]any{"Test": "bar"}, m))
|
ExpectNoError(t, Deserialize(map[string]any{"Test": "123"}, m))
|
||||||
|
|
||||||
ExpectEqual(t, m.Test.foo, 0)
|
|
||||||
ExpectEqual(t, m.Test.bar, "bar")
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("int", func(t *testing.T) {
|
|
||||||
m := new(testModel)
|
|
||||||
ExpectNoError(t, Deserialize(map[string]any{"Test": 123}, m))
|
|
||||||
|
|
||||||
ExpectEqual(t, m.Test.foo, 123)
|
ExpectEqual(t, m.Test.foo, 123)
|
||||||
ExpectEqual(t, m.Test.bar, "")
|
ExpectEqual(t, m.Test.bar, "123")
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("invalid", func(t *testing.T) {
|
t.Run("invalid", func(t *testing.T) {
|
||||||
m := new(testModel)
|
m := new(testModel)
|
||||||
ExpectError(t, errInvalid, Deserialize(map[string]any{"Test": 123.456}, m))
|
ExpectError(t, strconv.ErrSyntax, Deserialize(map[string]any{"Test": 123}, m))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
28
internal/utils/strutils/parser.go
Normal file
28
internal/utils/strutils/parser.go
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
package strutils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/yusing/go-proxy/internal/logging"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Parser interface {
|
||||||
|
Parse(value string) error
|
||||||
|
}
|
||||||
|
|
||||||
|
func Parse[T Parser](from string) (t T, err error) {
|
||||||
|
tt := reflect.TypeOf(t)
|
||||||
|
if tt.Kind() == reflect.Ptr {
|
||||||
|
t = reflect.New(tt.Elem()).Interface().(T)
|
||||||
|
}
|
||||||
|
err = t.Parse(from)
|
||||||
|
return t, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func MustParse[T Parser](from string) T {
|
||||||
|
t, err := Parse[T](from)
|
||||||
|
if err != nil {
|
||||||
|
logging.Panic().Err(err).Msg("must failed")
|
||||||
|
}
|
||||||
|
return t
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue