-
Notifications
You must be signed in to change notification settings - Fork 17
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
feat(unmarshaler): add interface so that children nodes can define custom unmarshal behavior #11
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pkarakal thanks for the PR.
Before accepting it, I'd like to run through a couple questions (see comments) and a potential alternative solution:
Do you think this feature could be implemented as a decode hook?
I think it could be, in which case, it may be a better approach and may reduce complexity.
I've implemented something similar here a while ago: https://github.com/sagikazarmark/mapstructurex/blob/main/decode_hook_map_decoder.go
mapstructure.go
Outdated
@@ -301,6 +301,10 @@ type Metadata struct { | |||
Unset []string | |||
} | |||
|
|||
type Unmarshaler interface { | |||
UnmarshalMapStructure(interface{}) error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit-picking: can we brainstorm on this name a bit? It doesn't sit right with me right off the bat.
A couple ideas:
- UnmarshalMap (might be too generic)
- UnmarshalMapstructure (feels closer to the library name)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's go with UnmarshalMapstructure
. It matches the naming in some other places in the code
@@ -301,6 +301,10 @@ type Metadata struct { | |||
Unset []string | |||
} | |||
|
|||
type Unmarshaler interface { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this PR gets accepted as it is, I think it might make sense to document the behavior on the interface.
(Technically, the interface doesn't have to be exported, but it's a good place to document the feature)
return fmt.Errorf("error unmarshaling") | ||
} | ||
|
||
func ExampleDecode_structImplementsUnmarshalerInterface() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should add more sophisticated examples. Particularly for maps, named types, pointers, pointers of pointers, etc.
It can be turned into a decode hook for sure. What I mostly had in mind is having an interface that structs could implement to control the way they are decoded into a struct. This can definitely be controlled by a feature flag in the I believe that it would a good feature to have for anyone trying to manipulate a parsed config without having to write a custom decode hook. JSON and YAML packages already have a custom Take for example the following use case: I have a program that can have multiple data stores (e.g. a database, a message broker) that implement a common interface. To create those data stores, I have a Config interface which has a method that instantiate them and can have different fields. What I want to have is a global config that takes an array of those configs and based on the key I provide parse the decode the element as the correct config type to then instantiate the store. (Basically turn the YAML into the following struct, where the scheduleStores:
- storeName: rdb
postgres:
- host: "127.0.0.1"
port: 5432
user: "root"
password: "pass"
name: "postgres"
- storeName: amqp
rabbitmq:
- host: "127.0.0.1"
port: 5672
protocol: "amqp"
vhost: "/
username: "admin"
password: "admin"
exchange: "default
queue: "default" type ScheduleStoreConfig struct {
StoreName string `mapstructure:"storeName"`
ScheduleConfig store.Configs `mapstructure:"-,omitempty"`
}
type Config interface {
// Name returns the name of the store backend.
Name() string
// other methods
}
type PGConfig struct {
Port int `mapstructure:"port"`
DatabaseHost string `mapstructure:"host"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
DatabaseName string `mapstructure:"name"`
}
func (c *PGConfig) Name() string {
return "postgres"
}
type RMQConfig struct {
Port uint64 `mapstructure:"port"`
Protocol string `mapstructure:"protocol"`
Host string `mapstructure:"host"`
VHost string `mapstructure:"vhost"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Exchange string `mapstructure:"exchange"`
Queue string `mapstructure:"queue"`
}
func (c *RMQConfig) Name() string {
return "rabbitmq"
} On the PR comments, no strong feelings on the method name and I can definitely add documentation and more complex test cases. Just trying to see if what I'm describing is something you'd like in the library. |
I'm in favor of how it is implemented in this PR, rather than as decode hook:
Note also the discussion in #22. Seems 11 people were in favor of the original PR in mitchellh's repo, and 3 from this repo (@pkarakal, @pcfreak30, myself). @sagikazarmark As its been 2.5 years since that original PR, I'd prefer an executive decision be made one way or the other, rather than let this project go stale. Either:
|
Yes please, I really want to not need to be maintaining a fork b/c this/#22? hasn't been merged yet, its an unneeded maintenance burden for my project. |
I can surely update the PR and make any changes required, since maintaining a fork for this is also not something I fancy, just want the ok to proceed from the maintainers, so that the effort is not in vain |
Given the high demand for this feature, I'm willing to accept this change. Honestly, I still don't see any scenario where a decode hook couldn't be used to achieve the same (decode structs implementing an interface), but as others pointed out, it's a relatively simple change with a low risk of breaking anything. That being said, in order for this change to be accepted:
Lastly, I wonder if we should add a builtin error that signals mapstructure that the struct was not able to decode itself and the decoder should proceed with any decode hooks or builtin decoders. WDYT? (We can always add this later, but I just had this idea now) |
Cool thanks for that. Imma go ahead and make the required changes and will definitely add more test cases for that. I think it's also fine to have the custom error in case the unmarshaling fails so that it may proceed with the rest. This could also be a decoder configuration: if there is an error in one of the steps, proceed to the next step and join errors along the way. Having those separated I think would also be wise. WDYT? I'm fine either having both of these in this PR or splitting into two PRs. |
@pkarakal btw while I haven't looked please check LumeWeb@c63fee0 to see if what your doing is equiv to what I am b/c i had to modify the older pr due to a quirk and having the interface check run before |
…stom unmarshal behavior
673d723
to
18d5916
Compare
I renamed the function name to @pcfreak30 I think just the name of the interface and the method are different. Other than that the implementation looks the same to me. |
I just compared your latest push to what my fork has. LGTM. Don't think I will have issues with that. |
This adds the
Unmarshaler
interface where structs have to implement theUnmarshalMapStructure
method and then ondecoder.Decode
, this will trigger the custom unmarshal method. This will make sure thatmapstructure
is more in line with the rest of the marshaling libraries (likejson
andyaml
)