config.Load() now sets defaults for optional fields

This commit is contained in:
Adrien Marquès 2018-05-24 16:53:39 +02:00
parent 75db1513bb
commit 73c36a3821
3 changed files with 58 additions and 5 deletions

View File

@ -35,7 +35,7 @@ func (c *Controller) format(controllerName string) error {
/* (3) stop if no parameter */ /* (3) stop if no parameter */
if method.Ptr.Parameters == nil || len(method.Ptr.Parameters) < 1 { if method.Ptr.Parameters == nil || len(method.Ptr.Parameters) < 1 {
method.Ptr.Parameters = make(map[string]MethodParameter, 0) method.Ptr.Parameters = make(map[string]*MethodParameter, 0)
continue continue
} }
@ -111,3 +111,53 @@ func (c *Controller) format(controllerName string) error {
return nil return nil
} }
// setDefaults sets the defaults for optional configuration fields
func (c *Controller) setDefaults() {
/* (1) Get methods */
methods := []*Method{
c.GET,
c.POST,
c.PUT,
c.DELETE,
}
/* (2) Browse methods */
for _, m := range methods {
// ignore if not set
if m == nil {
continue
}
/* (3) Browse parameters */
for name, param := range m.Parameters {
// 1. Default 'opt': required //
if param.Optional == nil {
param.Optional = new(bool)
}
// 2. Default 'rename': same as name
if param.Rename == nil {
param.Rename = &name
}
}
}
/* (4) Stop here if no children */
if c.Children == nil || len(c.Children) < 1 {
return
}
/* (5) Iterate over children */
for _, child := range c.Children {
child.setDefaults()
}
return
}

View File

@ -36,6 +36,9 @@ func Load(path string) (*Controller, error) {
return nil, err return nil, err
} }
/* (5) Set default optional fields */
receiver.setDefaults()
return receiver, nil return receiver, nil
} }

View File

@ -11,10 +11,10 @@ type MethodParameter struct {
Default *interface{} `json:"def"` Default *interface{} `json:"def"`
} }
type Method struct { type Method struct {
Description string `json:"des"` Description string `json:"des"`
Permission [][]string `json:"per"` Permission [][]string `json:"per"`
Parameters map[string]MethodParameter `json:"par"` Parameters map[string]*MethodParameter `json:"par"`
Options map[string]interface{} `json:"opt"` Options map[string]interface{} `json:"opt"`
} }
type Controller struct { type Controller struct {