Skip to content

Commit 5220e08

Browse files
authored
Merge pull request #43 from rfyiamcool/docs/code
provide complete usage
2 parents 3f98836 + db193a3 commit 5220e08

File tree

2 files changed

+233
-21
lines changed

2 files changed

+233
-21
lines changed

README.md

Lines changed: 106 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,39 @@ Usage
3030
-----
3131

3232
```go
33+
package main
34+
35+
import (
36+
"encoding/json"
37+
"fmt"
38+
"math/rand"
39+
40+
"github.com/creasty/defaults"
41+
)
42+
3343
type Gender string
3444

3545
type Sample struct {
36-
Name string `default:"John Smith"`
37-
Age int `default:"27"`
38-
Gender Gender `default:"m"`
39-
40-
Slice []string `default:"[]"`
41-
SliceByJSON []int `default:"[1, 2, 3]"` // Supports JSON
42-
43-
Map map[string]int `default:"{}"`
44-
MapByJSON map[string]int `default:"{\"foo\": 123}"`
45-
MapOfStruct map[string]OtherStruct
46-
MapOfPtrStruct map[string]*OtherStruct
47-
MapOfStructWithTag map[string]OtherStruct `default:"{\"Key1\": {\"Foo\":123}}"`
48-
49-
Struct OtherStruct `default:"{}"`
46+
Name string `default:"John Smith"`
47+
Age int `default:"27"`
48+
Gender Gender `default:"m"`
49+
Working bool `default:"true"`
50+
51+
SliceInt []int `default:"[1, 2, 3]"`
52+
SlicePtr []*int `default:"[1, 2, 3]"`
53+
SliceString []string `default:"[\"a\", \"b\"]"`
54+
55+
MapNull map[string]int `default:"{}"`
56+
Map map[string]int `default:"{\"key1\": 123}"`
57+
MapOfStruct map[string]OtherStruct `default:"{\"Key2\": {\"Foo\":123}}"`
58+
MapOfPtrStruct map[string]*OtherStruct `default:"{\"Key3\": {\"Foo\":123}}"`
59+
MapOfStructWithTag map[string]OtherStruct `default:"{\"Key4\": {\"Foo\":123}}"`
60+
61+
Struct OtherStruct `default:"{\"Foo\": 123}"`
5062
StructPtr *OtherStruct `default:"{\"Foo\": 123}"`
5163

52-
NoTag OtherStruct // Recurses into a nested struct by default
53-
OptOut OtherStruct `default:"-"` // Opt-out
64+
NoTag OtherStruct // Recurses into a nested struct by default
65+
NoOption OtherStruct `default:"-"` // no option
5466
}
5567

5668
type OtherStruct struct {
@@ -65,11 +77,84 @@ func (s *OtherStruct) SetDefaults() {
6577
s.Random = rand.Int() // Set a dynamic value
6678
}
6779
}
68-
```
6980

70-
```go
71-
obj := &Sample{}
72-
if err := defaults.Set(obj); err != nil {
73-
panic(err)
81+
func main() {
82+
obj := &Sample{}
83+
if err := defaults.Set(obj); err != nil {
84+
panic(err)
85+
}
86+
87+
out, err := json.MarshalIndent(obj, "", " ")
88+
if err != nil {
89+
panic(err)
90+
}
91+
fmt.Println(string(out))
92+
93+
// Output:
94+
// {
95+
// "Name": "John Smith",
96+
// "Age": 27,
97+
// "Gender": "m",
98+
// "Working": true,
99+
// "SliceInt": [
100+
// 1,
101+
// 2,
102+
// 3
103+
// ],
104+
// "SlicePtr": [
105+
// 1,
106+
// 2,
107+
// 3
108+
// ],
109+
// "SliceString": [
110+
// "a",
111+
// "b"
112+
// ],
113+
// "MapNull": {},
114+
// "Map": {
115+
// "key1": 123
116+
// },
117+
// "MapOfStruct": {
118+
// "Key2": {
119+
// "Hello": "world",
120+
// "Foo": 123,
121+
// "Random": 5577006791947779410
122+
// }
123+
// },
124+
// "MapOfPtrStruct": {
125+
// "Key3": {
126+
// "Hello": "world",
127+
// "Foo": 123,
128+
// "Random": 8674665223082153551
129+
// }
130+
// },
131+
// "MapOfStructWithTag": {
132+
// "Key4": {
133+
// "Hello": "world",
134+
// "Foo": 123,
135+
// "Random": 6129484611666145821
136+
// }
137+
// },
138+
// "Struct": {
139+
// "Hello": "world",
140+
// "Foo": 123,
141+
// "Random": 4037200794235010051
142+
// },
143+
// "StructPtr": {
144+
// "Hello": "world",
145+
// "Foo": 123,
146+
// "Random": 3916589616287113937
147+
// },
148+
// "NoTag": {
149+
// "Hello": "world",
150+
// "Foo": 0,
151+
// "Random": 6334824724549167320
152+
// },
153+
// "NoOption": {
154+
// "Hello": "",
155+
// "Foo": 0,
156+
// "Random": 0
157+
// }
158+
// }
74159
}
75160
```

