mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-10 18:56:55 +02:00
replace Converter interface with string parser interface
This commit is contained in:
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
|
||||
}
|
||||
Reference in New Issue
Block a user