refactor(serialization): generalize unmarshal/load functions with pluggable format handlers

Replace YAML-specific functions with generic ones accepting unmarshaler/marshaler
function parameters. This enables future support for JSON and other formats
while maintaining current YAML behavior.

- UnmarshalValidateYAML -> UnmarshalValidate(unmarshalFunc)
- UnmarshalValidateYAMLXSync -> UnmarshalValidateXSync(unmarshalFunc)
- SaveJSON -> SaveFile(marshalFunc)
- LoadJSONIfExist -> LoadFileIfExist(unmarshalFunc)
- Add UnmarshalValidateReader for reader-based decoding

Testing: all 12 staged test files updated to use new API
This commit is contained in:
yusing
2026-01-29 11:57:32 +08:00
parent 32971b45c3
commit a75441aa8a
12 changed files with 119 additions and 58 deletions

View File

@@ -6,6 +6,7 @@ import (
"strconv"
"testing"
"github.com/goccy/go-yaml"
"github.com/stretchr/testify/require"
expect "github.com/yusing/goutils/testing"
)
@@ -303,6 +304,6 @@ autocert:
} `yaml:"options"`
} `yaml:"autocert"`
}
require.NoError(t, UnmarshalValidateYAML(data, &cfg))
require.NoError(t, UnmarshalValidate(data, &cfg, yaml.Unmarshal))
require.Equal(t, "test", cfg.Autocert.Options.AuthToken)
}