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

..03-May-2022-

.github/workflows/H26-Nov-2020-1918

CHANGELOG.mdH A D26-Nov-20201.9 KiB6943

LICENSEH A D26-Nov-20201.1 KiB2217

README.mdH A D26-Nov-20201.6 KiB4734

decode_hooks.goH A D26-Nov-20205.5 KiB233173

decode_hooks_test.goH A D26-Nov-20208 KiB422374

error.goH A D26-Nov-20201 KiB5138

go.modH A D26-Nov-202050 42

mapstructure.goH A D26-Nov-202040.9 KiB1,443896

mapstructure_benchmark_test.goH A D26-Nov-20205.6 KiB286240

mapstructure_bugs_test.goH A D26-Nov-202012.1 KiB570463

mapstructure_examples_test.goH A D26-Nov-20205.8 KiB257175

mapstructure_test.goH A D26-Nov-202049.9 KiB2,5152,116

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