11package defaults
22
33import (
4+ "encoding/json"
5+ "errors"
6+ "net"
47 "reflect"
8+ "strconv"
59 "testing"
610 "time"
711
@@ -112,9 +116,12 @@ type Sample struct {
112116 MyMap MyMap `default:"{}"`
113117 MySlice MySlice `default:"[]"`
114118
115- StructWithJSON Struct `default:"{\"Foo\": 123}"`
116- StructPtrWithJSON * Struct `default:"{\"Foo\": 123}"`
117- MapWithJSON map [string ]int `default:"{\"foo\": 123}"`
119+ StructWithText net.IP `default:"10.0.0.1"`
120+ StructPtrWithText * net.IP `default:"10.0.0.1"`
121+ StructWithJSON Struct `default:"{\"Foo\": 123}"`
122+ StructPtrWithJSON * Struct `default:"{\"Foo\": 123}"`
123+ MapWithJSON map [string ]int `default:"{\"foo\": 123}"`
124+ TypeWithUnmarshalJSON JSONOnlyType `default:"\"one\""`
118125
119126 MapOfPtrStruct map [string ]* Struct
120127 MapOfStruct map [string ]Struct
@@ -155,6 +162,24 @@ type Embedded struct {
155162 Int int `default:"1"`
156163}
157164
165+ type JSONOnlyType int
166+
167+ func (j * JSONOnlyType ) UnmarshalJSON (b []byte ) error {
168+ var tmp string
169+ if err := json .Unmarshal (b , & tmp ); err != nil {
170+ return err
171+ }
172+ if i , err := strconv .Atoi (tmp ); err == nil {
173+ * j = JSONOnlyType (i )
174+ return nil
175+ }
176+ if tmp == "one" {
177+ * j = 1
178+ return nil
179+ }
180+ return errors .New ("cannot unmarshal" )
181+ }
182+
158183func TestMustSet (t * testing.T ) {
159184
160185 t .Run ("right way" , func (t * testing.T ) {
@@ -485,6 +510,14 @@ func TestInit(t *testing.T) {
485510 }
486511 })
487512
513+ t .Run ("complex types with text unmarshal" , func (t * testing.T ) {
514+ if ! sample .StructWithText .Equal (net .ParseIP ("10.0.0.1" )) {
515+ t .Errorf ("it should initialize struct with text" )
516+ }
517+ if ! sample .StructPtrWithText .Equal (net .ParseIP ("10.0.0.1" )) {
518+ t .Errorf ("it should initialize struct with text" )
519+ }
520+ })
488521 t .Run ("complex types with json" , func (t * testing.T ) {
489522 if sample .StructWithJSON .Foo != 123 {
490523 t .Errorf ("it should initialize struct with json" )
@@ -499,6 +532,10 @@ func TestInit(t *testing.T) {
499532 t .Errorf ("it should initialize slice with json" )
500533 }
501534
535+ if int (sample .TypeWithUnmarshalJSON ) != 1 {
536+ t .Errorf ("it should initialize json unmarshaled value" )
537+ }
538+
502539 t .Run ("invalid json" , func (t * testing.T ) {
503540 if err := Set (& struct {
504541 I []int `default:"[!]"`
0 commit comments