mirror of
https://github.com/yusing/godoxy.git
synced 2026-02-24 09:44:58 +01:00
25 lines
548 B
Go
25 lines
548 B
Go
package utils
|
|
|
|
import (
|
|
"net/http"
|
|
"reflect"
|
|
"strings"
|
|
|
|
E "github.com/yusing/go-proxy/error"
|
|
)
|
|
|
|
func snakeToPascal(s string) string {
|
|
toHyphenCamel := http.CanonicalHeaderKey(strings.ReplaceAll(s, "_", "-"))
|
|
return strings.ReplaceAll(toHyphenCamel, "-", "")
|
|
}
|
|
|
|
func SetFieldFromSnake[T, VT any](obj *T, field string, value VT) E.NestedError {
|
|
field = snakeToPascal(field)
|
|
prop := reflect.ValueOf(obj).Elem().FieldByName(field)
|
|
if prop.Kind() == 0 {
|
|
return E.Invalid("field", field)
|
|
}
|
|
prop.Set(reflect.ValueOf(value))
|
|
return E.Nil()
|
|
}
|