2015-02-02 01:51:01 +02:00
|
|
|
package authboss
|
|
|
|
|
2018-05-09 05:39:39 +02:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2018-02-05 07:24:55 +02:00
|
|
|
// Keys for use in HTMLData that are meaningful
|
|
|
|
const (
|
|
|
|
// DataErr is for one off errors that don't really belong to
|
2018-05-12 18:23:52 +02:00
|
|
|
// a particular field. It should be a string.
|
2018-02-05 07:24:55 +02:00
|
|
|
DataErr = "error"
|
2018-05-12 18:23:52 +02:00
|
|
|
// DataValidation is for validation errors, it should always
|
|
|
|
// have been created using the Map() style functions in the
|
|
|
|
// validation method so that html/text template users don't
|
|
|
|
// struggle in displaying them.
|
|
|
|
//
|
|
|
|
// It is: map[string][]string, where the key in the map is the field
|
|
|
|
// and the []string on the other side is the list of problems
|
|
|
|
// with that field.
|
|
|
|
//
|
|
|
|
// It's also important to note that if the errors that were Map()'d
|
|
|
|
// did not implement FieldError or for generic errors
|
|
|
|
// the empty string ("") is used as a key in the map for those
|
|
|
|
// errors that couldn't be fit to a specific field.
|
2018-02-05 07:24:55 +02:00
|
|
|
DataValidation = "errors"
|
2018-05-12 18:23:52 +02:00
|
|
|
// DataPreserve preserves fields during large form exercises
|
|
|
|
// like user registration so we don't have to re-type safe
|
|
|
|
// information like addresses etc.
|
|
|
|
//
|
|
|
|
// This data looks like map[string]string, and is simply
|
|
|
|
// keyed by the field name, and the value is the field value.
|
2018-02-26 01:20:57 +02:00
|
|
|
DataPreserve = "preserve"
|
2018-05-07 21:33:02 +02:00
|
|
|
// DataModules contains a map[string]bool of which modules are loaded
|
2018-09-16 00:39:26 +02:00
|
|
|
// The bool is largely extraneous and can be ignored, if the module is
|
|
|
|
// loaded it will be present in the map, if not it will be missing.
|
2018-05-01 03:21:56 +02:00
|
|
|
DataModules = "modules"
|
2018-02-05 07:24:55 +02:00
|
|
|
)
|
|
|
|
|
2015-02-20 00:34:29 +02:00
|
|
|
// HTMLData is used to render templates with.
|
|
|
|
type HTMLData map[string]interface{}
|
|
|
|
|
|
|
|
// NewHTMLData creates HTMLData from key-value pairs. The input is a key-value
|
2018-09-16 00:39:26 +02:00
|
|
|
// slice, where odd elements are keys, and the following even element
|
|
|
|
// is their value.
|
2015-02-20 00:34:29 +02:00
|
|
|
func NewHTMLData(data ...interface{}) HTMLData {
|
|
|
|
if len(data)%2 != 0 {
|
2018-02-26 01:20:57 +02:00
|
|
|
panic("it should be a key value list of arguments.")
|
2015-02-20 00:34:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
h := make(HTMLData)
|
|
|
|
|
|
|
|
for i := 0; i < len(data)-1; i += 2 {
|
|
|
|
k, ok := data[i].(string)
|
|
|
|
if !ok {
|
|
|
|
panic("Keys must be strings.")
|
|
|
|
}
|
|
|
|
|
|
|
|
h[k] = data[i+1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2015-02-20 15:08:11 +02:00
|
|
|
// Merge adds the data from other to h. If there are conflicting keys
|
|
|
|
// they are overwritten by other's values.
|
|
|
|
func (h HTMLData) Merge(other HTMLData) HTMLData {
|
|
|
|
for k, v := range other {
|
|
|
|
h[k] = v
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// MergeKV adds extra key-values to the HTMLData. The input is a key-value
|
2018-09-16 00:39:26 +02:00
|
|
|
// slice, where odd elements are keys, and the following even element
|
|
|
|
// is their value.
|
2015-02-20 15:08:11 +02:00
|
|
|
func (h HTMLData) MergeKV(data ...interface{}) HTMLData {
|
2015-02-20 00:34:29 +02:00
|
|
|
if len(data)%2 != 0 {
|
|
|
|
panic("It should be a key value list of arguments.")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(data)-1; i += 2 {
|
|
|
|
k, ok := data[i].(string)
|
|
|
|
if !ok {
|
|
|
|
panic("Keys must be strings.")
|
|
|
|
}
|
2015-02-20 00:50:14 +02:00
|
|
|
|
|
|
|
h[k] = data[i+1]
|
2015-02-20 00:34:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return h
|
2015-02-02 01:51:01 +02:00
|
|
|
}
|
2018-05-09 05:39:39 +02:00
|
|
|
|
|
|
|
// MergeDataInRequest edits the request pointer to point to a new request with
|
|
|
|
// a modified context that contains the merged data.
|
|
|
|
func MergeDataInRequest(r **http.Request, other HTMLData) {
|
|
|
|
ctx := (*r).Context()
|
|
|
|
currentIntf := ctx.Value(CTXKeyData)
|
|
|
|
if currentIntf == nil {
|
|
|
|
*r = (*r).WithContext(context.WithValue(ctx, CTXKeyData, other))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
current := currentIntf.(HTMLData)
|
|
|
|
merged := current.Merge(other)
|
|
|
|
*r = (*r).WithContext(context.WithValue(ctx, CTXKeyData, merged))
|
|
|
|
}
|