From 73c36a382184835eafc4927e97e58f11b912805a Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 24 May 2018 16:53:39 +0200 Subject: [PATCH] config.Load() now sets defaults for optional fields --- internal/config/private.go | 52 +++++++++++++++++++++++++++++++++++++- internal/config/public.go | 3 +++ internal/config/types.go | 8 +++--- 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/internal/config/private.go b/internal/config/private.go index 5c58a91..40e1bef 100644 --- a/internal/config/private.go +++ b/internal/config/private.go @@ -35,7 +35,7 @@ func (c *Controller) format(controllerName string) error { /* (3) stop if no parameter */ 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 } @@ -111,3 +111,53 @@ func (c *Controller) format(controllerName string) error { 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 + +} diff --git a/internal/config/public.go b/internal/config/public.go index 2d34e14..9388960 100644 --- a/internal/config/public.go +++ b/internal/config/public.go @@ -36,6 +36,9 @@ func Load(path string) (*Controller, error) { return nil, err } + /* (5) Set default optional fields */ + receiver.setDefaults() + return receiver, nil } diff --git a/internal/config/types.go b/internal/config/types.go index 524d477..cd94b77 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -11,10 +11,10 @@ type MethodParameter struct { Default *interface{} `json:"def"` } type Method struct { - Description string `json:"des"` - Permission [][]string `json:"per"` - Parameters map[string]MethodParameter `json:"par"` - Options map[string]interface{} `json:"opt"` + Description string `json:"des"` + Permission [][]string `json:"per"` + Parameters map[string]*MethodParameter `json:"par"` + Options map[string]interface{} `json:"opt"` } type Controller struct {