Skip to content

Commit 1b45936

Browse files
committed
doc.go
1 parent b2d367b commit 1b45936

File tree

2 files changed

+43
-49
lines changed

2 files changed

+43
-49
lines changed

Diff for: doc.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Package flat flattens a nested Golang map into a one level deep map. Flat also supports unflatten, turn a one level map into nested one.
2+
//
3+
// You can flatten a Go map
4+
//
5+
// in = map[string]interface{}{
6+
// "foo": map[string]interface{}{
7+
// "bar": map[string]interface{}{
8+
// "t": 123,
9+
// },
10+
// "k": 456,
11+
// },
12+
// }
13+
//
14+
// out, err := flat.Flatten(in, nil)
15+
// // out = map[string]interface{}{
16+
// // "foo.bar.t": 123,
17+
// // "foo.k": 456,
18+
// // }
19+
//
20+
// and a reverse with unflatten
21+
// in = map[string]interface{}{
22+
// "foo.bar.t": 123,
23+
// "foo.k": 456,
24+
// }
25+
// out, err := flat.Unflatten(in, nil)
26+
// // out = map[string]interface{}{
27+
// // "foo": map[string]interface{}{
28+
// // "bar": map[string]interface{}{
29+
// // "t": 123,
30+
// // },
31+
// // "k": 456,
32+
// // },
33+
// // }
34+
//
35+
package flat

Diff for: flat.go

+8-49
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,3 @@
1-
// Flat flattens a nested Golang map into a one level deep map. Flat also supports unflatten, turn a one level map into nested one.
2-
//
3-
// You can flatten a Go map
4-
//
5-
// in = map[string]interface{}{
6-
// "foo": map[string]interface{}{
7-
// "bar": map[string]interface{}{
8-
// "t": 123,
9-
// },
10-
// "k": 456,
11-
// },
12-
// }
13-
//
14-
// out, err := flat.Flatten(in, nil)
15-
// out = map[string]interface{}{
16-
// "foo.bar.t": 123,
17-
// "foo.k": 456,
18-
// }
19-
20-
// and a reverse with unflatten
21-
// in = map[string]interface{}{
22-
// "foo.bar.t": 123,
23-
// "foo.k": 456,
24-
// }
25-
// out, err := flat.Unflatten(in, nil)
26-
// out = map[string]interface{}{
27-
// "foo": map[string]interface{}{
28-
// "bar": map[string]interface{}{
29-
// "t": 123,
30-
// },
31-
// "k": 456,
32-
// },
33-
// }
34-
351
package flat
362

373
import (
@@ -42,20 +8,18 @@ import (
428
"github.com/imdario/mergo"
439
)
4410

45-
// Options the flatten options
46-
// by default
47-
// Demiliter: "."
48-
// Safe: false
49-
// MaxDepth: 20
11+
// Options the flatten options.
12+
// By default: Demiliter = "."
5013
type Options struct {
5114
Delimiter string
5215
Safe bool
53-
Object bool
5416
MaxDepth int
5517
}
5618

5719
// Flatten the map, it returns a map one level deep
58-
// regardless of how nested the original map was
20+
// regardless of how nested the original map was.
21+
// By default, the flatten has Delimiter = ".", and
22+
// no limitation of MaxDepth
5923
func Flatten(nested map[string]interface{}, opts *Options) (m map[string]interface{}, err error) {
6024
if opts == nil {
6125
opts = &Options{
@@ -121,16 +85,11 @@ func flatten(prefix string, depth int, nested interface{}, opts *Options) (flatm
12185
return
12286
}
12387

124-
// merge is the function that update to map with from and key
125-
// example: from is a map
88+
// update is the function that update to map with from
89+
// example:
12690
// to = {"hi": "there"}
12791
// from = {"foo": "bar"}
128-
// new to = {"hi": "there", "foo": "bar"}
129-
// example: from is an empty map
130-
// to = {"hi": "there"}
131-
// from = {}
132-
// key = "world"
133-
// key = {"hi": "there", "world": {}}
92+
// then, to = {"hi": "there", "foo": "bar"}
13493
func update(to map[string]interface{}, from map[string]interface{}) {
13594
for kt, vt := range from {
13695
to[kt] = vt

0 commit comments

Comments
 (0)