Пример #1
0
func (opts *middlewareOpts) getValue(ctx context.Context, desc *options.Option) (_ interface{}, _ error) {
	node, ok := opts.context[desc.Name]
	if ok {
		if v := node.Value(ctx); v != nil {
			return v, nil
		}
	}
	template, ok := opts.template[desc.Name]
	if ok {
		tempctx, _ := components.TemplateContext(ctx)
		res, err := template.Execute(tempctx)
		if err != nil {
			return nil, err
		}
		v, _ := helpers.CleanDeep(res)
		return v, nil
	}
	if v, ok := opts.options[desc.Name]; ok {
		return v, nil
	}
	if desc.DefKey != nil {
		return ctx.Value(desc.DefKey), nil
	}
	return
}
Пример #2
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
}
Пример #3
0
// RenderInContext - Renders compiled component from context.
// Stores result in context to be retrieved with `components.ContextRendered`.
func RenderInContext(next xhandler.HandlerC) xhandler.HandlerC {
	return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
		c, ok := components.CompiledFromContext(ctx)
		if !ok {
			helpers.WriteError(w, r, http.StatusBadRequest, "component not compiled")
			return
		}
		t, _ := components.TemplateContext(ctx)
		res, err := components.Render(c, t)
		if err != nil {
			helpers.WriteError(w, r, http.StatusExpectationFailed, fmt.Sprintf("render error: %v", err))
			return
		}
		ctx = components.NewRenderedContext(ctx, res)
		next.ServeHTTPC(ctx, w, r)
	})
}
Пример #4
0
// List - Gets option list value.
func (opts *middlewareOpts) List(ctx context.Context, desc *options.Option) (res []interface{}, err error) {
	if temp, ok := opts.template[desc.Name]; ok {
		tempctx, _ := components.TemplateContext(ctx)
		v, err := temp.Execute(tempctx)
		if err != nil {
			return nil, err
		}
		if values, ok := v.([]interface{}); ok {
			res = append(res, values...)
		}
	}
	if node, ok := opts.context[desc.Name]; ok {
		if value, ok := node.Value(ctx).([]interface{}); ok {
			res = append(res, value...)
		}
	}
	if options, ok := opts.options[desc.Name].([]interface{}); ok {
		res = append(res, options...)
	}
	if defaults, ok := desc.Default.([]interface{}); ok {
		res = append(res, defaults...)
	}
	return
}