custom null float64 not support when get or count, but find is ok #1256

Open
opened 2019-03-29 06:51:09 +00:00 by hlily2005 · 5 comments
hlily2005 commented 2019-03-29 06:51:09 +00:00 (Migrated from github.com)

not supported {{0 false}} as []

not supported {{0 false}} as []

Could you give more detail about how do you want to use this via XORM?

Could you give more detail about how do you want to use this via XORM?
hlily2005 commented 2019-03-29 06:57:31 +00:00 (Migrated from github.com)

@lunny
I define struct, like this:

type MyNullFloat64 struct {
	sql.NullFloat64
}

func (f *MyNullFloat64) MarshalJSON() ([]byte, error) {
	if f.Valid {
		return json.Marshal(f.Float64)
	} else {
		return json.Marshal(nil)
	}
}

func (f *MyNullFloat64) UnmarshalJSON(b []byte) (err error) {
	var x *float64
	if err := json.Unmarshal(b, &x); err != nil {
		return err
	}
	if x != nil {
		f.Valid = true
		f.Float64 = *x
	} else {
		f.Valid = false
	}
	return nil
}
type MyPlan struct {
	Uuid	string
	Amount	MyNullFloat64
}

xorm().find(&[]MyPlan{}) is ok, but xorm().Get(&MyPlan{}) or xorm().Count(&MyPlan{}) will error, error msg is 'not supported {{0 false}} as []'

@lunny I define struct, like this: ``` type MyNullFloat64 struct { sql.NullFloat64 } func (f *MyNullFloat64) MarshalJSON() ([]byte, error) { if f.Valid { return json.Marshal(f.Float64) } else { return json.Marshal(nil) } } func (f *MyNullFloat64) UnmarshalJSON(b []byte) (err error) { var x *float64 if err := json.Unmarshal(b, &x); err != nil { return err } if x != nil { f.Valid = true f.Float64 = *x } else { f.Valid = false } return nil } ``` ``` type MyPlan struct { Uuid string Amount MyNullFloat64 } ``` xorm().find(&[]MyPlan{}) is ok, but xorm().Get(&MyPlan{}) or xorm().Count(&MyPlan{}) will error, error msg is 'not supported {{0 false}} as []'
hlily2005 commented 2019-03-29 07:00:05 +00:00 (Migrated from github.com)

@lunny
If use sql.NullFloat64, XORM's count or get no error. My XORM version is 0.7.1

@lunny If use sql.NullFloat64, XORM's count or get no error. My XORM version is 0.7.1

Maybe you can try

type MyNullFloat64 sql.NullFloat64
Maybe you can try ```go type MyNullFloat64 sql.NullFloat64 ```
hlily2005 commented 2019-03-29 07:21:03 +00:00 (Migrated from github.com)

@lunny
I try it, had the same error callback

@lunny I try it, had the same error callback
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#1256
No description provided.