Skip to content

Commit

Permalink
fix(be): loading env after config refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fiftin committed Sep 29, 2024
1 parent bea1c60 commit 08a059a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions db/bolt/BoltDb.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@ func (d *BoltDb) isObjectInUse(bucketID int, objProps db.ObjectProps, objID obje

func CreateTestStore() *BoltDb {
util.Config = &util.ConfigType{
BoltDb: &util.DbConfig{},
Dialect: "bolt",
}

Expand Down
13 changes: 12 additions & 1 deletion util/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,8 @@ func getConfigValue(path string) string {
for i, nested := range nested_path {
attribute = reflect.Indirect(attribute).FieldByName(nested)
lastDepth := len(nested_path) == i+1
if !lastDepth && attribute.Kind() != reflect.Struct || lastDepth && attribute.Kind() == reflect.Invalid {
if !lastDepth && attribute.Kind() != reflect.Struct && attribute.Kind() != reflect.Pointer ||
lastDepth && attribute.Kind() == reflect.Invalid {
panic(fmt.Errorf("got non-existent config attribute '%v'", path))
}
}
Expand Down Expand Up @@ -516,6 +517,16 @@ func loadEnvironmentToObject(obj interface{}) error {
return err
}
continue
} else if fieldType.Type.Kind() == reflect.Ptr && fieldType.Type.Elem().Kind() == reflect.Struct {
if fieldValue.IsZero() {
newValue := reflect.New(fieldType.Type.Elem())
fieldValue.Set(newValue)
}
err := loadEnvironmentToObject(fieldValue.Interface())
if err != nil {
return err
}
continue
}

envVar := fieldType.Tag.Get("env")
Expand Down
6 changes: 5 additions & 1 deletion util/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ func TestGetConfigValue(t *testing.T) {
Config.CookieHash = testCookieHash
Config.MaxParallelTasks = testMaxParallelTasks
Config.LdapNeedTLS = testLdapNeedTls
Config.BoltDb.Hostname = testDbHost
Config.BoltDb = &DbConfig{
Hostname: testDbHost,
}

if getConfigValue("Port") != testPort {
t.Error("Could not get value for config attribute 'Port'!")
Expand All @@ -144,6 +146,7 @@ func TestGetConfigValue(t *testing.T) {
if getConfigValue("LdapNeedTLS") != fmt.Sprintf("%v", testLdapNeedTls) {
t.Error("Could not get value for config attribute 'LdapNeedTLS'!")
}

if getConfigValue("BoltDb.Hostname") != fmt.Sprintf("%v", testDbHost) {
t.Error("Could not get value for config attribute 'BoltDb.Hostname'!")
}
Expand Down Expand Up @@ -223,6 +226,7 @@ func TestSetConfigValue(t *testing.T) {
func TestLoadConfigEnvironmet(t *testing.T) {

Config = new(ConfigType)
Config.BoltDb = &DbConfig{}
Config.Dialect = DbDriverBolt

var envPort = "1337"
Expand Down

0 comments on commit 08a059a

Please sign in to comment.