File tree Expand file tree Collapse file tree 2 files changed +67
-2
lines changed Expand file tree Collapse file tree 2 files changed +67
-2
lines changed Original file line number Diff line number Diff line change 4
4
5
5
package setting
6
6
7
+ import "reflect"
8
+
7
9
// GetCronSettings maps the cron subsection to the provided config
8
10
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
11
29
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments