Source File
funcs.go
Belonging Package
text/template
package template
import (
)
func () map[string]reflect.Value {
builtinFuncsOnce.Do(func() {
builtinFuncsOnce.v = createValueFuncs(builtins())
})
return builtinFuncsOnce.v
}
func ( map[string]reflect.Value, FuncMap) {
for , := range {
if !goodName() {
panic(fmt.Errorf("function name %q is not a valid identifier", ))
}
:= reflect.ValueOf()
if .Kind() != reflect.Func {
panic("value for " + + " not a function")
}
if !goodFunc(.Type()) {
panic(fmt.Errorf("can't install method/function %q with %d results", , .Type().NumOut()))
}
[] =
}
}
func (, FuncMap) {
for , := range {
[] =
}
}
func ( reflect.Value, reflect.Type) (reflect.Value, error) {
if !.IsValid() {
if !canBeNil() {
return reflect.Value{}, fmt.Errorf("value is nil; should be of type %s", )
}
= reflect.Zero()
}
if .Type().AssignableTo() {
return , nil
}
if intLike(.Kind()) && intLike(.Kind()) && .Type().ConvertibleTo() {
= .Convert()
return , nil
}
return reflect.Value{}, fmt.Errorf("value has type %s; should be %s", .Type(), )
}
func ( reflect.Kind) bool {
switch {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return true
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return true
}
return false
}
func ( reflect.Value, int) (int, error) {
var int64
switch .Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
= .Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
= int64(.Uint())
case reflect.Invalid:
return 0, fmt.Errorf("cannot index slice/array with nil")
default:
return 0, fmt.Errorf("cannot index slice/array with type %s", .Type())
}
if < 0 || int() < 0 || int() > {
return 0, fmt.Errorf("index out of range: %d", )
}
return int(), nil
}
func ( reflect.Value, ...reflect.Value) (reflect.Value, error) {
= indirectInterface()
if !.IsValid() {
return reflect.Value{}, fmt.Errorf("index of untyped nil")
}
for , := range {
= indirectInterface()
var bool
if , = indirect(); {
return reflect.Value{}, fmt.Errorf("index of nil pointer")
}
switch .Kind() {
case reflect.Array, reflect.Slice, reflect.String:
, := indexArg(, .Len())
if != nil {
return reflect.Value{},
}
= .Index()
case reflect.Map:
, := prepareArg(, .Type().Key())
if != nil {
return reflect.Value{},
}
if := .MapIndex(); .IsValid() {
=
} else {
= reflect.Zero(.Type().Elem())
}
func ( reflect.Value, ...reflect.Value) (reflect.Value, error) {
= indirectInterface()
if !.IsValid() {
return reflect.Value{}, fmt.Errorf("slice of untyped nil")
}
if len() > 3 {
return reflect.Value{}, fmt.Errorf("too many slice indexes: %d", len())
}
var int
switch .Kind() {
case reflect.String:
if len() == 3 {
return reflect.Value{}, fmt.Errorf("cannot 3-index slice a string")
}
= .Len()
case reflect.Array, reflect.Slice:
= .Cap()
default:
return reflect.Value{}, fmt.Errorf("can't slice item of type %s", .Type())
func ( reflect.Value, ...reflect.Value) (reflect.Value, error) {
= indirectInterface()
if !.IsValid() {
return reflect.Value{}, fmt.Errorf("call of nil")
}
:= .Type()
if .Kind() != reflect.Func {
return reflect.Value{}, fmt.Errorf("non-function of type %s", )
}
if !goodFunc() {
return reflect.Value{}, fmt.Errorf("function called with %d args; should be 1 or 2", .NumOut())
}
:= .NumIn()
var reflect.Type
if .IsVariadic() {
if len() < -1 {
return reflect.Value{}, fmt.Errorf("wrong number of args: got %d want at least %d", len(), -1)
}
= .In( - 1).Elem()
} else {
if len() != {
return reflect.Value{}, fmt.Errorf("wrong number of args: got %d want %d", len(), )
}
}
:= make([]reflect.Value, len())
for , := range {
:=
if !.IsVariadic() || < -1 {
= .In()
}
var error
if [], = prepareArg(, ); != nil {
return reflect.Value{}, fmt.Errorf("arg %d: %s", , )
}
}
return safeCall(, )
}
func ( reflect.Value) bool {
, := isTrue(indirectInterface())
return
}
var (
errBadComparisonType = errors.New("invalid type for comparison")
errBadComparison = errors.New("incompatible types for comparison")
errNoComparison = errors.New("missing argument for comparison")
)
type kind int
const (
invalidKind kind = iota
boolKind
complexKind
intKind
floatKind
stringKind
uintKind
)
func ( reflect.Value) (kind, error) {
switch .Kind() {
case reflect.Bool:
return boolKind, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return intKind, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return uintKind, nil
case reflect.Float32, reflect.Float64:
return floatKind, nil
case reflect.Complex64, reflect.Complex128:
return complexKind, nil
case reflect.String:
return stringKind, nil
}
return invalidKind, errBadComparisonType
}
func ( reflect.Value, ...reflect.Value) (bool, error) {
= indirectInterface()
if != zero {
if := .Type(); !.Comparable() {
return false, fmt.Errorf("uncomparable type %s: %v", , )
}
}
if len() == 0 {
return false, errNoComparison
}
, := basicKind()
for , := range {
= indirectInterface()
, := basicKind()
:= false
switch {
case == intKind && == uintKind:
= .Int() >= 0 && uint64(.Int()) == .Uint()
case == uintKind && == intKind:
= .Int() >= 0 && .Uint() == uint64(.Int())
default:
return false, errBadComparison
}
} else {
switch {
case boolKind:
= .Bool() == .Bool()
case complexKind:
= .Complex() == .Complex()
case floatKind:
= .Float() == .Float()
case intKind:
= .Int() == .Int()
case stringKind:
= .String() == .String()
case uintKind:
= .Uint() == .Uint()
default:
if == zero {
= ==
} else {
if := .Type(); !.Comparable() {
return false, fmt.Errorf("uncomparable type %s: %v", , )
}
= .Interface() == .Interface()
}
}
}
if {
return true, nil
}
}
return false, nil
}
, := eq(, )
return !,
}
switch {
case == intKind && == uintKind:
= .Int() < 0 || uint64(.Int()) < .Uint()
case == uintKind && == intKind:
= .Int() >= 0 && .Uint() < uint64(.Int())
default:
return false, errBadComparison
}
} else {
switch {
case boolKind, complexKind:
return false, errBadComparisonType
case floatKind:
= .Float() < .Float()
case intKind:
= .Int() < .Int()
case stringKind:
= .String() < .String()
case uintKind:
= .Uint() < .Uint()
default:
panic("invalid kind")
}
}
return , nil
}
if !strings.ContainsAny(, "'\"&<>\000") {
return
}
var bytes.Buffer
HTMLEscape(&, []byte())
return .String()
}
func ( ...interface{}) string {
return HTMLEscapeString(evalArgs())
}
continue
}
.Write([:])
func ( ...interface{}) string {
return JSEscapeString(evalArgs())
}
func ( ...interface{}) string {
return url.QueryEscape(evalArgs())
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |