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

Decimal cannot be converted to type float32 #81

Closed
iv-menshenin opened this issue Apr 13, 2019 · 3 comments
Closed

Decimal cannot be converted to type float32 #81

iv-menshenin opened this issue Apr 13, 2019 · 3 comments

Comments

@iv-menshenin
Copy link

iv-menshenin commented Apr 13, 2019

Scan error on column index 9, name "DISCOUNT": reflect.Value.
Convert: value of type decimal.
Decimal cannot be converted to type float32 on column DISCOUNT"

scanner:


func (f *refScanner) Scan (src interface{}) (err error) {

	defer func(){
		if r := recover(); r != nil {
			if s, ok := r.(string); ok {
				err = errors.New(s + " on column `" + f.ColumnName + "`")
			} else {
				err = errors.New(fmt.Sprintf("<%T>: %s on column `%s`", r, r, f.ColumnName))
			}
		}
	}()

	sv := reflect.ValueOf(src)
	if sv.IsValid() {

		k := sv.Kind()
		if k == reflect.Ptr || k == reflect.Interface {

			if sv.IsNil() {
				return
			}
			sv = sv.Elem()

		}

		if f.Destination.Kind() == reflect.Slice && f.Destination.Type().Elem().Kind() != reflect.Struct {

			f.Destination.Set(sv.Convert(f.Destination.Type()))

		} else
		if f.Destination.Kind() == reflect.Slice {

			// the slice must extends in each iteration
			currCap := f.Destination.Cap()
			currLen := f.RowsCount 			// needed len

			// extends if needed
			if currLen > currCap {
				newCap := (currLen + currCap / 2) + 4
				newSlice := reflect.MakeSlice(f.Destination.Type(), currLen, newCap)
				reflect.Copy(newSlice, *f.Destination)
				f.Destination.Set(newSlice)
			}
			// each iteration, remember
			f.Destination.SetLen(currLen)
			field := f.Destination.Index(f.RowsCount - 1).Field(f.Index)
			if field.Kind() == reflect.ValueOf(&(time.Time{})).Kind() && field.IsNil() {
				field.Set(reflect.New(field.Type().Elem()))
				field = field.Elem()
			}
			field.Set(sv.Convert(field.Type()))

		} else {

			if f.Destination.Kind() == reflect.ValueOf(&(time.Time{})).Kind() && f.Destination.IsNil() {
				f.Destination.Set(reflect.New(f.Destination.Type().Elem()))
				f.Destination.Elem().Set(sv.Convert(f.Destination.Elem().Type()))

			} else {
				f.Destination.Set(sv.Convert(f.Destination.Type()))

			}

		}
		return

	}

	return

}

sql procedure signature:


procedure get_list(
    session char(16) character set octets,
    fltid varchar(64),
    fskip integer,
    fcount integer,
    sorted varchar(10),
    approved boolean
    )returns(   
    created timestamp,
    client_uuid char(16) character set octets,  
    group_uuid char(16) character set octets,
    fullname varchar(64),
    ctype varchar(64),
    pic varchar(64),
    ncountry varchar(64),
    country_code varchar(64),
    ncity varchar(64),   
    city_uuid char(16) character set octets,
    discount numeric(4,2),  
    like_rate numeric(4,2),
    balance numeric(18,4),
    likes int,
    gift int,
    toptags varchar(2048),
    errcode integer,
    err varchar(250)
    );

Struct:


	TApiClientListV100 struct {
		UUID []uint8					`json:"uuid" sql:"CLIENT_UUID"`
		GroupUUID []uint8				`json:"group_uuid" sql:"GROUP_UUID"`
		FullName string					`json:"clientname" sql:"FULLNAME"`
		Type string						`json:"type" sql:"CTYPE"`
		Picture string					`json:"picture" sql:"PIC"`
		Country string					`json:"country" sql:"NCOUNTRY"`
		CountryCode string				`json:"country_code" sql:"COUNTRY_CODE"`
		City string						`json:"city" sql:"NCITY"`
		CityUUID []uint8				`json:"city_uuid" sql:"CITY_UUID"`
		Discount float32				`json:"discount" sql:"DISCOUNT"`
		Balance float32					`json:"balance" sql:"BALANCE"`
		Likes int						`json:"likes" sql:"LIKES"`
		StrTags string					`json:"-" sql:"TOPTAGS"`
		Hashtags []TApiTag100			`json:"tags"`
	}
@iv-menshenin
Copy link
Author

iv-menshenin commented Apr 13, 2019

It worked well a few months ago (six to seven months)
And brokes after 'git pull'

here is woked fine = bf34760

@iv-menshenin
Copy link
Author

and same error with float64
in this commit it works fine = bf34760

@nakagami
Copy link
Owner

nakagami commented Jun 9, 2019

@nakagami nakagami closed this as completed Jul 2, 2021
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

2 participants