more updates and renaming

Former-commit-id: 8c194821906b2170df92672fe9dfb9d710640659
This commit is contained in:
Henrique Dias
2017-06-19 17:10:03 +01:00
parent ae61d5b6aa
commit 4829870890
25 changed files with 8783 additions and 546 deletions

47
page/functions.go Normal file
View File

@@ -0,0 +1,47 @@
package page
import (
"encoding/base64"
"encoding/json"
"html/template"
"log"
"reflect"
)
// Create the functions map, then the template, check for erros and
// execute the template if there aren't errors
var functionMap = template.FuncMap{
"Defined": defined,
"CSS": css,
"Marshal": marshal,
"EncodeBase64": encodeBase64,
}
// defined checks if variable is defined in a struct
func defined(data interface{}, field string) bool {
t := reflect.Indirect(reflect.ValueOf(data)).Type()
if t.Kind() != reflect.Struct {
log.Print("Non-struct type not allowed.")
return false
}
_, b := t.FieldByName(field)
return b
}
// css returns the sanitized and safe css
func css(s string) template.CSS {
return template.CSS(s)
}
// marshal converts an interface to json and sanitizes it
func marshal(v interface{}) template.JS {
a, _ := json.Marshal(v)
return template.JS(a)
}
// encodeBase64 encodes a string in base 64
func encodeBase64(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
}

41
page/functions_test.go Normal file
View File

@@ -0,0 +1,41 @@
package page
import "testing"
type testDefinedData struct {
f1 string
f2 bool
f3 int
f4 func()
}
type testDefined struct {
data interface{}
field string
result bool
}
var testDefinedCases = []testDefined{
{testDefinedData{}, "f1", true},
{testDefinedData{}, "f2", true},
{testDefinedData{}, "f3", true},
{testDefinedData{}, "f4", true},
{testDefinedData{}, "f5", false},
{[]string{}, "", false},
{map[string]int{"oi": 4}, "", false},
{"asa", "", false},
{"int", "", false},
}
func TestDefined(t *testing.T) {
for _, pair := range testDefinedCases {
v := defined(pair.data, pair.field)
if v != pair.result {
t.Error(
"For", pair.data,
"expected", pair.result,
"got", v,
)
}
}
}

View File

@@ -3,7 +3,6 @@ package page
import (
"bytes"
"encoding/base64"
"encoding/json"
"html/template"
"log"
@@ -12,7 +11,6 @@ import (
"github.com/hacdias/filemanager"
"github.com/hacdias/filemanager/assets"
"github.com/hacdias/filemanager/utils"
)
// Page contains the informations and functions needed to show the Page
@@ -31,7 +29,6 @@ type Info struct {
Data interface{}
Editor bool
Display string
Token string
}
// BreadcrumbMapItem ...
@@ -94,21 +91,6 @@ func (i Info) PreviousLink() string {
// PrintAsHTML formats the page in HTML and executes the template
func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, error) {
// Create the functions map, then the template, check for erros and
// execute the template if there aren't errors
functions := template.FuncMap{
"Defined": utils.Defined,
"CSS": func(s string) template.CSS {
return template.CSS(s)
},
"Marshal": func(v interface{}) template.JS {
a, _ := json.Marshal(v)
return template.JS(a)
},
"EncodeBase64": func(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
},
}
if p.Minimal {
templates = append(templates, "minimal")
@@ -132,7 +114,7 @@ func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, erro
// If it's the first iteration, creates a new template and add the
// functions map
if i == 0 {
tpl, err = template.New(t).Funcs(functions).Parse(string(Page))
tpl, err = template.New(t).Funcs(functionMap).Parse(string(Page))
} else {
tpl, err = tpl.Parse(string(Page))
}