refactor: improved json loading flow and log messages

This commit is contained in:
yusing
2025-03-28 07:00:21 +08:00
parent 8c03c5e82e
commit c0c6e21a16
4 changed files with 25 additions and 24 deletions

View File

@@ -529,13 +529,10 @@ func SaveJSON[T any](path string, src *T, perm os.FileMode) error {
return os.WriteFile(path, data, perm)
}
func LoadJSONIfExist[T any](path string, dst *T) error {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
func LoadJSONIfExist[T any](path string, dst *T) (exists bool, err error) {
err = loadSerialized(path, dst, json.Unmarshal)
if err != nil && os.IsNotExist(err) {
return false, nil
}
return loadSerialized(path, dst, json.Unmarshal)
return true, err
}