Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Int from string support? #44

Open
MikeCongdon1 opened this issue Jan 23, 2019 · 0 comments
Open

Int from string support? #44

MikeCongdon1 opened this issue Jan 23, 2019 · 0 comments

Comments

@MikeCongdon1
Copy link

// NewInt creates a new Int from a string
func NewIntFromString(i string, valid bool) Int {
if valid {
inter, err := strconv.Atoi(i)
if err != nil {
return Int{
NullInt64: sql.NullInt64{
Int64: 0,
Valid: false,
},
}
}
in := int64(inter)
return Int{
NullInt64: sql.NullInt64{
Int64: in,
Valid: valid,
},
}
}
return Int{
NullInt64: sql.NullInt64{
Int64: 0,
Valid: false,
},
}
}
test:
func TestNewIntFromString(t *testing.T) {
type args struct {
i string
valid bool
}
tests := []struct {
name string
args args
want Int
}{
{
name: "equal zero",
args: args{
i: "0",
valid: false,
},
want: IntFrom(0),
// TODO: Add test cases.
},
{
name: "equal one",
args: args{
i: "1",
valid: true,
},
want: IntFrom(1),
// TODO: Add test cases.
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewIntFromString(tt.args.i, tt.args.valid); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewIntFromString() = %v, want %v", got, tt.want)
}
})
}
}

I use this like:
id:= zero.NewIntFromString(c.GetQuery("id"))
where c is a gin.Context (could do similar with http.GetForm)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant