Ompluscator / dynamic-struct

Golang package for editing struct's fields during runtime and mapping structs to other structs.
MIT License
683 stars 81 forks source link

Can I cast to a dynamic struct? #9

Closed amlwwalker closed 4 years ago

amlwwalker commented 4 years ago

I am creating a dynamic struct and storing the definition ([]byte{} data) to a database.

I have an interface{} which I need to cast back to the dynamic type, once I load it out of the database. Is this possible?

Ompluscator commented 4 years ago

In general, direct casting like following code is not possible: value, ok := data.(<something>) Still, it would be possible to store data like []byte{} into database, if those bytes reflect result of json.Marshal method- in that case, json.Unmarshal back to instance of dynamic struct would be possible, as a way of casting :)

err := json.Unmarshal(data, &value)