mirror of
https://github.com/gohugoio/hugo.git
synced 2025-01-31 20:44:34 +00:00
Refactor tpl codes
- Use indirect function instead of the same code copies - Fix missing arguments of some test codes
This commit is contained in:
parent
fa8ac87d5e
commit
975f4cf126
2 changed files with 13 additions and 26 deletions
|
@ -317,14 +317,9 @@ func First(limit interface{}, seq interface{}) (interface{}, error) {
|
|||
}
|
||||
|
||||
seqv := reflect.ValueOf(seq)
|
||||
// this is better than my first pass; ripped from text/template/exec.go indirect():
|
||||
for ; seqv.Kind() == reflect.Ptr || seqv.Kind() == reflect.Interface; seqv = seqv.Elem() {
|
||||
if seqv.IsNil() {
|
||||
return nil, errors.New("can't iterate over a nil value")
|
||||
}
|
||||
if seqv.Kind() == reflect.Interface && seqv.NumMethod() > 0 {
|
||||
break
|
||||
}
|
||||
seqv, isNil := indirect(seqv)
|
||||
if isNil {
|
||||
return nil, errors.New("can't iterate over a nil value")
|
||||
}
|
||||
|
||||
switch seqv.Kind() {
|
||||
|
@ -477,13 +472,9 @@ func Delimit(seq, delimiter interface{}, last ...interface{}) (template.HTML, er
|
|||
}
|
||||
|
||||
seqv := reflect.ValueOf(seq)
|
||||
for ; seqv.Kind() == reflect.Ptr || seqv.Kind() == reflect.Interface; seqv = seqv.Elem() {
|
||||
if seqv.IsNil() {
|
||||
return "", errors.New("can't iterate over a nil value")
|
||||
}
|
||||
if seqv.Kind() == reflect.Interface && seqv.NumMethod() > 0 {
|
||||
break
|
||||
}
|
||||
seqv, isNil := indirect(seqv)
|
||||
if isNil {
|
||||
return "", errors.New("can't iterate over a nil value")
|
||||
}
|
||||
|
||||
var str string
|
||||
|
@ -521,13 +512,9 @@ func Delimit(seq, delimiter interface{}, last ...interface{}) (template.HTML, er
|
|||
|
||||
func Sort(seq interface{}, args ...interface{}) ([]interface{}, error) {
|
||||
seqv := reflect.ValueOf(seq)
|
||||
for ; seqv.Kind() == reflect.Ptr || seqv.Kind() == reflect.Interface; seqv = seqv.Elem() {
|
||||
if seqv.IsNil() {
|
||||
return nil, errors.New("can't iterate over a nil value")
|
||||
}
|
||||
if seqv.Kind() == reflect.Interface && seqv.NumMethod() > 0 {
|
||||
break
|
||||
}
|
||||
seqv, isNil := indirect(seqv)
|
||||
if isNil {
|
||||
return nil, errors.New("can't iterate over a nil value")
|
||||
}
|
||||
|
||||
// Create a list of pairs that will be used to do the sort
|
||||
|
|
|
@ -112,7 +112,7 @@ func TestDoArithmetic(t *testing.T) {
|
|||
result, err := doArithmetic(this.a, this.b, this.op)
|
||||
if b, ok := this.expect.(bool); ok && !b {
|
||||
if err == nil {
|
||||
t.Errorf("[%d] doArithmetic didn't return an expected error")
|
||||
t.Errorf("[%d] doArithmetic didn't return an expected error", i)
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
|
@ -147,7 +147,7 @@ func TestMod(t *testing.T) {
|
|||
result, err := Mod(this.a, this.b)
|
||||
if b, ok := this.expect.(bool); ok && !b {
|
||||
if err == nil {
|
||||
t.Errorf("[%d] modulo didn't return an expected error")
|
||||
t.Errorf("[%d] modulo didn't return an expected error", i)
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
|
@ -187,7 +187,7 @@ func TestModBool(t *testing.T) {
|
|||
result, err := ModBool(this.a, this.b)
|
||||
if this.expect == nil {
|
||||
if err == nil {
|
||||
t.Errorf("[%d] modulo didn't return an expected error")
|
||||
t.Errorf("[%d] modulo didn't return an expected error", i)
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
|
@ -218,7 +218,7 @@ func TestFirst(t *testing.T) {
|
|||
results, err := First(this.count, this.sequence)
|
||||
if b, ok := this.expect.(bool); ok && !b {
|
||||
if err == nil {
|
||||
t.Errorf("[%d] First didn't return an expected error")
|
||||
t.Errorf("[%d] First didn't return an expected error", i)
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in a new issue