Ompluscator / dynamic-struct

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

provide types(string,int etc) directly in place of instance of types #11

Closed dharmjit closed 4 years ago

dharmjit commented 4 years ago

Hi, I just came across this library and started playing with it for one of my use case. I have some dynamic domain types in my application which can be created through UI and user can provide name, types for all attributes etc. which is then saved to DB. Now for the data I need to create dynamic Struct from the DB which has type already stored. So is it possible to add field like below

dynamicstruct.NewStruct()
    .AddField("Name", string, `json:"name"`).Build().New()
Ompluscator commented 4 years ago

Hi. Yes, it's possible, just for type you need to pass some value which is of that type. So, if you want string, you pass, for example, an empty string. For int, you pass some integer number etc:

0 // int "" //string 0.0 //float64 int64(0) //int64 ...