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

error type out of range when use for embed fields #1

Closed
Mehrdad-Dadkhah opened this issue Jul 26, 2017 · 8 comments
Closed

error type out of range when use for embed fields #1

Mehrdad-Dadkhah opened this issue Jul 26, 2017 · 8 comments

Comments

@Mehrdad-Dadkhah
Copy link

hi
tnx for good package.

when I run this code:

       type Product struct {
		gorm.Model `view:"gorm"`
		Code string `view:"*"`
		Price uint `view:"*"`

	}


	product := &Product{}

	gormProduct := retag.Convert(product, retag.NewView("json", "gorm"))

	fmt.Println(gormProduct, reflect.TypeOf(gormProduct))

output error is:

runtime: typeOff 0xffffffffffffffed base 0xc4200ac700 not in ranges:
	types 0x66f000 etypes 0x72154c
fatal error: runtime: type offset base pointer out of range

goroutine 1 [running]:
runtime.throw(0x705437, 0x2e)
	/usr/local/go/src/runtime/panic.go:596 +0x95 fp=0xc42015d160 sp=0xc42015d140
runtime.resolveTypeOff(0xc4200ac700, 0xffffffed, 0x671d87)
	/usr/local/go/src/runtime/type.go:223 +0x350 fp=0xc42015d1c0 sp=0xc42015d160
runtime.(*_type).typeOff(0xc4200ac700, 0xffffffed, 0x671d87)
	/usr/local/go/src/runtime/type.go:239 +0x33 fp=0xc42015d1e8 sp=0xc42015d1c0
runtime.additab(0x7f2f9e916440, 0x101)
	/usr/local/go/src/runtime/iface.go:112 +0x1e2 fp=0xc42015d2d0 sp=0xc42015d1e8
runtime.getitab(0x6b6840, 0xc4200ac700, 0x4a1701, 0xc4200ac700)
	/usr/local/go/src/runtime/iface.go:79 +0x184 fp=0xc42015d368 sp=0xc42015d2d0
runtime.assertE2I2(0x6b6840, 0xc4200ac700, 0xc42014d880, 0x0, 0x0, 0x0)
	/usr/local/go/src/runtime/iface.go:294 +0x43 fp=0xc42015d398 sp=0xc42015d368
fmt.(*pp).handleMethods(0xc4200b63c0, 0xc400000076, 0x100)
	/usr/local/go/src/fmt/print.go:560 +0x70 fp=0xc42015d428 sp=0xc42015d398
fmt.(*pp).printValue(0xc4200b63c0, 0xc4200ac700, 0xc42012b6e8, 0x199, 0xc400000076, 0x3)
	/usr/local/go/src/fmt/print.go:695 +0x2d6b fp=0xc42015d620 sp=0xc42015d428
fmt.(*pp).printValue(0xc4200b63c0, 0xc42012b7a0, 0xc42012b6e0, 0x199, 0xc400000076, 0x2)
	/usr/local/go/src/fmt/print.go:778 +0x21de fp=0xc42015d818 sp=0xc42015d620
fmt.(*pp).printValue(0xc4200b63c0, 0xc42012b800, 0xc42012b6e0, 0x199, 0xc400000076, 0x1)
	/usr/local/go/src/fmt/print.go:778 +0x21de fp=0xc42015da10 sp=0xc42015d818
fmt.(*pp).printValue(0xc4200b63c0, 0xc42014f5c0, 0xc42012b6e0, 0x16, 0x76, 0x0)
	/usr/local/go/src/fmt/print.go:848 +0x1dd1 fp=0xc42015dc08 sp=0xc42015da10
fmt.(*pp).printArg(0xc4200b63c0, 0xc42014f5c0, 0xc42012b6e0, 0x76)
	/usr/local/go/src/fmt/print.go:682 +0x217 fp=0xc42015dc90 sp=0xc42015dc08
fmt.(*pp).doPrintln(0xc4200b63c0, 0xc42015de68, 0x1, 0x1)
	/usr/local/go/src/fmt/print.go:1138 +0xa1 fp=0xc42015dd08 sp=0xc42015dc90
fmt.Fprintln(0x826fc0, 0xc42008e008, 0xc42015de68, 0x1, 0x1, 0xc42012b6e0, 0x82df60, 0xc42012b800)
	/usr/local/go/src/fmt/print.go:247 +0x5c fp=0xc42015dd70 sp=0xc42015dd08
fmt.Println(0xc42015de68, 0x1, 0x1, 0xc42014d720, 0xc42014f5c0, 0xc42012b6e0)
	/usr/local/go/src/fmt/print.go:257 +0x57 fp=0xc42015ddc0 sp=0xc42015dd70
main.main()
	/home/shatranj/GoLang/gorpc/src/app/testRetag.go:62 +0x723 fp=0xc42015df88 sp=0xc42015ddc0
runtime.main()
	/usr/local/go/src/runtime/proc.go:185 +0x20a fp=0xc42015dfe0 sp=0xc42015df88
runtime.goexit()
	/usr/local/go/src/runtime/asm_amd64.s:2197 +0x1 fp=0xc42015dfe8 sp=0xc42015dfe0

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
	/usr/local/go/src/runtime/asm_amd64.s:2197 +0x1
exit status 2

go version:

go version go1.8 linux/amd64

@sevlyar
Copy link
Owner

