Convert bool to tinyint golang
I am using the latest version of xorm and want to create a simple go struct as follows:
types myStruct struct {
isDeleted bool `xorm:"'isDeleted' tinyint(3)"`
}
I know the bool type in go evaluates to true and false, but I need to map it to a mySql database where the values are tinyint(3) and 1 maps to true and 0 to false. In the example above no matter what my post requests look like isDeleted always evaluates to 0. Thanks in advance for any advice on this issue. This https://github.com/go-xorm/xorm/issues/673 may provide some context.
go go-xorm
add a comment |
I am using the latest version of xorm and want to create a simple go struct as follows:
types myStruct struct {
isDeleted bool `xorm:"'isDeleted' tinyint(3)"`
}
I know the bool type in go evaluates to true and false, but I need to map it to a mySql database where the values are tinyint(3) and 1 maps to true and 0 to false. In the example above no matter what my post requests look like isDeleted always evaluates to 0. Thanks in advance for any advice on this issue. This https://github.com/go-xorm/xorm/issues/673 may provide some context.
go go-xorm
Aboolean
in MySQL IS atinyint
. Any particular reason the column was specified as atinyint
instead of aboolean
? This could have been avoided by having the database handle it.
– RayfenWindspear
Nov 21 '18 at 17:41
add a comment |
I am using the latest version of xorm and want to create a simple go struct as follows:
types myStruct struct {
isDeleted bool `xorm:"'isDeleted' tinyint(3)"`
}
I know the bool type in go evaluates to true and false, but I need to map it to a mySql database where the values are tinyint(3) and 1 maps to true and 0 to false. In the example above no matter what my post requests look like isDeleted always evaluates to 0. Thanks in advance for any advice on this issue. This https://github.com/go-xorm/xorm/issues/673 may provide some context.
go go-xorm
I am using the latest version of xorm and want to create a simple go struct as follows:
types myStruct struct {
isDeleted bool `xorm:"'isDeleted' tinyint(3)"`
}
I know the bool type in go evaluates to true and false, but I need to map it to a mySql database where the values are tinyint(3) and 1 maps to true and 0 to false. In the example above no matter what my post requests look like isDeleted always evaluates to 0. Thanks in advance for any advice on this issue. This https://github.com/go-xorm/xorm/issues/673 may provide some context.
go go-xorm
go go-xorm
asked Nov 21 '18 at 17:16
00robinette00robinette
7817
7817
Aboolean
in MySQL IS atinyint
. Any particular reason the column was specified as atinyint
instead of aboolean
? This could have been avoided by having the database handle it.
– RayfenWindspear
Nov 21 '18 at 17:41
add a comment |
Aboolean
in MySQL IS atinyint
. Any particular reason the column was specified as atinyint
instead of aboolean
? This could have been avoided by having the database handle it.
– RayfenWindspear
Nov 21 '18 at 17:41
A
boolean
in MySQL IS a tinyint
. Any particular reason the column was specified as a tinyint
instead of a boolean
? This could have been avoided by having the database handle it.– RayfenWindspear
Nov 21 '18 at 17:41
A
boolean
in MySQL IS a tinyint
. Any particular reason the column was specified as a tinyint
instead of a boolean
? This could have been avoided by having the database handle it.– RayfenWindspear
Nov 21 '18 at 17:41
add a comment |
1 Answer
1
active
oldest
votes
I'm not sure what/if xorm
can do about it, but you can just create a type and implement the Valuer
and Scanner
interfaces for it. Here is an example I did a pull request for using a bit(1)
for a bool
.
https://github.com/jmoiron/sqlx/blob/master/types/types.go#L152
For an integer, you would just return the int
instead of a byte
containing the int
. Like so:
type IntBool bool
// Value implements the driver.Valuer interface,
// and turns the IntBool into an integer for MySQL storage.
func (i IntBool) Value() (driver.Value, error) {
if i {
return 1, nil
}
return 0, nil
}
// Scan implements the sql.Scanner interface,
// and turns the int incoming from MySQL into an IntBool
func (i *IntBool) Scan(src interface{}) error {
v, ok := src.(int)
if !ok {
return errors.New("bad int type assertion")
}
*i = v == 1
return nil
}
Then your struct would just use the new type
type myStruct struct {
isDeleted IntBool `xorm:"'isDeleted' tinyint(3)"`
}
But, again, is there any particular reason you declared this boolean value as a tinyint
? MySQL has a boolean type and things would just work.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417403%2fconvert-bool-to-tinyint-golang%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm not sure what/if xorm
can do about it, but you can just create a type and implement the Valuer
and Scanner
interfaces for it. Here is an example I did a pull request for using a bit(1)
for a bool
.
https://github.com/jmoiron/sqlx/blob/master/types/types.go#L152
For an integer, you would just return the int
instead of a byte
containing the int
. Like so:
type IntBool bool
// Value implements the driver.Valuer interface,
// and turns the IntBool into an integer for MySQL storage.
func (i IntBool) Value() (driver.Value, error) {
if i {
return 1, nil
}
return 0, nil
}
// Scan implements the sql.Scanner interface,
// and turns the int incoming from MySQL into an IntBool
func (i *IntBool) Scan(src interface{}) error {
v, ok := src.(int)
if !ok {
return errors.New("bad int type assertion")
}
*i = v == 1
return nil
}
Then your struct would just use the new type
type myStruct struct {
isDeleted IntBool `xorm:"'isDeleted' tinyint(3)"`
}
But, again, is there any particular reason you declared this boolean value as a tinyint
? MySQL has a boolean type and things would just work.
add a comment |
I'm not sure what/if xorm
can do about it, but you can just create a type and implement the Valuer
and Scanner
interfaces for it. Here is an example I did a pull request for using a bit(1)
for a bool
.
https://github.com/jmoiron/sqlx/blob/master/types/types.go#L152
For an integer, you would just return the int
instead of a byte
containing the int
. Like so:
type IntBool bool
// Value implements the driver.Valuer interface,
// and turns the IntBool into an integer for MySQL storage.
func (i IntBool) Value() (driver.Value, error) {
if i {
return 1, nil
}
return 0, nil
}
// Scan implements the sql.Scanner interface,
// and turns the int incoming from MySQL into an IntBool
func (i *IntBool) Scan(src interface{}) error {
v, ok := src.(int)
if !ok {
return errors.New("bad int type assertion")
}
*i = v == 1
return nil
}
Then your struct would just use the new type
type myStruct struct {
isDeleted IntBool `xorm:"'isDeleted' tinyint(3)"`
}
But, again, is there any particular reason you declared this boolean value as a tinyint
? MySQL has a boolean type and things would just work.
add a comment |
I'm not sure what/if xorm
can do about it, but you can just create a type and implement the Valuer
and Scanner
interfaces for it. Here is an example I did a pull request for using a bit(1)
for a bool
.
https://github.com/jmoiron/sqlx/blob/master/types/types.go#L152
For an integer, you would just return the int
instead of a byte
containing the int
. Like so:
type IntBool bool
// Value implements the driver.Valuer interface,
// and turns the IntBool into an integer for MySQL storage.
func (i IntBool) Value() (driver.Value, error) {
if i {
return 1, nil
}
return 0, nil
}
// Scan implements the sql.Scanner interface,
// and turns the int incoming from MySQL into an IntBool
func (i *IntBool) Scan(src interface{}) error {
v, ok := src.(int)
if !ok {
return errors.New("bad int type assertion")
}
*i = v == 1
return nil
}
Then your struct would just use the new type
type myStruct struct {
isDeleted IntBool `xorm:"'isDeleted' tinyint(3)"`
}
But, again, is there any particular reason you declared this boolean value as a tinyint
? MySQL has a boolean type and things would just work.
I'm not sure what/if xorm
can do about it, but you can just create a type and implement the Valuer
and Scanner
interfaces for it. Here is an example I did a pull request for using a bit(1)
for a bool
.
https://github.com/jmoiron/sqlx/blob/master/types/types.go#L152
For an integer, you would just return the int
instead of a byte
containing the int
. Like so:
type IntBool bool
// Value implements the driver.Valuer interface,
// and turns the IntBool into an integer for MySQL storage.
func (i IntBool) Value() (driver.Value, error) {
if i {
return 1, nil
}
return 0, nil
}
// Scan implements the sql.Scanner interface,
// and turns the int incoming from MySQL into an IntBool
func (i *IntBool) Scan(src interface{}) error {
v, ok := src.(int)
if !ok {
return errors.New("bad int type assertion")
}
*i = v == 1
return nil
}
Then your struct would just use the new type
type myStruct struct {
isDeleted IntBool `xorm:"'isDeleted' tinyint(3)"`
}
But, again, is there any particular reason you declared this boolean value as a tinyint
? MySQL has a boolean type and things would just work.
answered Nov 21 '18 at 18:09
RayfenWindspearRayfenWindspear
3,7331430
3,7331430
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417403%2fconvert-bool-to-tinyint-golang%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
A
boolean
in MySQL IS atinyint
. Any particular reason the column was specified as atinyint
instead of aboolean
? This could have been avoided by having the database handle it.– RayfenWindspear
Nov 21 '18 at 17:41