replace Converter interface with string parser interface

This commit is contained in:
yusing
2024-12-17 09:45:37 +08:00
parent c5d96f96e1
commit 0aa00ab226
3 changed files with 41 additions and 39 deletions

View 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
}