Use varutils instead
Former-commit-id: 454971910d12915f6668454ab00c0f92f9f80f40 [formerly 5fd29b6b99f57f9cd6be8a5c09151ef3feabbcc9] [formerly ac7480dcb9b5d3c240163f5f406f0ecebc842cee [formerly 2d83da4d7eb6e96fad54290909d6f8acd5d4785c]] Former-commit-id: 77ec28f8a63a9b994059d28f467697cc6f03288b [formerly d031877a753306d0dd42c58686538a9eaf3211fc] Former-commit-id: 818e4d03e50e4a04f1f38001c48dd45e71ed91ef
This commit is contained in:
		
							parent
							
								
									f40731d3b4
								
							
						
					
					
						commit
						75ed779b17
					
				| 
						 | 
				
			
			@ -10,7 +10,7 @@ import (
 | 
			
		|||
 | 
			
		||||
	rice "github.com/GeertJohan/go.rice"
 | 
			
		||||
	"github.com/hacdias/filemanager"
 | 
			
		||||
	"github.com/hacdias/filemanager/variables"
 | 
			
		||||
	"github.com/hacdias/varutils"
 | 
			
		||||
	"github.com/robfig/cron"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -140,7 +140,7 @@ func (h hugo) AfterAPI(c *filemanager.RequestContext, w http.ResponseWriter, r *
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func (h hugo) JavaScript() string {
 | 
			
		||||
	return rice.MustFindBox("./").MustString("hugo.js")
 | 
			
		||||
	return rice.MustFindBox("./assets/").MustString("hugo.js")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// run runs Hugo with the define arguments.
 | 
			
		||||
| 
						 | 
				
			
			@ -151,7 +151,7 @@ func (h hugo) run(force bool) {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	// Prevent running if watching is enabled
 | 
			
		||||
	if b, pos := variables.StringInSlice("--watch", h.Args); b && !force {
 | 
			
		||||
	if b, pos := varutils.StringInSlice("--watch", h.Args); b && !force {
 | 
			
		||||
		if len(h.Args) > pos && h.Args[pos+1] != "false" {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,13 +0,0 @@
 | 
			
		|||
package variables
 | 
			
		||||
 | 
			
		||||
import "reflect"
 | 
			
		||||
 | 
			
		||||
// IsMap checks if some variable is a map
 | 
			
		||||
func IsMap(sth interface{}) bool {
 | 
			
		||||
	return reflect.ValueOf(sth).Kind() == reflect.Map
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// IsSlice checks if some variable is a slice
 | 
			
		||||
func IsSlice(sth interface{}) bool {
 | 
			
		||||
	return reflect.ValueOf(sth).Kind() == reflect.Slice
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,49 +0,0 @@
 | 
			
		|||
package variables
 | 
			
		||||
 | 
			
		||||
import "testing"
 | 
			
		||||
 | 
			
		||||
type interfaceToBool struct {
 | 
			
		||||
	Value  interface{}
 | 
			
		||||
	Result bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var testIsMap = []*interfaceToBool{
 | 
			
		||||
	{"teste", false},
 | 
			
		||||
	{453478, false},
 | 
			
		||||
	{-984512, false},
 | 
			
		||||
	{true, false},
 | 
			
		||||
	{map[string]bool{}, true},
 | 
			
		||||
	{map[int]bool{}, true},
 | 
			
		||||
	{map[interface{}]bool{}, true},
 | 
			
		||||
	{[]string{}, false},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestIsMap(t *testing.T) {
 | 
			
		||||
	for _, test := range testIsMap {
 | 
			
		||||
		if IsMap(test.Value) != test.Result {
 | 
			
		||||
			t.Errorf("Incorrect value on IsMap for %v; want: %v; got: %v", test.Value, test.Result, !test.Result)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var testIsSlice = []*interfaceToBool{
 | 
			
		||||
	{"teste", false},
 | 
			
		||||
	{453478, false},
 | 
			
		||||
	{-984512, false},
 | 
			
		||||
	{true, false},
 | 
			
		||||
	{map[string]bool{}, false},
 | 
			
		||||
	{map[int]bool{}, false},
 | 
			
		||||
	{map[interface{}]bool{}, false},
 | 
			
		||||
	{[]string{}, true},
 | 
			
		||||
	{[]int{}, true},
 | 
			
		||||
	{[]bool{}, true},
 | 
			
		||||
	{[]interface{}{}, true},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestIsSlice(t *testing.T) {
 | 
			
		||||
	for _, test := range testIsSlice {
 | 
			
		||||
		if IsSlice(test.Value) != test.Result {
 | 
			
		||||
			t.Errorf("Incorrect value on IsSlice for %v; want: %v; got: %v", test.Value, test.Result, !test.Result)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,47 +0,0 @@
 | 
			
		|||
package variables
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"log"
 | 
			
		||||
	"reflect"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Dict allows to send more than one variable into a template.
 | 
			
		||||
func Dict(values ...interface{}) (map[string]interface{}, error) {
 | 
			
		||||
	if len(values)%2 != 0 {
 | 
			
		||||
		return nil, errors.New("invalid dict call")
 | 
			
		||||
	}
 | 
			
		||||
	dict := make(map[string]interface{}, len(values)/2)
 | 
			
		||||
	for i := 0; i < len(values); i += 2 {
 | 
			
		||||
		key, ok := values[i].(string)
 | 
			
		||||
		if !ok {
 | 
			
		||||
			return nil, errors.New("dict keys must be strings")
 | 
			
		||||
		}
 | 
			
		||||
		dict[key] = values[i+1]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return dict, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FieldInStruct checks if variable is defined in a struct.
 | 
			
		||||
func FieldInStruct(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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// StringInSlice checks if a slice contains a string.
 | 
			
		||||
func StringInSlice(a string, list []string) (bool, int) {
 | 
			
		||||
	for i, b := range list {
 | 
			
		||||
		if b == a {
 | 
			
		||||
			return true, i
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return false, 0
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,41 +0,0 @@
 | 
			
		|||
package variables
 | 
			
		||||
 | 
			
		||||
import "testing"
 | 
			
		||||
 | 
			
		||||
type testFieldInStructData struct {
 | 
			
		||||
	f1 string
 | 
			
		||||
	f2 bool
 | 
			
		||||
	f3 int
 | 
			
		||||
	f4 func()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type testFieldInStruct struct {
 | 
			
		||||
	data   interface{}
 | 
			
		||||
	field  string
 | 
			
		||||
	result bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var testFieldInStructCases = []testFieldInStruct{
 | 
			
		||||
	{testFieldInStructData{}, "f1", true},
 | 
			
		||||
	{testFieldInStructData{}, "f2", true},
 | 
			
		||||
	{testFieldInStructData{}, "f3", true},
 | 
			
		||||
	{testFieldInStructData{}, "f4", true},
 | 
			
		||||
	{testFieldInStructData{}, "f5", false},
 | 
			
		||||
	{[]string{}, "", false},
 | 
			
		||||
	{map[string]int{"oi": 4}, "", false},
 | 
			
		||||
	{"asa", "", false},
 | 
			
		||||
	{"int", "", false},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestFieldInStruct(t *testing.T) {
 | 
			
		||||
	for _, pair := range testFieldInStructCases {
 | 
			
		||||
		v := FieldInStruct(pair.data, pair.field)
 | 
			
		||||
		if v != pair.result {
 | 
			
		||||
			t.Error(
 | 
			
		||||
				"For", pair.data,
 | 
			
		||||
				"expected", pair.result,
 | 
			
		||||
				"got", v,
 | 
			
		||||
			)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue