From f60cba5b8704c77a96e0ee06ab5e74c0a392bfb2 Mon Sep 17 00:00:00 2001 From: Tristan Cartledge Date: Sat, 18 Feb 2023 21:25:15 +0000 Subject: [PATCH] fix: make upgrade function optional --- io.go | 15 +++++++++++++-- io_test.go | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/io.go b/io.go index 01f136b..0777511 100644 --- a/io.go +++ b/io.go @@ -23,6 +23,7 @@ type ( type options struct { readFileFunc ReadFileFunc writeFileFunc WriteFileFunc + UpgradeFunc UpgradeFunc getLanguageDefaultFunc GetLanguageDefaultFunc } @@ -33,13 +34,19 @@ func WithFileSystemFuncs(rf ReadFileFunc, wf WriteFileFunc) Option { } } +func WithUpgradeFunc(f UpgradeFunc) Option { + return func(o *options) { + o.UpgradeFunc = f + } +} + func WithLanguageDefaultFunc(f GetLanguageDefaultFunc) Option { return func(o *options) { o.getLanguageDefaultFunc = f } } -func Load(dir string, lang string, uf UpgradeFunc, opts ...Option) (*Config, error) { +func Load(dir string, lang string, opts ...Option) (*Config, error) { o := &options{ readFileFunc: os.ReadFile, writeFileFunc: os.WriteFile, @@ -84,8 +91,12 @@ func Load(dir string, lang string, uf UpgradeFunc, opts ...Option) (*Config, err } if version != Version { + if o.UpgradeFunc == nil { + return nil, fmt.Errorf("config is version %s but upgrades not available", version) + } + // Upgrade config file if version is different and write it - cfgMap, err = upgrade(version, cfgMap, uf) + cfgMap, err = upgrade(version, cfgMap, o.UpgradeFunc) if err != nil { return nil, err } diff --git a/io_test.go b/io_test.go index 2bdeda7..27715c7 100644 --- a/io_test.go +++ b/io_test.go @@ -141,7 +141,7 @@ func TestLoad_Success(t *testing.T) { require.NoError(t, err) defer os.RemoveAll(dir) - cfg, err := Load(filepath.Join(os.TempDir(), testDir), "go", testUpdateLang) + cfg, err := Load(filepath.Join(os.TempDir(), testDir), "go", WithUpgradeFunc(testUpdateLang)) assert.NoError(t, err) assert.Equal(t, tt.want, cfg) _, err = os.Stat(filepath.Join(dir, "gen.yaml"))