sevlyar commented Jul 31, 2017

Thank you for the report! The issue should be fixed by 5656e2f commit, please check.

@sevlyar sevlyar closed this as completed Jul 31, 2017
@Mehrdad-Dadkhah
Copy link
Author

Thank you @sevlyar
yes it is fixed.
but unfortunately it was not useful for me! because retag change struct type to *struct.

for example in my sample code I need a struct with type Product but it return *struct and I can't cast it!

@sevlyar
Copy link
Owner

sevlyar commented Aug 1, 2017

@Mehrdad-Dadkhah, You can use next code if you need a structure instead of pointer:

gormProductPtr := Convert(product, NewView("json", "gorm"))
gormProduct := reflect.ValueOf(gormProductPtr).Elem().Interface()

But i don't understand a source of your requirements. Could you clarify your case of retag using? It would help me to extend scope of using.

@Mehrdad-Dadkhah
Copy link
Author

Thanks for reply @sevlyar

I test it but I have error!
My code is:

package main

import (
	"fmt"
	"github.com/sevlyar/retag"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"reflect"
)

func main() {
	type Product struct {
		gorm.Model `view:"gorm"`
		Code string `view:"*"`
		Price uint `view:"*"`

	}


	product := Product{}

	gormProductPtr := retag.Convert(product, retag.NewView("json", "gorm"))
	gormProduct := reflect.ValueOf(gormProductPtr).Elem().Interface()
	
	fmt.Println(gormProduct)
}

And error:

panic: reflect: Elem of invalid type

goroutine 1 [running]:
reflect.(*rtype).Elem(0x6bb980, 0xc42011f500, 0x99)
	/usr/local/go/src/reflect/type.go:954 +0xde
vendor/github.com/sevlyar/retag.Convert(0x6bb980, 0xc42011f500, 0x80dac0, 0xc42013f8e0, 0xc42014fea0, 0xc4200a4ce8)
	/home/shatranj/GoLang/gorpc/src/vendor/github.com/sevlyar/retag/tags.go:58 +0x7e
main.main()
	/home/shatranj/GoLang/gorpc/src/app/testRetag.go:22 +0x143
exit status 2

@Mehrdad-Dadkhah
Copy link
Author

About my case, I have a adaptor for gorm orm and another one for other. Gorm need a struct that gorm.Model was embedded to it and another (for example X) orm don't need it.
I don't want to define two struct one for gorm and another for X orm so I need change struct definition and make a new struct in my adaptor base on in used orm.

@sevlyar
Copy link
Owner

sevlyar commented Aug 1, 2017

@Mehrdad-Dadkhah Convert requires pointer (&product):

gormProductPtr := retag.Convert(&product, retag.NewView("json", "gorm"))

I will add semantic error message for that case.

@Mehrdad-Dadkhah
Copy link
Author

@sevlyar
Yes it's work. I test it with this simple example:

package main

import (
	"fmt"
	"github.com/sevlyar/retag"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"reflect"
)

func main() {
	type Product struct {
		gorm.Model `view:"gorm"`
		Code string `view:"*"`
		Price uint `view:"*"`

	}


	product := Product{}

	gormProductPtr := retag.Convert(&product, retag.NewView("json", "gorm"))
	gormProduct := reflect.ValueOf(gormProductPtr).Elem().Interface()
	fmt.Println("type of gormProduct : ",reflect.TypeOf(gormProduct))

	orginalProduct, _ := gormProduct.(Product);
	fmt.Println("type of orginalProduct : ", reflect.TypeOf(orginalProduct))

	connection, _ := gorm.Open("mysql", "root:2014@/gorpc?charset=utf8&parseTime=True&loc=Local")
	dbObj := connection.First(&orginalProduct, 1)

	fmt.Println("yes! gorm work with it fine. result: ")
	fmt.Println(orginalProduct, dbObj)
}

and output is:

type of gormProduct :  struct { Model gorm.Model; Code string; Price uint }
type of orginalProduct :  main.Product
yes! gorm work with it fine. result: 
{{1 2017-05-05 16:36:56 +0430 IRDT 2017-07-11 23:16:33 +0430 IRDT <nil>} L1212 2000} &{0xc42011f620 <nil> 1 <nil> 0xc42012d860 0xc42012d7c0 0xc4201257c0 0 {0xc4201380f0} <nil> false  map[gorm:order_by_primary_key:ASC] map[]}

one question:
Can we retrieve a type from a variable of that type?
I want to use it for casting ...
I want to cast a struct to another (for this case gormProduct with type of struct{} to orginalProduct with type of Product) when I have not straight access to Product type and just have a variable of that type.
is my question clear?

thanks very very much for help me @sevlyar

@sevlyar
Copy link
Owner

sevlyar commented Aug 16, 2017

@Mehrdad-Dadkhah Convert just converts a type of the pointer. It doesn't allocate or copy memory:

    product := Product{}
    ptr := &product 
    newPtr := retag.Convert(ptr, retag.NewView("json", "gorm"))
    // Now ptr and newPtr both point on the same memory - product.

    // ...
    connection.First(&newPtr, 1)
    // Now fields of the product are completely filled by gorm:
    fmt.Println(product.Code, product.Price)

p.s.
I am sorry. I already written an answer on your question earlier but seems i have forgot to press 'Comment' button.

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