• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.travis.ymlH A D05-Oct-201860 96

CHANGELOG.mdH A D05-Oct-2018625 2214

LICENSEH A D05-Oct-20181.1 KiB2217

README.mdH A D05-Oct-20181.6 KiB4734

decode_hooks.goH A D05-Oct-20185.1 KiB218161

decode_hooks_test.goH A D05-Oct-20186.4 KiB324287

error.goH A D05-Oct-20181 KiB5138

go.modH A D05-Oct-201841 21

mapstructure.goH A D05-Oct-201831.7 KiB1,150808

mapstructure_benchmark_test.goH A D05-Oct-20185.3 KiB280234

mapstructure_bugs_test.goH A D05-Oct-20189.7 KiB461379

mapstructure_examples_test.goH A D05-Oct-20184.6 KiB204136

mapstructure_test.goH A D05-Oct-201838.2 KiB1,9801,655

README.md

1# mapstructure [![Godoc](https://godoc.org/github.com/mitchellh/mapstructure?status.svg)](https://godoc.org/github.com/mitchellh/mapstructure)
2
3mapstructure is a Go library for decoding generic map values to structures
4and vice versa, while providing helpful error handling.
5
6This library is most useful when decoding values from some data stream (JSON,
7Gob, etc.) where you don't _quite_ know the structure of the underlying data
8until you read a part of it. You can therefore read a `map[string]interface{}`
9and use this library to decode it into the proper underlying native Go
10structure.
11
12## Installation
13
14Standard `go get`:
15
16```
17$ go get github.com/mitchellh/mapstructure
18```
19
20## Usage & Example
21
22For usage and examples see the [Godoc](http://godoc.org/github.com/mitchellh/mapstructure).
23
24The `Decode` function has examples associated with it there.
25
26## But Why?!
27
28Go offers fantastic standard libraries for decoding formats such as JSON.
29The standard method is to have a struct pre-created, and populate that struct
30from the bytes of the encoded format. This is great, but the problem is if
31you have configuration or an encoding that changes slightly depending on
32specific fields. For example, consider this JSON:
33
34```json
35{
36  "type": "person",
37  "name": "Mitchell"
38}
39```
40
41Perhaps we can't populate a specific structure without first reading
42the "type" field from the JSON. We could always do two passes over the
43decoding of the JSON (reading the "type" first, and the rest later).
44However, it is much simpler to just decode this into a `map[string]interface{}`
45structure, read the "type" key, then use something like this library
46to decode it into the proper structure.
47