2015-02-01 15:51:01 -08:00
|
|
|
package authboss
|
|
|
|
|
2018-02-04 21:24:55 -08:00
|
|
|
// Keys for use in HTMLData that are meaningful
|
|
|
|
const (
|
|
|
|
// DataErr is for one off errors that don't really belong to
|
|
|
|
// a particular field
|
|
|
|
DataErr = "error"
|
|
|
|
// DataValidation is for validation errors
|
|
|
|
DataValidation = "errors"
|
2018-02-25 15:20:57 -08:00
|
|
|
// DataPreserve preserves fields
|
|
|
|
DataPreserve = "preserve"
|
2018-04-30 18:21:56 -07:00
|
|
|
// DataModules contains a map[string]Moduler of which modules are loaded
|
|
|
|
// the Init() method should NEVER be called in a template. This structure
|
|
|
|
// is only returned to avoid allocations.
|
|
|
|
DataModules = "modules"
|
2018-02-04 21:24:55 -08:00
|
|
|
)
|
|
|
|
|
2015-02-19 14:34:29 -08: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
|
|
|
|
// slice, where odd elements are keys, and the following even element is their value.
|
|
|
|
func NewHTMLData(data ...interface{}) HTMLData {
|
|
|
|
if len(data)%2 != 0 {
|
2018-02-25 15:20:57 -08:00
|
|
|
panic("it should be a key value list of arguments.")
|
2015-02-19 14:34:29 -08: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 05:08:11 -08: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
|
2015-02-19 14:34:29 -08:00
|
|
|
// slice, where odd elements are keys, and the following even element is their value.
|
2015-02-20 05:08:11 -08:00
|
|
|
func (h HTMLData) MergeKV(data ...interface{}) HTMLData {
|
2015-02-19 14:34:29 -08: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-19 14:50:14 -08:00
|
|
|
|
|
|
|
h[k] = data[i+1]
|
2015-02-19 14:34:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return h
|
2015-02-01 15:51:01 -08:00
|
|
|
}
|