|
| 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