asaskevich / govalidator

[Go] Package of validators and sanitizers for strings, numerics, slices and structs
MIT License
6.04k stars 555 forks source link

IsInt(ToString("1")) fails #54

Closed robmurtha closed 9 years ago

robmurtha commented 9 years ago

tmp,_ := v.ToString(fmt.Sprint(1)) Expect(v.IsInt(tmp)).To(BeTrue(),"v.ToString makes double quotes, IsInt fails to recognize int string")

asaskevich commented 9 years ago

json.Marshal, that used inside function, adds double quotes. I think that it can be replaced with fmt.Sprintf("%v", obj) that works like this:

package main

import (
    "fmt"
)

type Outer struct {
    A int
    B int
    InnerItem Inner
}

type Inner struct {
    C float64
    D complex64
}

func main() {
    println(fmt.Sprintf("%v", 1))                                                         //=> 1
    println(fmt.Sprintf("%v", "abc"))                                                   //=> abc
    println(fmt.Sprintf("%v", 'a'))                                                        //=> 97
    println(fmt.Sprintf("%v", true))                                                    //=> true
    println(fmt.Sprintf("%v", Outer{10, 20, Inner{1.45, 1.5 + 3.5i}})) //=> {10 20 {1.45 (1.5+3.5i)}}
    println(fmt.Sprintf("%v", 1.5))                                                      //=> 1.5
    println(fmt.Sprintf("%v", 1.0 + 10i))                                            //=> (1+10i)
}

I will try to replace json.Marshal with fmt.Sprintf if all tests will be OK.

robmurtha commented 9 years ago

Ok I can see the purpose is for output vs conversion. Perhaps a ToQuotedString(obj interface{}) interface would be a more intuitive name but I'm not asking for a change, I've been using Sprintf. Thanks I like your package.

On Sat, May 2, 2015 at 5:29 AM, Alex Saskevich notifications@github.com wrote:

json.Marshal, that used inside function, adds double quotes. I think that it can be replaced with fmt.Sprintf("%v", obj) that works like this:

package main import ( "fmt" ) type Outer struct { A int B int InnerItem Inner } type Inner struct { C float64 D complex64 } func main() { println(fmt.Sprintf("%v", 1)) //=> 1 println(fmt.Sprintf("%v", "abc")) //=> abc println(fmt.Sprintf("%v", 'a')) //=> 97 println(fmt.Sprintf("%v", true)) //=> true println(fmt.Sprintf("%v", Outer{10, 20, Inner{1.45, 1.5 + 3.5i}})) //=> {10 20 {1.45 (1.5+3.5i)}} println(fmt.Sprintf("%v", 1.5)) //=> 1.5 println(fmt.Sprintf("%v", 1.0 + 10i)) //=> (1+10i) }

I will try to replace json.Marshal with fmt.Sprintf if all tests will be OK.

— Reply to this email directly or view it on GitHub https://github.com/asaskevich/govalidator/issues/54#issuecomment-98339388 .

Rob Murtha 302-359-5943

http://www.linkedin.com/in/robmurtha http://www.twitter.com/robmurtha