github.com/bytedance/go-tagexpr/v2@v2.9.8/handler.go (about)

     1  package tagexpr
     2  
     3  import "reflect"
     4  
     5  // FieldHandler field handler
     6  type FieldHandler struct {
     7  	selector string
     8  	field    *fieldVM
     9  	expr     *TagExpr
    10  }
    11  
    12  func newFieldHandler(expr *TagExpr, fieldSelector string, field *fieldVM) *FieldHandler {
    13  	return &FieldHandler{
    14  		selector: fieldSelector,
    15  		field:    field,
    16  		expr:     expr,
    17  	}
    18  }
    19  
    20  // StringSelector returns the field selector of string type.
    21  func (f *FieldHandler) StringSelector() string {
    22  	return f.selector
    23  }
    24  
    25  // FieldSelector returns the field selector of FieldSelector type.
    26  func (f *FieldHandler) FieldSelector() FieldSelector {
    27  	return FieldSelector(f.selector)
    28  }
    29  
    30  // Value returns the field value.
    31  // NOTE:
    32  //  If initZero==true, initialize nil pointer to zero value
    33  func (f *FieldHandler) Value(initZero bool) reflect.Value {
    34  	return f.field.reflectValueGetter(f.expr.ptr, initZero)
    35  }
    36  
    37  // EvalFuncs returns the tag expression eval functions.
    38  func (f *FieldHandler) EvalFuncs() map[ExprSelector]func() interface{} {
    39  	targetTagExpr, _ := f.expr.checkout(f.selector)
    40  	evals := make(map[ExprSelector]func() interface{}, len(f.field.exprs))
    41  	for k, v := range f.field.exprs {
    42  		expr := v
    43  		exprSelector := ExprSelector(k)
    44  		evals[exprSelector] = func() interface{} {
    45  			return expr.run(exprSelector.Name(), targetTagExpr)
    46  		}
    47  	}
    48  	return evals
    49  }
    50  
    51  // StructField returns the field StructField object.
    52  func (f *FieldHandler) StructField() reflect.StructField {
    53  	return f.field.structField
    54  }
    55  
    56  // ExprHandler expr handler
    57  type ExprHandler struct {
    58  	base       string
    59  	path       string
    60  	selector   string
    61  	expr       *TagExpr
    62  	targetExpr *TagExpr
    63  }
    64  
    65  func newExprHandler(te, tte *TagExpr, base, es string) *ExprHandler {
    66  	return &ExprHandler{
    67  		base:       base,
    68  		selector:   es,
    69  		expr:       te,
    70  		targetExpr: tte,
    71  	}
    72  }
    73  
    74  // TagExpr returns the *TagExpr.
    75  func (e *ExprHandler) TagExpr() *TagExpr {
    76  	return e.expr
    77  }
    78  
    79  // StringSelector returns the expression selector of string type.
    80  func (e *ExprHandler) StringSelector() string {
    81  	return e.selector
    82  }
    83  
    84  // ExprSelector returns the expression selector of ExprSelector type.
    85  func (e *ExprHandler) ExprSelector() ExprSelector {
    86  	return ExprSelector(e.selector)
    87  }
    88  
    89  // Path returns the path description of the expression.
    90  func (e *ExprHandler) Path() string {
    91  	if e.path == "" {
    92  		if e.targetExpr.path == "" {
    93  			e.path = e.selector
    94  		} else {
    95  			e.path = e.targetExpr.path + FieldSeparator + e.selector
    96  		}
    97  	}
    98  	return e.path
    99  }
   100  
   101  // Eval evaluate the value of the struct tag expression.
   102  // NOTE:
   103  //  result types: float64, string, bool, nil
   104  func (e *ExprHandler) Eval() interface{} {
   105  	return e.expr.s.exprs[e.selector].run(e.base, e.targetExpr)
   106  }
   107  
   108  // EvalFloat evaluates the value of the struct tag expression.
   109  // NOTE:
   110  //  If the expression value type is not float64, return 0.
   111  func (e *ExprHandler) EvalFloat() float64 {
   112  	r, _ := e.Eval().(float64)
   113  	return r
   114  }
   115  
   116  // EvalString evaluates the value of the struct tag expression.
   117  // NOTE:
   118  //  If the expression value type is not string, return "".
   119  func (e *ExprHandler) EvalString() string {
   120  	r, _ := e.Eval().(string)
   121  	return r
   122  }
   123  
   124  // EvalBool evaluates the value of the struct tag expression.
   125  // NOTE:
   126  //  If the expression value is not 0, '' or nil, return true.
   127  func (e *ExprHandler) EvalBool() bool {
   128  	return FakeBool(e.Eval())
   129  }