git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/validate/converter_example_test.go (about)

     1  package validate
     2  
     3  import "time"
     4  
     5  func ExampleToBoolean() {
     6  	// Returns the boolean value represented by the string.
     7  	// It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.
     8  	// Any other value returns an error.
     9  	_, _ = ToBoolean("false")  // false, nil
    10  	_, _ = ToBoolean("T")      // true, nil
    11  	_, _ = ToBoolean("123123") // false, error
    12  }
    13  
    14  func ExampleToInt() {
    15  	_, _ = ToInt(1.0)     // 1, nil
    16  	_, _ = ToInt("-124")  // -124, nil
    17  	_, _ = ToInt("false") // 0, error
    18  }
    19  
    20  func ExampleToFloat() {
    21  	_, _ = ToFloat("-124.2e123") // -124.2e123, nil
    22  	_, _ = ToFloat("false")      // 0, error
    23  }
    24  
    25  func ExampleToString() {
    26  	_ = ToString(new(interface{}))        // 0xc000090200
    27  	_ = ToString(time.Second + time.Hour) // 1h1s
    28  	_ = ToString(123)                     // 123
    29  }
    30  
    31  func ExampleToJSON() {
    32  	_, _ = ToJSON([]int{1, 2, 3})          // [1, 2, 3]
    33  	_, _ = ToJSON(map[int]int{1: 2, 2: 3}) // { "1": 2, "2": 3 }
    34  	_, _ = ToJSON(func() {})               // error
    35  }