exec.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //This package is copied from Go library text/template.
  2. //The original private functions indirect and printableValue
  3. //are exported as public functions.
  4. package template
  5. import (
  6. "fmt"
  7. "reflect"
  8. )
  9. var Indirect = indirect
  10. var PrintableValue = printableValue
  11. var (
  12. errorType = reflect.TypeOf((*error)(nil)).Elem()
  13. fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
  14. )
  15. // indirect returns the item at the end of indirection, and a bool to indicate if it's nil.
  16. // We indirect through pointers and empty interfaces (only) because
  17. // non-empty interfaces have methods we might need.
  18. func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
  19. for ; v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface; v = v.Elem() {
  20. if v.IsNil() {
  21. return v, true
  22. }
  23. if v.Kind() == reflect.Interface && v.NumMethod() > 0 {
  24. break
  25. }
  26. }
  27. return v, false
  28. }
  29. // printableValue returns the, possibly indirected, interface value inside v that
  30. // is best for a call to formatted printer.
  31. func printableValue(v reflect.Value) (interface{}, bool) {
  32. if v.Kind() == reflect.Ptr {
  33. v, _ = indirect(v) // fmt.Fprint handles nil.
  34. }
  35. if !v.IsValid() {
  36. return "<no value>", true
  37. }
  38. if !v.Type().Implements(errorType) && !v.Type().Implements(fmtStringerType) {
  39. if v.CanAddr() && (reflect.PtrTo(v.Type()).Implements(errorType) || reflect.PtrTo(v.Type()).Implements(fmtStringerType)) {
  40. v = v.Addr()
  41. } else {
  42. switch v.Kind() {
  43. case reflect.Chan, reflect.Func:
  44. return nil, false
  45. }
  46. }
  47. }
  48. return v.Interface(), true
  49. }
  50. // canBeNil reports whether an untyped nil can be assigned to the type. See reflect.Zero.
  51. func canBeNil(typ reflect.Type) bool {
  52. switch typ.Kind() {
  53. case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
  54. return true
  55. }
  56. return false
  57. }
  58. // isTrue reports whether the value is 'true', in the sense of not the zero of its type,
  59. // and whether the value has a meaningful truth value.
  60. func isTrue(val reflect.Value) (truth, ok bool) {
  61. if !val.IsValid() {
  62. // Something like var x interface{}, never set. It's a form of nil.
  63. return false, true
  64. }
  65. switch val.Kind() {
  66. case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
  67. truth = val.Len() > 0
  68. case reflect.Bool:
  69. truth = val.Bool()
  70. case reflect.Complex64, reflect.Complex128:
  71. truth = val.Complex() != 0
  72. case reflect.Chan, reflect.Func, reflect.Ptr, reflect.Interface:
  73. truth = !val.IsNil()
  74. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  75. truth = val.Int() != 0
  76. case reflect.Float32, reflect.Float64:
  77. truth = val.Float() != 0
  78. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  79. truth = val.Uint() != 0
  80. case reflect.Struct:
  81. truth = true // Struct values are always true.
  82. default:
  83. return
  84. }
  85. return truth, true
  86. }