Esempio n. 1
0
// Map - Gets option map value.
func (opts *middlewareOpts) Map(ctx context.Context, desc *options.Option) (res template.Context, err error) {
	res = make(template.Context)
	if temp, ok := opts.template[desc.Name]; ok {
		tempctx, ok := components.TemplateContext(ctx)
		if !ok {
			return nil, errors.New("No template context set")
		}
		v, err := temp.Execute(tempctx)
		if err != nil {
			return nil, err
		}
		res = helpers.WithDefaultsMap(res, v)
	}
	if node, ok := opts.context[desc.Name]; ok {
		opt := node.Value(ctx)
		if value, ok := opt.(template.Context); ok {
			res = helpers.WithDefaultsMap(res, value)
		} else if opt != nil {
			return nil, fmt.Errorf("invalid context value for option %q: %#v", desc.Name, opt)
		}
	}
	opt := opts.options[desc.Name]
	if options, ok := opt.(template.Context); ok {
		res = helpers.WithDefaultsMap(res, options)
	} else if opt != nil {
		return nil, fmt.Errorf("invalid option value for option %q: %#v", desc.Name, opt)
	}
	res = helpers.WithDefaultsMap(res, desc.Default)
	return
}
Esempio n. 2
0
// SetDefaults - Set options defaults.
func (middleware *Middleware) SetDefaults(other *Middleware) {
	middleware.Options = helpers.WithDefaultsMap(middleware.Options, other.Options)
	middleware.Context = helpers.WithDefaultsMap(middleware.Context, other.Context)
	middleware.Template = helpers.WithDefaultsMap(middleware.Template, other.Template)
}