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

     1  package tagexpr
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  const (
     8  	// FieldSeparator in the expression selector,
     9  	// the separator between field names
    10  	FieldSeparator = "."
    11  	// ExprNameSeparator in the expression selector,
    12  	// the separator of the field name and expression name
    13  	ExprNameSeparator = "@"
    14  	// DefaultExprName the default name of single model expression
    15  	DefaultExprName = ExprNameSeparator
    16  )
    17  
    18  // FieldSelector expression selector
    19  type FieldSelector string
    20  
    21  // JoinFieldSelector creates a field selector.
    22  func JoinFieldSelector(path ...string) string {
    23  	return strings.Join(path, FieldSeparator)
    24  }
    25  
    26  // Name returns the current field name.
    27  func (f FieldSelector) Name() string {
    28  	s := string(f)
    29  	idx := strings.LastIndex(s, FieldSeparator)
    30  	if idx == -1 {
    31  		return s
    32  	}
    33  	return s[idx+1:]
    34  }
    35  
    36  // Split returns the path segments and the current field name.
    37  func (f FieldSelector) Split() (paths []string, name string) {
    38  	s := string(f)
    39  	a := strings.Split(s, FieldSeparator)
    40  	idx := len(a) - 1
    41  	if idx > 0 {
    42  		return a[:idx], a[idx]
    43  	}
    44  	return nil, s
    45  }
    46  
    47  // Parent returns the parent FieldSelector.
    48  func (f FieldSelector) Parent() (string, bool) {
    49  	s := string(f)
    50  	i := strings.LastIndex(s, FieldSeparator)
    51  	if i < 0 {
    52  		return "", false
    53  	}
    54  	return s[:i], true
    55  }
    56  
    57  // String returns string type value.
    58  func (f FieldSelector) String() string {
    59  	return string(f)
    60  }
    61  
    62  // JoinExprSelector creates a expression selector.
    63  func JoinExprSelector(pathFields []string, exprName string) string {
    64  	p := strings.Join(pathFields, FieldSeparator)
    65  	if p == "" || exprName == "" {
    66  		return p
    67  	}
    68  	return p + ExprNameSeparator + exprName
    69  }
    70  
    71  // ExprSelector expression selector
    72  type ExprSelector string
    73  
    74  // Name returns the name of the expression.
    75  func (e ExprSelector) Name() string {
    76  	s := string(e)
    77  	atIdx := strings.LastIndex(s, ExprNameSeparator)
    78  	if atIdx == -1 {
    79  		return DefaultExprName
    80  	}
    81  	return s[atIdx+1:]
    82  }
    83  
    84  // Field returns the field selector it belongs to.
    85  func (e ExprSelector) Field() string {
    86  	s := string(e)
    87  	idx := strings.LastIndex(s, ExprNameSeparator)
    88  	if idx != -1 {
    89  		s = s[:idx]
    90  	}
    91  	return s
    92  }
    93  
    94  // ParentField returns the parent field selector it belongs to.
    95  func (e ExprSelector) ParentField() (string, bool) {
    96  	return FieldSelector(e.Field()).Parent()
    97  }
    98  
    99  // Split returns the field selector and the expression name.
   100  func (e ExprSelector) Split() (field FieldSelector, name string) {
   101  	s := string(e)
   102  	atIdx := strings.LastIndex(s, ExprNameSeparator)
   103  	if atIdx == -1 {
   104  		return FieldSelector(s), DefaultExprName
   105  	}
   106  	return FieldSelector(s[:atIdx]), s[atIdx+1:]
   107  }
   108  
   109  // String returns string type value.
   110  func (e ExprSelector) String() string {
   111  	return string(e)
   112  }