example/main.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"math/rand"
7+
8+
"github.com/creasty/defaults"
9+
)
10+
11+
type Gender string
12+
13+
type Sample struct {
14+
Name string `default:"John Smith"`
15+
Age int `default:"27"`
16+
Gender Gender `default:"m"`
17+
Working bool `default:"true"`
18+
19+
SliceInt []int `default:"[1, 2, 3]"`
20+
SlicePtr []*int `default:"[1, 2, 3]"`
21+
SliceString []string `default:"[\"a\", \"b\"]"`
22+
23+
MapNull map[string]int `default:"{}"`
24+
Map map[string]int `default:"{\"key1\": 123}"`
25+
MapOfStruct map[string]OtherStruct `default:"{\"Key2\": {\"Foo\":123}}"`
26+
MapOfPtrStruct map[string]*OtherStruct `default:"{\"Key3\": {\"Foo\":123}}"`
27+
MapOfStructWithTag map[string]OtherStruct `default:"{\"Key4\": {\"Foo\":123}}"`
28+
29+
Struct OtherStruct `default:"{\"Foo\": 123}"`
30+
StructPtr *OtherStruct `default:"{\"Foo\": 123}"`
31+
32+
NoTag OtherStruct // Recurses into a nested struct by default
33+
NoOption OtherStruct `default:"-"` // no option
34+
}
35+
36+
type OtherStruct struct {
37+
Hello string `default:"world"` // Tags in a nested struct also work
38+
Foo int `default:"-"`
39+
Random int `default:"-"`
40+
}
41+
42+
// SetDefaults implements defaults.Setter interface
43+
func (s *OtherStruct) SetDefaults() {
44+
if defaults.CanUpdate(s.Random) { // Check if it's a zero value (recommended)
45+
s.Random = rand.Int() // Set a dynamic value
46+
}
47+
}
48+
49+
func main() {
50+
obj := &Sample{}
51+
if err := defaults.Set(obj); err != nil {
52+
panic(err)
53+
}
54+
55+
out, err := json.MarshalIndent(obj, "", " ")
56+
if err != nil {
57+
panic(err)
58+
}
59+
fmt.Println(string(out))
60+
61+
// Output:
62+
// {
63+
// "Name": "John Smith",
64+
// "Age": 27,
65+
// "Gender": "m",
66+
// "Working": true,
67+
// "SliceInt": [
68+
// 1,
69+
// 2,
70+
// 3
71+
// ],
72+
// "SlicePtr": [
73+
// 1,
74+
// 2,
75+
// 3
76+
// ],
77+
// "SliceString": [
78+
// "a",
79+
// "b"
80+
// ],
81+
// "MapNull": {},
82+
// "Map": {
83+
// "key1": 123
84+
// },
85+
// "MapOfStruct": {
86+
// "Key2": {
87+
// "Hello": "world",
88+
// "Foo": 123,
89+
// "Random": 5577006791947779410
90+
// }
91+
// },
92+
// "MapOfPtrStruct": {
93+
// "Key3": {
94+
// "Hello": "world",
95+
// "Foo": 123,
96+
// "Random": 8674665223082153551
97+
// }
98+
// },
99+
// "MapOfStructWithTag": {
100+
// "Key4": {
101+
// "Hello": "world",
102+
// "Foo": 123,
103+
// "Random": 6129484611666145821
104+
// }
105+
// },
106+
// "Struct": {
107+
// "Hello": "world",
108+
// "Foo": 123,
109+
// "Random": 4037200794235010051
110+
// },
111+
// "StructPtr": {
112+
// "Hello": "world",
113+
// "Foo": 123,
114+
// "Random": 3916589616287113937
115+
// },
116+
// "NoTag": {
117+
// "Hello": "world",
118+
// "Foo": 0,
119+
// "Random": 6334824724549167320
120+
// },
121+
// "NoOption": {
122+
// "Hello": "",
123+
// "Foo": 0,
124+
// "Random": 0
125+
// }
126+
// }
127+
}

0 commit comments

Comments
 (0)