Skip to content

Commit

Permalink
fix: update code
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Jan 14, 2025
1 parent 65b8e71 commit 574f9af
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
26 changes: 12 additions & 14 deletions pkg/configutil/once.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ import (
// If previous config file does not exist, it reads the config from file
// or return a default config if not found.
// The returned config is only suitable for read only scenarios for short-lived processes.
func LoadConfigOnce() (*config.Config, error) {
return sync.OnceValues(func() (*config.Config, error) {
configInfo, err := config.LoadConfig()
if err != nil {
return nil, err
}
// set default value
configInfo.SignatureFormat = strings.ToLower(configInfo.SignatureFormat)
if configInfo.SignatureFormat == "" {
configInfo.SignatureFormat = envelope.JWS
}
return configInfo, nil
})()
}
var LoadConfigOnce = sync.OnceValues(func() (*config.Config, error) {
configInfo, err := config.LoadConfig()
if err != nil {
return nil, err
}
// set default value
configInfo.SignatureFormat = strings.ToLower(configInfo.SignatureFormat)
if configInfo.SignatureFormat == "" {
configInfo.SignatureFormat = envelope.JWS
}
return configInfo, nil
})
11 changes: 5 additions & 6 deletions pkg/configutil/once_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package configutil
import (
"os"
"path/filepath"
"reflect"
"strings"
"testing"

Expand All @@ -32,8 +31,8 @@ func TestLoadConfigOnce(t *testing.T) {
if err != nil {
t.Fatal("LoadConfigOnce failed.")
}
if !reflect.DeepEqual(config1, config2) {
t.Fatal("Configs differ in content.")
if config1 != config2 {
t.Fatal("LoadConfigOnce should return the same config.")
}
}

Expand All @@ -50,8 +49,8 @@ func TestLoadConfigOnceError(t *testing.T) {
if err == nil || !strings.Contains(err.Error(), "invalid character") {
t.Fatal("LoadConfigOnce should fail.")
}
_, err = LoadConfigOnce()
if err == nil || !strings.Contains(err.Error(), "invalid character") {
t.Fatal("LoadConfigOnce should fail.")
_, err2 := LoadConfigOnce()
if err != err2 {
t.Fatal("LoadConfigOnce should return the same error.")
}
}

0 comments on commit 574f9af

Please sign in to comment.