Skip to content

Commit f1ab1c5

Browse files
authored
Allow extended config on cron settings (#12939)
* Allow extended config on cron settings Fix #12934 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 3c36080 commit f1ab1c5

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

modules/setting/cron.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,26 @@
44

55
package setting
66

7+
import "reflect"
8+
79
// GetCronSettings maps the cron subsection to the provided config
810
func GetCronSettings(name string, config interface{}) (interface{}, error) {
9-
err := Cfg.Section("cron." + name).MapTo(config)
10-
return config, err
11+
if err := Cfg.Section("cron." + name).MapTo(config); err != nil {
12+
return config, err
13+
}
14+
15+
typ := reflect.TypeOf(config).Elem()
16+
val := reflect.ValueOf(config).Elem()
17+
18+
for i := 0; i < typ.NumField(); i++ {
19+
field := val.Field(i)
20+
tpField := typ.Field(i)
21+
if tpField.Type.Kind() == reflect.Struct && tpField.Anonymous {
22+
if err := Cfg.Section("cron." + name).MapTo(field.Addr().Interface()); err != nil {
23+
return config, err
24+
}
25+
}
26+
}
27+
28+
return config, nil
1129
}

modules/setting/cron_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package setting
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
ini "gopkg.in/ini.v1"
12+
)
13+
14+
func Test_GetCronSettings(t *testing.T) {
15+
16+
type BaseStruct struct {
17+
Base bool
18+
Second string
19+
}
20+
21+
type Extended struct {
22+
BaseStruct
23+
Extend bool
24+
}
25+
26+
iniStr := `
27+
[cron.test]
28+
Base = true
29+
Second = white rabbit
30+
Extend = true
31+
`
32+
Cfg, _ = ini.Load([]byte(iniStr))
33+
34+
extended := &Extended{
35+
BaseStruct: BaseStruct{
36+
Second: "queen of hearts",
37+
},
38+
}
39+
40+
_, err := GetCronSettings("test", extended)
41+
42+
assert.NoError(t, err)
43+
assert.True(t, extended.Base)
44+
assert.EqualValues(t, extended.Second, "white rabbit")
45+
assert.True(t, extended.Extend)
46+
47+
}

0 commit comments

Comments
 (0)