func.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2016 José Santos <henrique_1609@me.com>
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package jet
  15. import (
  16. "fmt"
  17. "reflect"
  18. "time"
  19. )
  20. // Arguments holds the arguments passed to jet.Func.
  21. type Arguments struct {
  22. runtime *Runtime
  23. args CallArgs
  24. pipedVal *reflect.Value
  25. }
  26. // IsSet checks whether an argument is set or not. It behaves like the build-in isset function.
  27. func (a *Arguments) IsSet(argumentIndex int) bool {
  28. if argumentIndex < len(a.args.Exprs) {
  29. if a.args.Exprs[argumentIndex].Type() == NodeUnderscore {
  30. return a.pipedVal != nil
  31. }
  32. return a.runtime.isSet(a.args.Exprs[argumentIndex])
  33. }
  34. if len(a.args.Exprs) == 0 && argumentIndex == 0 {
  35. return a.pipedVal != nil
  36. }
  37. return false
  38. }
  39. // Get gets an argument by index.
  40. func (a *Arguments) Get(argumentIndex int) reflect.Value {
  41. if argumentIndex < len(a.args.Exprs) {
  42. if a.args.Exprs[argumentIndex].Type() == NodeUnderscore {
  43. return *a.pipedVal
  44. }
  45. return a.runtime.evalPrimaryExpressionGroup(a.args.Exprs[argumentIndex])
  46. }
  47. if len(a.args.Exprs) == 0 && argumentIndex == 0 {
  48. return *a.pipedVal
  49. }
  50. return reflect.Value{}
  51. }
  52. // Panicf panics with formatted error message.
  53. func (a *Arguments) Panicf(format string, v ...interface{}) {
  54. panic(fmt.Errorf(format, v...))
  55. }
  56. // RequireNumOfArguments panics if the number of arguments is not in the range specified by min and max.
  57. // In case there is no minimum pass -1, in case there is no maximum pass -1 respectively.
  58. func (a *Arguments) RequireNumOfArguments(funcname string, min, max int) {
  59. num := a.NumOfArguments()
  60. if min >= 0 && num < min {
  61. a.Panicf("unexpected number of arguments in a call to %s", funcname)
  62. } else if max >= 0 && num > max {
  63. a.Panicf("unexpected number of arguments in a call to %s", funcname)
  64. }
  65. }
  66. // NumOfArguments returns the number of arguments
  67. func (a *Arguments) NumOfArguments() int {
  68. num := len(a.args.Exprs)
  69. if a.pipedVal != nil && !a.args.HasPipeSlot {
  70. return num + 1
  71. }
  72. return num
  73. }
  74. // Runtime get the Runtime context
  75. func (a *Arguments) Runtime() *Runtime {
  76. return a.runtime
  77. }
  78. // ParseInto parses the arguments into the provided pointers. It returns an error if the number of pointers passed in does not
  79. // equal the number of arguments, if any argument's value is invalid according to Go's reflect package, if an argument can't
  80. // be used as the value the pointer passed in at the corresponding position points to, or if an unhandled pointer type is encountered.
  81. // Allowed pointer types are pointers to interface{}, int, int64, float64, bool, string, time.Time, reflect.Value, []interface{},
  82. // map[string]interface{}. If a pointer to a reflect.Value is passed in, the argument be assigned as-is to the value pointed to. For
  83. // pointers to int or float types, type conversion is performed automatically if necessary.
  84. func (a *Arguments) ParseInto(ptrs ...interface{}) error {
  85. if len(ptrs) < a.NumOfArguments() {
  86. return fmt.Errorf("have %d arguments, but only %d pointers to parse into", a.NumOfArguments(), len(ptrs))
  87. }
  88. for i := 0; i < a.NumOfArguments(); i++ {
  89. arg, ptr := indirectEface(a.Get(i)), ptrs[i]
  90. ok := false
  91. if !arg.IsValid() {
  92. return fmt.Errorf("argument at position %d is not a valid value", i)
  93. }
  94. switch p := ptr.(type) {
  95. case *reflect.Value:
  96. *p, ok = arg, true
  97. case *int:
  98. switch arg.Kind() {
  99. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  100. *p, ok = int(arg.Int()), true
  101. case reflect.Float32, reflect.Float64:
  102. *p, ok = int(arg.Float()), true
  103. default:
  104. return fmt.Errorf("could not parse %v (%s) into %v (%T)", arg, arg.Type(), ptr, ptr)
  105. }
  106. case *int64:
  107. switch arg.Kind() {
  108. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  109. *p, ok = arg.Int(), true
  110. case reflect.Float32, reflect.Float64:
  111. *p, ok = int64(arg.Float()), true
  112. default:
  113. return fmt.Errorf("could not parse %v (%s) into %v (%T)", arg, arg.Type(), ptr, ptr)
  114. }
  115. case *float64:
  116. switch arg.Kind() {
  117. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  118. *p, ok = float64(arg.Int()), true
  119. case reflect.Float32, reflect.Float64:
  120. *p, ok = arg.Float(), true
  121. default:
  122. return fmt.Errorf("could not parse %v (%s) into %v (%T)", arg, arg.Type(), ptr, ptr)
  123. }
  124. }
  125. if ok {
  126. continue
  127. }
  128. if !arg.CanInterface() {
  129. return fmt.Errorf("argument at position %d can't be accessed via Interface()", i)
  130. }
  131. val := arg.Interface()
  132. switch p := ptr.(type) {
  133. case *interface{}:
  134. *p, ok = val, true
  135. case *bool:
  136. *p, ok = val.(bool)
  137. case *string:
  138. *p, ok = val.(string)
  139. case *time.Time:
  140. *p, ok = val.(time.Time)
  141. case *[]interface{}:
  142. *p, ok = val.([]interface{})
  143. case *map[string]interface{}:
  144. *p, ok = val.(map[string]interface{})
  145. default:
  146. return fmt.Errorf("trying to parse %v into %v: unhandled value type %T", arg, p, val)
  147. }
  148. if !ok {
  149. return fmt.Errorf("could not parse %v (%s) into %v (%T)", arg, arg.Type(), ptr, ptr)
  150. }
  151. }
  152. return nil
  153. }
  154. // Func function implementing this type is called directly, which is faster than calling through reflect.
  155. // If a function is being called many times in the execution of a template, you may consider implementing
  156. // a wrapper for that function implementing a Func.
  157. type Func func(Arguments) reflect.Value