gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/config/handler_test.go (about)

     1  package config
     2  
     3  import (
     4  	"gitee.com/sy_183/go-common/unit"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  type A struct {
    10  	S  string        `default:"hello"`
    11  	I  int           `default:"-1"`
    12  	U  uint          `default:"1"`
    13  	F  float64       `default:"3.14"`
    14  	B  bool          `default:"true"`
    15  	T  time.Time     `default:"1998-07-13 00:00:00" timeLayer:"2006-01-02 15:04:05"`
    16  	D  time.Duration `default:"1s"`
    17  	UN unit.Size     `default:"1M"`
    18  	SP *string
    19  	M  map[string]any  `default:"{username: admin, password: admin@123}"`
    20  	SS []string        `default:"[192.168.1.11, 192.168.1.12]"`
    21  	SA [3]string       `default:"[test, user1, user2]"`
    22  	MP *map[string]any `default:"{}"`
    23  	ST struct {
    24  		S  string         `yaml:"s" default:"ttt"`
    25  		SP *string        `yaml:"sp"`
    26  		M  map[string]any `yaml:"m"`
    27  		SS []string       `yaml:"ss"`
    28  	} `default:"{s: tt, sp: ll, m: , ss: [1, 2, 3]}"`
    29  	STP *struct {
    30  		S  string
    31  		SP *string `default:"ming"`
    32  		M  map[string]any
    33  		SS []string
    34  	} `default:""`
    35  }
    36  
    37  func TestHandleA(t *testing.T) {
    38  	a := new(A)
    39  	err := Handle(a)
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  }
    44  
    45  type C struct {
    46  	S  string
    47  	SP *string
    48  	M  map[string]any
    49  	Mp *map[string]any
    50  }
    51  
    52  func (c C) PreModify() (nc any, modified bool) {
    53  	return C{S: "123"}, true
    54  }
    55  
    56  type D struct {
    57  	S  string
    58  	SP *string
    59  	M  map[string]any
    60  	Mp *map[string]any
    61  }
    62  
    63  func (d *D) PreModify() (nc any, modified bool) {
    64  	d.S = "123"
    65  	return d, true
    66  }
    67  
    68  func (d *D) PreHandle() {
    69  	s := "12345"
    70  	d.SP = &s
    71  }
    72  
    73  type B struct {
    74  	S  string
    75  	SP *string
    76  	M  map[string]C  `default:"{a: , b: }"`
    77  	Mp *map[string]D `default:"{a: , b: }"`
    78  	C  C
    79  	CP *C `default:""`
    80  	D  D
    81  	DP *D `default:""`
    82  }
    83  
    84  func TestHandleB(t *testing.T) {
    85  	b := new(B)
    86  	err := Handle(b)
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  }