diff --git a/internal/autocert/provider_test/ovh_test.go b/internal/autocert/provider_test/ovh_test.go index 6428dd31..6b84e1c1 100644 --- a/internal/autocert/provider_test/ovh_test.go +++ b/internal/autocert/provider_test/ovh_test.go @@ -45,6 +45,6 @@ oauth2_config: testYaml = testYaml[1:] // remove first \n opt := make(map[string]any) require.NoError(t, yaml.Unmarshal([]byte(testYaml), &opt)) - require.NoError(t, utils.Deserialize(opt, cfg)) + require.NoError(t, utils.MapUnmarshalValidate(opt, cfg)) require.Equal(t, cfg, cfgExpected) } diff --git a/internal/autocert/providers.go b/internal/autocert/providers.go index 7e5e12ab..dbbcb6d3 100644 --- a/internal/autocert/providers.go +++ b/internal/autocert/providers.go @@ -16,7 +16,7 @@ func DNSProvider[CT any, PT challenge.Provider]( ) Generator { return func(opt map[string]any) (challenge.Provider, gperr.Error) { cfg := defaultCfg() - err := utils.Deserialize(opt, &cfg) + err := utils.MapUnmarshalValidate(opt, &cfg) if err != nil { return nil, err } diff --git a/internal/config/config.go b/internal/config/config.go index ffcf7738..7aacdf18 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -220,7 +220,7 @@ func (cfg *Config) load() gperr.Error { } model := config.DefaultConfig() - if err := utils.DeserializeYAML(data, model); err != nil { + if err := utils.UnmarshalValidateYAML(data, model); err != nil { gperr.LogFatal(errMsg, err) } diff --git a/internal/config/types/config.go b/internal/config/types/config.go index 0934d6f7..a6396fee 100644 --- a/internal/config/types/config.go +++ b/internal/config/types/config.go @@ -88,7 +88,7 @@ func HasInstance() bool { func Validate(data []byte) gperr.Error { var model Config - return utils.DeserializeYAML(data, &model) + return utils.UnmarshalValidateYAML(data, &model) } var matchDomainsRegex = regexp.MustCompile(`^[^\.]?([\w\d\-_]\.?)+[^\.]?$`) diff --git a/internal/docker/container.go b/internal/docker/container.go index 915f988d..3e260386 100644 --- a/internal/docker/container.go +++ b/internal/docker/container.go @@ -178,7 +178,7 @@ func (c *Container) loadDeleteIdlewatcherLabels(helper containerHelper) { ContainerName: c.ContainerName, }, } - err := utils.Deserialize(cfg, idwCfg) + err := utils.MapUnmarshalValidate(cfg, idwCfg) if err != nil { gperr.LogWarn("invalid idlewatcher config", gperr.PrependSubject(c.ContainerName, err)) } else { diff --git a/internal/logging/accesslog/config_test.go b/internal/logging/accesslog/config_test.go index 55adde48..6d10f09a 100644 --- a/internal/logging/accesslog/config_test.go +++ b/internal/logging/accesslog/config_test.go @@ -30,7 +30,7 @@ func TestNewConfig(t *testing.T) { expect.NoError(t, err) var config RequestLoggerConfig - err = utils.Deserialize(parsed, &config) + err = utils.MapUnmarshalValidate(parsed, &config) expect.NoError(t, err) expect.Equal(t, config.BufferSize, 10) diff --git a/internal/net/gphttp/middleware/middleware.go b/internal/net/gphttp/middleware/middleware.go index ccf67059..c6b5fd80 100644 --- a/internal/net/gphttp/middleware/middleware.go +++ b/internal/net/gphttp/middleware/middleware.go @@ -118,7 +118,7 @@ func (m *Middleware) apply(optsRaw OptionsRaw) gperr.Error { } else { m.priority = DefaultPriority } - return utils.Deserialize(optsRaw, m.impl) + return utils.MapUnmarshalValidate(optsRaw, m.impl) } func (m *Middleware) finalize() error { diff --git a/internal/notif/config.go b/internal/notif/config.go index 1f35cdb5..5f48f2e6 100644 --- a/internal/notif/config.go +++ b/internal/notif/config.go @@ -47,7 +47,7 @@ func (cfg *NotificationConfig) UnmarshalMap(m map[string]any) (err gperr.Error) } // unmarshal provider config - if err := utils.Deserialize(m, cfg.Provider); err != nil { + if err := utils.MapUnmarshalValidate(m, cfg.Provider); err != nil { return err } diff --git a/internal/notif/config_test.go b/internal/notif/config_test.go index 465cefb5..1ade43e4 100644 --- a/internal/notif/config_test.go +++ b/internal/notif/config_test.go @@ -150,7 +150,7 @@ func TestNotificationConfig(t *testing.T) { t.Run(tt.name, func(t *testing.T) { var cfg NotificationConfig provider := tt.cfg["provider"] - err := utils.Deserialize(tt.cfg, &cfg) + err := utils.MapUnmarshalValidate(tt.cfg, &cfg) if tt.wantErr { ExpectHasError(t, err) } else { diff --git a/internal/route/provider/docker.go b/internal/route/provider/docker.go index 67c995a0..29d85184 100755 --- a/internal/route/provider/docker.go +++ b/internal/route/provider/docker.go @@ -172,7 +172,7 @@ func (p *DockerProvider) routesFromContainerLabels(container *docker.Container) } // deserialize map into entry object - err := U.Deserialize(entryMap, r) + err := U.MapUnmarshalValidate(entryMap, r) if err != nil { errs.Add(err.Subject(alias)) } else { @@ -181,7 +181,7 @@ func (p *DockerProvider) routesFromContainerLabels(container *docker.Container) } if wildcardProps != nil { for _, re := range routes { - if err := U.Deserialize(wildcardProps, re); err != nil { + if err := U.MapUnmarshalValidate(wildcardProps, re); err != nil { errs.Add(err.Subject(docker.WildcardAlias)) break } diff --git a/internal/route/provider/file.go b/internal/route/provider/file.go index 79855b93..6c23a602 100644 --- a/internal/route/provider/file.go +++ b/internal/route/provider/file.go @@ -34,7 +34,7 @@ func FileProviderImpl(filename string) (ProviderImpl, error) { } func validate(data []byte) (routes route.Routes, err gperr.Error) { - err = utils.DeserializeYAML(data, &routes) + err = utils.UnmarshalValidateYAML(data, &routes) return } diff --git a/internal/route/rules/rules_test.go b/internal/route/rules/rules_test.go index e5bd7af3..aa8e8d16 100644 --- a/internal/route/rules/rules_test.go +++ b/internal/route/rules/rules_test.go @@ -28,7 +28,7 @@ func TestParseRule(t *testing.T) { var rules struct { Rules Rules } - err := utils.Deserialize(utils.SerializedObject{"rules": test}, &rules) + err := utils.MapUnmarshalValidate(utils.SerializedObject{"rules": test}, &rules) ExpectNoError(t, err) ExpectEqual(t, len(rules.Rules), len(test)) ExpectEqual(t, rules.Rules[0].Name, "test") diff --git a/internal/route/types/http_config_test.go b/internal/route/types/http_config_test.go index 52d9d3aa..c6ea8cf8 100644 --- a/internal/route/types/http_config_test.go +++ b/internal/route/types/http_config_test.go @@ -40,7 +40,7 @@ func TestHTTPConfigDeserialize(t *testing.T) { t.Run(tt.name, func(t *testing.T) { cfg := Route{} tt.input["host"] = "internal" - err := utils.Deserialize(tt.input, &cfg) + err := utils.MapUnmarshalValidate(tt.input, &cfg) if err != nil { expect.NoError(t, err) } diff --git a/internal/utils/serialization.go b/internal/utils/serialization.go index cf6e7715..d4491b55 100644 --- a/internal/utils/serialization.go +++ b/internal/utils/serialization.go @@ -170,8 +170,8 @@ func dive(dst reflect.Value) (v reflect.Value, t reflect.Type, err gperr.Error) } } -// Deserialize takes a SerializedObject and a target value, and assigns the values in the SerializedObject to the target value. -// Deserialize ignores case differences between the field names in the SerializedObject and the target. +// MapUnmarshalValidate takes a SerializedObject and a target value, and assigns the values in the SerializedObject to the target value. +// MapUnmarshalValidate ignores case differences between the field names in the SerializedObject and the target. // // The target value must be a struct or a map[string]any. // If the target value is a struct , and implements the MapUnmarshaller interface, @@ -183,11 +183,11 @@ func dive(dst reflect.Value) (v reflect.Value, t reflect.Type, err gperr.Error) // If the target value is a map[string]any the SerializedObject will be deserialized into the map. // // The function returns an error if the target value is not a struct or a map[string]any, or if there is an error during deserialization. -func Deserialize(src SerializedObject, dst any) (err gperr.Error) { - return deserialize(src, dst, true) +func MapUnmarshalValidate(src SerializedObject, dst any) (err gperr.Error) { + return mapUnmarshalValidate(src, dst, true) } -func deserialize(src SerializedObject, dst any, checkValidateTag bool) (err gperr.Error) { +func mapUnmarshalValidate(src SerializedObject, dst any, checkValidateTag bool) (err gperr.Error) { dstV := reflect.ValueOf(dst) dstT := dstV.Type() @@ -378,7 +378,7 @@ func Convert(src reflect.Value, dst reflect.Value, checkValidateTag bool) gperr. if !ok { return ErrUnsupportedConversion.Subject(dstT.String() + " to " + srcT.String()) } - return deserialize(obj, dst.Addr().Interface(), checkValidateTag) + return mapUnmarshalValidate(obj, dst.Addr().Interface(), checkValidateTag) case srcKind == reflect.Slice: if src.Len() == 0 { return nil @@ -500,21 +500,21 @@ func ConvertString(src string, dst reflect.Value) (convertible bool, convErr gpe return true, Convert(reflect.ValueOf(tmp), dst, true) } -func DeserializeYAML[T any](data []byte, target *T) gperr.Error { +func UnmarshalValidateYAML[T any](data []byte, target *T) gperr.Error { m := make(map[string]any) if err := yaml.Unmarshal(data, &m); err != nil { return gperr.Wrap(err) } - return Deserialize(m, target) + return MapUnmarshalValidate(m, target) } -func DeserializeYAMLMap[V any](data []byte) (_ functional.Map[string, V], err gperr.Error) { +func UnmarshalValidateYAMLXSync[V any](data []byte) (_ functional.Map[string, V], err gperr.Error) { m := make(map[string]any) if err = gperr.Wrap(yaml.Unmarshal(data, &m)); err != nil { return } m2 := make(map[string]V, len(m)) - if err = Deserialize(m, m2); err != nil { + if err = MapUnmarshalValidate(m, m2); err != nil { return } return functional.NewMapFrom(m2), nil diff --git a/internal/utils/serialization_test.go b/internal/utils/serialization_test.go index df08211e..9990095e 100644 --- a/internal/utils/serialization_test.go +++ b/internal/utils/serialization_test.go @@ -40,7 +40,7 @@ func TestDeserialize(t *testing.T) { t.Run("deserialize", func(t *testing.T) { var s2 S - err := Deserialize(testStructSerialized, &s2) + err := MapUnmarshalValidate(testStructSerialized, &s2) ExpectNoError(t, err) ExpectEqual(t, s2, testStruct) }) @@ -60,13 +60,13 @@ func TestDeserializeAnonymousField(t *testing.T) { } // all, anon := extractFields(reflect.TypeOf(s2)) // t.Fatalf("anon %v, all %v", anon, all) - err := Deserialize(map[string]any{"a": 1, "b": 2, "c": 3}, &s) + err := MapUnmarshalValidate(map[string]any{"a": 1, "b": 2, "c": 3}, &s) ExpectNoError(t, err) ExpectEqual(t, s.A, 1) ExpectEqual(t, s.B, 2) ExpectEqual(t, s.C, 3) - err = Deserialize(map[string]any{"a": 1, "b": 2, "c": 3}, &s2) + err = MapUnmarshalValidate(map[string]any{"a": 1, "b": 2, "c": 3}, &s2) ExpectNoError(t, err) ExpectEqual(t, s2.A, 1) ExpectEqual(t, s2.B, 2) @@ -148,7 +148,7 @@ func (c *testType) Parse(v string) (err error) { func TestConvertor(t *testing.T) { t.Run("valid", func(t *testing.T) { m := new(testModel) - ExpectNoError(t, Deserialize(map[string]any{"Test": "123"}, m)) + ExpectNoError(t, MapUnmarshalValidate(map[string]any{"Test": "123"}, m)) ExpectEqual(t, m.Test.foo, 123) ExpectEqual(t, m.Test.bar, "123") @@ -156,18 +156,18 @@ func TestConvertor(t *testing.T) { t.Run("int_to_string", func(t *testing.T) { m := new(testModel) - ExpectNoError(t, Deserialize(map[string]any{"Test": "123"}, m)) + ExpectNoError(t, MapUnmarshalValidate(map[string]any{"Test": "123"}, m)) ExpectEqual(t, m.Test.foo, 123) ExpectEqual(t, m.Test.bar, "123") - ExpectNoError(t, Deserialize(map[string]any{"Baz": 123}, m)) + ExpectNoError(t, MapUnmarshalValidate(map[string]any{"Baz": 123}, m)) ExpectEqual(t, m.Baz, "123") }) t.Run("invalid", func(t *testing.T) { m := new(testModel) - ExpectError(t, ErrUnsupportedConversion, Deserialize(map[string]any{"Test": struct{}{}}, m)) + ExpectError(t, ErrUnsupportedConversion, MapUnmarshalValidate(map[string]any{"Test": struct{}{}}, m)) }) }