github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zvalid/value.go (about)

     1  package zvalid
     2  
     3  import (
     4  	"container/list"
     5  	"strconv"
     6  	"strings"
     7  
     8  	"github.com/sohaha/zlsgo/ztype"
     9  )
    10  
    11  // Ok not err
    12  func (v Engine) Ok() bool {
    13  	return v.Error() == nil
    14  }
    15  
    16  // Error or whether the verification fails
    17  func (v Engine) Error() error {
    18  	if v.result {
    19  		return v.err
    20  	}
    21  	return v.valid().err
    22  }
    23  
    24  // Value get the final value
    25  func (v Engine) Value() (value string) {
    26  	return v.valid().value
    27  }
    28  
    29  // String to string
    30  func (v Engine) String() (string, error) {
    31  	v.valid()
    32  	return v.value, v.err
    33  }
    34  
    35  // Bool to bool
    36  func (v Engine) Bool() (bool, error) {
    37  	v.valid()
    38  	if ignore(&v) {
    39  		return false, v.err
    40  	}
    41  	value, err := strconv.ParseBool(v.value)
    42  	if err != nil {
    43  		return false, err
    44  	}
    45  	return value, nil
    46  }
    47  
    48  // Int convert to int
    49  func (v Engine) Int() (int, error) {
    50  	v.valid()
    51  	if ignore(&v) {
    52  		return 0, v.err
    53  	}
    54  	if v.valueInt != 0 {
    55  		return v.valueInt, nil
    56  	}
    57  	value, err := strconv.Atoi(v.value)
    58  	if err != nil {
    59  		return 0, err
    60  	}
    61  	v.valueInt = value
    62  	return value, nil
    63  }
    64  
    65  // Float64 convert to float64
    66  func (v Engine) Float64() (float64, error) {
    67  	v.valid()
    68  	if ignore(&v) {
    69  		return 0, v.err
    70  	}
    71  	if v.valueFloat != 0 {
    72  		return v.valueFloat, nil
    73  	}
    74  	value, err := strconv.ParseFloat(v.value, 64)
    75  	if err != nil {
    76  		return 0, err
    77  	}
    78  	v.valueFloat = value
    79  	return value, nil
    80  }
    81  
    82  // Split converted to [] string
    83  func (v Engine) Split(sep string) ([]string, error) {
    84  	v.valid()
    85  	if ignore(&v) {
    86  		return []string{}, v.err
    87  	}
    88  	value := strings.Split(v.value, sep)
    89  	if len(value) == 0 {
    90  		return []string{}, v.err
    91  	}
    92  	return value, nil
    93  }
    94  
    95  // Valid get the final value, or an notEmpty string if an error occurs
    96  func (v *Engine) valid() *Engine {
    97  	if v.result || v.queue == nil {
    98  		return v
    99  	}
   100  	v.result = true
   101  	if v.err == nil && !v.setRawValue {
   102  		v.err = ErrNoValidationValueSet
   103  		return v
   104  	}
   105  
   106  	queues := list.New()
   107  	queues.PushBackList(v.queue)
   108  	l := queues.Len()
   109  	if l > 0 {
   110  		for i := 0; i < l; i++ {
   111  			queue := queues.Front()
   112  			if q, ok := queue.Value.(queueT); ok {
   113  				nv := q(v)
   114  				v.value = nv.value
   115  				v.err = nv.err
   116  				v.defaultValue = nv.defaultValue
   117  			}
   118  			queues.Remove(queue)
   119  		}
   120  	}
   121  	return v
   122  }
   123  
   124  // SetAlias set alias
   125  func (v Engine) SetAlias(name string) Engine {
   126  	v.name = name
   127  	return v
   128  }
   129  
   130  // Verifi validate specified data
   131  func (v Engine) Verifi(value string, name ...string) Engine {
   132  	v.value = value
   133  	v.setRawValue = true
   134  	if len(name) > 0 {
   135  		v.name = name[0]
   136  	}
   137  	return v
   138  }
   139  
   140  // VerifiAny validate specified data
   141  func (v Engine) VerifiAny(value interface{}, name ...string) Engine {
   142  	var s string
   143  	switch vv := value.(type) {
   144  	case string:
   145  		s = vv
   146  	default:
   147  		s = ztype.ToString(vv)
   148  		// v.err = setError(&v, "unsupported type")
   149  	}
   150  	return v.Verifi(s, name...)
   151  }