merge-fields-recursive

command module
v0.0.0-...-838b099 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 3, 2023 License: MIT Imports: 4 Imported by: 0

README

Merge Matching Fields Recursively from Map to Struct

Suppose we have a map[string]any. We want to copy the content of that map to a struct if the keys in the map matches the tags in the struct.

This example is an extension of the simpler example of just mapping non-nested fields in a struct. This example traverse through all nested fields in the map and struct value.

Note

Reflection is tricky business as we attempt to find out the type metadata of an object at runtime. So we can't fully rely on static types and the compiler to do the checking for us. There are many edge cases that are not captured in this example as we want to focus on the pertinent code on reflection. Please don't just copy the example without understanding what the code and include additional checks and logic for handling edge cases.

Setup

  1. Run the program

    $ make run
    

Credits

Reference

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL