ini icon indicating copy to clipboard operation
ini copied to clipboard

Map to struct with custom logics

Open luqasz opened this issue 9 years ago • 6 comments

Hi.

Thx for your library. Very helpful. I was wondering if there is any way to map a section to struct with custom types. So that every value will be transformed via a custom function.

luqasz avatar Jan 11 '17 19:01 luqasz

Hi, thanks for the good idea, but unfortunately it's not supported yet. Will consider such functionality..

unknwon avatar Jan 17 '17 12:01 unknwon

In a second thought, what is your definition of custom types?

unknwon avatar Jan 22 '17 11:01 unknwon

Map from string to constant or function. E.g. every key in section can be "transformed" into a struct field and also validated.

luqasz avatar Jan 22 '17 16:01 luqasz

Thanks, would you mind give a more realistic example so I can understand the needs better?

unknwon avatar Jan 23 '17 07:01 unknwon

https://github.com/ClearcodeHQ/Go-Forward/blob/master/config.go#L162 I'd like to add some parameters that will be uints. I was thinking of some kind of function that will call a function based on key name. Validation logic and default values can be delagated to that functions which will be implemented by user.

Example. Value of key size should be >= 0. If -1 is in file, then MapTo will silently ignore it and provide a 0. Thus I am not able to write an error message saying that value is invalid.

luqasz avatar Jan 23 '17 21:01 luqasz

I see, if I understand correctly, you're asking a kind of validation feature similar to form validation, that would be a bit more complex than I would expect originally... :trollface:

unknwon avatar Jan 23 '17 22:01 unknwon