Hello,
The following text and my code are hosted here: https://gist.github.com/luizdepra/d503601068ae5a72caae
I'm looking for a way to insert an extract data dynamically from structs in Go. To better undestanding, this is my scenario. I need 2 functions:
func Build(name string, slice []interface{}) (Data, error)
: where name
is a object type identifier and slice
is a list of values. This function must create an object that represents the type name
and populate it with values formslice
, and it must use the order tag to do this job.func Extract(name string, data Data) ([]interface{}, error)
: where name
is a object type identifier and data
is a struct pointer. This function must create slice of interface{}
with the values from the fields of the object data
, again using order tag to set value order.I need to create those two function in a generic way because I have 44 diferent structs (generated by Apache's Thrift Go implementation). I wanna avoid to code 88 functions, 2 for each object.
I'm playing with reflect
package and done a lot of progress. See my main.go
file. I'm having 3 problems with my code:
reflect.Value
object, I wanna convert it to *ObjectType
, where ObjectType is retrieved inside the function.slice
will have int values as uint64 (because a default untyped conversion from a json string). I need to convert those values to the right type, as defined in the fields of ObjectType
..Elem()
from line 50. I suspect that this is caused by problem 1, but I may be wrong.I know I can solve convertion problems with a switch clause using Kind
values. But, if possible, I wanna avoid it.
So, you guys know any way to solve those problems, or any better soluction to achiev it?
Thank you,
Luiz
I'm looking for a way to insert an extract data dynamically from structs in Go. To better undestanding, this is my scenario. I need 2 functions:
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Klaus,The code in the Gist is almost identical my real code. See NormalData and ConfirmData, those two are real examples.And I can't change the original thrift generation script, but I can create another one to read the .thrift file and generate 88 functions (what is i'm doing right now). But I'm trying to find another way to solve my problem with less code repetition and not so wrapped arround code generators (thrift's generated code are 30k lines long, and my 88 generated functions are 4k lines long). I'm think: is better and easy to test and mantain 2 functions then 88.
--