예제 #1
0
파일: val.go 프로젝트: xgfone/go-tools
// IsZero return true if the value is the ZERO value, or false.
//
// For bool, false is ZERO. For the integer, 0 is the ZERO. For the complex,
// it is ZEOR if all the real and the imag are 0.0. For chan, func, map, ptr,
// interface, nil is ZERO. For the slice, it is ZERO if the value is nil or has
// no element. For the array, it is ZERO if the value has no element.
// For string, the empty string is ZERO. For struct, it always is false.
func IsZero(v interface{}) bool {
	ok, _ := template.IsTrue(v)
	return !ok

	// _v := reflect.ValueOf(v)
	// switch _v.Kind() {
	// case reflect.Bool:
	// 	return !_v.Bool()
	// case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
	// 	return _v.Int() == 0
	// case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
	// 	return _v.Uint() == 0
	// case reflect.Complex64, reflect.Complex128:
	// 	vv := _v.Complex()
	// 	if real(vv) == 0.0 && imag(vv) == 0.0 {
	// 		return true
	// 	}
	// 	return false
	// case reflect.Chan, reflect.Func, reflect.Map, reflect.Slice:
	// 	return (_v.IsNil() || _v.Len() == 0)
	// case reflect.Ptr:
	// 	return _v.IsNil()
	// case reflect.Interface:
	// 	return _v.IsNil()
	// case reflect.Array, reflect.String:
	// 	return _v.Len() == 0
	// case reflect.Struct:
	// 	return false
	// case reflect.Uintptr:
	// 	return _v.UnsafeAddr() == 0
	// case reflect.Invalid: // We think it as the interface nil
	// 	return true
	// }
	// return false
}
예제 #2
0
	return func(strs []string, str string) string {
		for _, mod := range strs {
			str = a(str, mod)
		}
		return str
	}
}

// TODO write some tests for these

var FuncMap = template.FuncMap{
	"hasPrefix": swapStringsFuncBoolArgsOrder(strings.HasPrefix),
	"hasSuffix": swapStringsFuncBoolArgsOrder(strings.HasSuffix),

	"ternary": func(truthy interface{}, falsey interface{}, val interface{}) interface{} {
		if t, ok := template.IsTrue(val); !ok {
			panic(fmt.Sprintf(`template.IsTrue(%+v) says things are NOT OK`, val))
		} else if t {
			return truthy
		} else {
			return falsey
		}
	},

	"first": thingsActionFactory("first", true, func(args []interface{}, arg interface{}) interface{} { return arg }),
	"last":  thingsActionFactory("last", false, func(args []interface{}, arg interface{}) interface{} { return arg }),

	"json": func(v interface{}) (string, error) {
		j, err := json.Marshal(v)
		return string(j), err
	},
예제 #3
0
파일: template.go 프로젝트: nuuyoo/go
// IsTrue reports whether the value is 'true', in the sense of not the zero of its type,
// and whether the value has a meaningful truth value. This is the definition of
// truth used by if and other such actions.
func IsTrue(val interface{}) (truth, ok bool) {
	return template.IsTrue(val)
}
예제 #4
0
파일: template.go 프로젝트: hanjinze/go-1
// IsTrue reports whether the value is 'true', in the sense of not the zero of its type,
// and whether the value has a meaningful truth value. This is the definition of
// truth used by if and other such actions.
func IsTrue(val reflect.Value) (truth, ok bool) {
	return template.IsTrue(val)
}