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

Allow types to determine how to decode themselves #22

Open
pcfreak30 opened this issue Jun 2, 2024 · 8 comments
Open

Allow types to determine how to decode themselves #22

pcfreak30 opened this issue Jun 2, 2024 · 8 comments
Labels
enhancement New feature or request

Comments

@pcfreak30
Copy link

Hello, I am re-posting this as an issue as it is an active need for me that is forcing me to fork out and apply PR's as patches to move forward.

Reference PR: mitchellh#294

PR Author @mthjs

Kudos.

@sagikazarmark
Copy link
Member

@pcfreak30 thanks for reposting the issue!

Looking at the original PR there seem to be some discussion around the details.

While we figure those out, would something like work for you: https://github.com/sagikazarmark/mapstructurex/blob/main/decode_hook_map_decoder.go

It's basically the same thing, implemented as a decode hook. You can tweak it to your own needs (I only needed to decode maps, but you could use it for any values).

@sagikazarmark sagikazarmark added the enhancement New feature or request label Jun 2, 2024
@pcfreak30
Copy link
Author

@sagikazarmark Don't think so. One of the barriers I was running into is the type test immediately after hook processing tries to parse it.

A use case is taking a string and processing it into a struct. For me that is a case of a UUID, and a word seed that gets converted to a Ed25519 key.

When its processed the mapper shouldn't try to be any more smart about it. See https://github.com/go-viper/mapstructure/blob/main/mapstructure.go#L477 https://github.com/go-viper/mapstructure/blob/main/mapstructure.go#L1292

@pcfreak30
Copy link
Author

Ive forked and used a variation of the PR LumeWeb@c63fee0

Using Unmarshaler as the interface name and moved it after hook decode as I agree you should be able to effectively filter the data, for more flexibility.

Though I think it could make sense to use support for Yaml Marshall, text unmarshall etc b/c of the standards already there.

@buzzdan
Copy link

buzzdan commented Jun 26, 2024

any ETA for this one?

@sagikazarmark
Copy link
Member

One of the barriers I was running into is the type test immediately after hook processing tries to parse it.

A use case is taking a string and processing it into a struct. For me that is a case of a UUID, and a word seed that gets converted to a Ed25519 key.

Forgive my ignorance, but could you provide a code sample that showcases what the problem is with the current decoder hook behaviour and why the above proposed solution wouldn't work in your case?

Thanks!

@pcfreak30
Copy link
Author

One of the barriers I was running into is the type test immediately after hook processing tries to parse it.
A use case is taking a string and processing it into a struct. For me that is a case of a UUID, and a word seed that gets converted to a Ed25519 key.

Forgive my ignorance, but could you provide a code sample that showcases what the problem is with the current decoder hook behaviour and why the above proposed solution wouldn't work in your case?

Thanks!

See https://github.com/LumeWeb/portal/blob/4cef4c5833fefc738f6c6d300da68ef87d6acdd2/config/types/identity.go#L64 and https://github.com/LumeWeb/portal/blob/develop/config/types/uuid.go#L34

Im using a forked version of the library atm for my needs. re-reading my past comment as well, One of the barriers I was running into is the type test immediately after hook processing tries to parse it. was a key issue.

I needed mapstructure to not try to be smart and just let me handle it.

See LumeWeb@c63fee0 to see the changes i made.

@git-hulk
Copy link

+1, this would be helpful for customizing struct decode behavior.

@pcfreak30
Copy link
Author

@sagikazarmark Hello, have you been able to spend any issue on this issue since august?

Would like to not need to maintain a fork 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants