Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: user define config #1640

Merged
merged 8 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
format fix
  • Loading branch information
A-Wanderer committed Dec 6, 2021
commit 3ed01b7a9c138c3397e57ec71aae6e67694e3957
24 changes: 16 additions & 8 deletions config/user_define_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package config

import(
import (
"github.com/creasty/defaults"
)

Expand All @@ -35,24 +35,32 @@ func (*CustomConfig) Prefix() string {
return constant.CustomConfigPrefix
}

func (udc *CustomConfig) Init() error {
return udc.check()
func (c *CustomConfig) Init() error {
return c.check()
}

func (udc *CustomConfig) check() error {
if err := defaults.Set(udc); err != nil {
func (c *CustomConfig) check() error {
if err := defaults.Set(c); err != nil {
return err
}
return verify(udc)
return verify(c)
}

func (udc *CustomConfig) GetDefineValue(key string, default_value interface{}) interface{} {
if define_value, ok := udc.DefineConfig[key]; ok {
func (c *CustomConfig) GetDefineValue(key string, default_value interface{}) interface{} {
if define_value, ok := c.DefineConfig[key]; ok {
return define_value
}
return default_value
}

func GetDefineValue(key string, default_value interface{}) interface{} {
rt := GetRootConfig()
if rt.Custom == nil {
return default_value
}
return rt.Custom.GetDefineValue(key, default_value)
}

type CustomConfigBuilder struct {
customConfig *CustomConfig
}
Expand Down
3 changes: 3 additions & 0 deletions config/user_define_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestCustomInit(t *testing.T) {
assert.Equal(t, CustomConfig.Version, "v1.0")
assert.Equal(t, CustomConfig.DefineConfig, map[string]interface{}(nil))
assert.Equal(t, CustomConfig.GetDefineValue("test", "test"), "test")
assert.Equal(t, GetDefineValue("test", "test"), "test")
})

t.Run("use config", func(t *testing.T) {
Expand All @@ -49,6 +50,8 @@ func TestCustomInit(t *testing.T) {
assert.Equal(t, CustomConfig.DefineConfig, map[string]interface{}{"test-config": true})
assert.Equal(t, CustomConfig.GetDefineValue("test-config", false), true)
assert.Equal(t, CustomConfig.GetDefineValue("test-no-config", false), false)
assert.Equal(t, GetDefineValue("test-config", false), true)
assert.Equal(t, GetDefineValue("test-no-config", false), false)
})

t.Run("config builder", func(t *testing.T) {
Expand Down