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

..03-May-2022-

.github/workflows/H12-Jan-2021-

CHANGELOG.mdH A D12-Jan-20212 KiB

LICENSEH A D12-Jan-20211.1 KiB

README.mdH A D12-Jan-20211.6 KiB

decode_hooks.goH A D12-Jan-20216.1 KiB

decode_hooks_test.goH A D12-Jan-20218.7 KiB

error.goH A D12-Jan-20211 KiB

go.modH A D12-Jan-202150

mapstructure.goH A D12-Jan-202141.5 KiB

mapstructure_benchmark_test.goH A D12-Jan-20215.6 KiB

mapstructure_bugs_test.goH A D12-Jan-202112.6 KiB

mapstructure_examples_test.goH A D12-Jan-20215.8 KiB

mapstructure_test.goH A D12-Jan-202149.4 KiB

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