func.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. argExpr []Expression
  24. argVal []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. return a.runtime.isSet(a.argExpr[argumentIndex])
  29. }
  30. // Get gets an argument by index.
  31. func (a *Arguments) Get(argumentIndex int) reflect.Value {
  32. if argumentIndex < len(a.argVal) {
  33. return a.argVal[argumentIndex]
  34. }
  35. if argumentIndex < len(a.argVal)+len(a.argExpr) {
  36. return a.runtime.evalPrimaryExpressionGroup(a.argExpr[argumentIndex-len(a.argVal)])
  37. }
  38. return reflect.Value{}
  39. }
  40. // Panicf panics with formatted error message.
  41. func (a *Arguments) Panicf(format string, v ...interface{}) {
  42. panic(fmt.Errorf(format, v...))
  43. }
  44. // RequireNumOfArguments panics if the number of arguments is not in the range specified by min and max.
  45. // In case there is no minimum pass -1, in case there is no maximum pass -1 respectively.
  46. func (a *Arguments) RequireNumOfArguments(funcname string, min, max int) {
  47. num := len(a.argExpr) + len(a.argVal)
  48. if min >= 0 && num < min {
  49. a.Panicf("unexpected number of arguments in a call to %s", funcname)
  50. } else if max >= 0 && num > max {
  51. a.Panicf("unexpected number of arguments in a call to %s", funcname)
  52. }
  53. }
  54. // NumOfArguments returns the number of arguments
  55. func (a *Arguments) NumOfArguments() int {
  56. return len(a.argExpr) + len(a.argVal)
  57. }
  58. // Runtime get the Runtime context
  59. func (a *Arguments) Runtime() *Runtime {
  60. return a.runtime
  61. }
  62. // ParseInto parses the arguments into the provided pointers. It returns an error if the number of pointers passed in does not
  63. // equal the number of arguments, if any argument's value is invalid according to Go's reflect package, if an argument can't
  64. // be used as the value the pointer passed in at the corresponding position points to, or if an unhandled pointer type is encountered.
  65. // Allowed pointer types are pointers to interface{}, int, int64, float64, bool, string, time.Time, reflect.Value, []interface{},
  66. // 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
  67. // pointers to int or float types, type conversion is performed automatically if necessary.
  68. func (a *Arguments) ParseInto(ptrs ...interface{}) error {
  69. if len(ptrs) < a.NumOfArguments() {
  70. return fmt.Errorf("have %d arguments, but only %d pointers to parse into", a.NumOfArguments(), len(ptrs))
  71. }
  72. for i := 0; i < a.NumOfArguments(); i++ {
  73. arg, ptr := indirectEface(a.Get(i)), ptrs[i]
  74. ok := false
  75. if !arg.IsValid() {
  76. return fmt.Errorf("argument at position %d is not a valid value", i)
  77. }
  78. switch p := ptr.(type) {
  79. case *reflect.Value:
  80. *p, ok = arg, true
  81. case *int:
  82. switch arg.Kind() {
  83. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  84. *p, ok = int(arg.Int()), true
  85. case reflect.Float32, reflect.Float64:
  86. *p, ok = int(arg.Float()), true
  87. default:
  88. return fmt.Errorf("could not parse %v (%s) into %v (%T)", arg, arg.Type(), ptr, ptr)
  89. }
  90. case *int64:
  91. switch arg.Kind() {
  92. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  93. *p, ok = arg.Int(), true
  94. case reflect.Float32, reflect.Float64:
  95. *p, ok = int64(arg.Float()), true
  96. default:
  97. return fmt.Errorf("could not parse %v (%s) into %v (%T)", arg, arg.Type(), ptr, ptr)
  98. }
  99. case *float64:
  100. switch arg.Kind() {
  101. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  102. *p, ok = float64(arg.Int()), true
  103. case reflect.Float32, reflect.Float64:
  104. *p, ok = arg.Float(), true
  105. default:
  106. return fmt.Errorf("could not parse %v (%s) into %v (%T)", arg, arg.Type(), ptr, ptr)
  107. }
  108. }
  109. if ok {
  110. continue
  111. }
  112. if !arg.CanInterface() {
  113. return fmt.Errorf("argument at position %d can't be accessed via Interface()", i)
  114. }
  115. val := arg.Interface()
  116. switch p := ptr.(type) {
  117. case *interface{}:
  118. *p, ok = val, true
  119. case *bool:
  120. *p, ok = val.(bool)
  121. case *string:
  122. *p, ok = val.(string)
  123. case *time.Time:
  124. *p, ok = val.(time.Time)
  125. case *[]interface{}:
  126. *p, ok = val.([]interface{})
  127. case *map[string]interface{}:
  128. *p, ok = val.(map[string]interface{})
  129. default:
  130. return fmt.Errorf("trying to parse %v into %v: unhandled value type %T", arg, p, val)
  131. }
  132. if !ok {
  133. return fmt.Errorf("could not parse %v (%s) into %v (%T)", arg, arg.Type(), ptr, ptr)
  134. }
  135. }
  136. return nil
  137. }
  138. // Func function implementing this type is called directly, which is faster than calling through reflect.
  139. // If a function is being called many times in the execution of a template, you may consider implementing
  140. // a wrapper for that function implementing a Func.
  141. type Func func(Arguments) reflect.Value