github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/conf/conf_test.go (about)

     1  package conf_test
     2  
     3  import (
     4  	"os"
     5  	"reflect"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/go-courier/ptr"
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"github.com/artisanhe/tools/conf"
    13  	"github.com/artisanhe/tools/conf/presets"
    14  	"github.com/artisanhe/tools/courier/transport_http/transform"
    15  )
    16  
    17  type E struct {
    18  	E int
    19  }
    20  
    21  func (e E) MarshalDefaults(v interface{}) {
    22  	if target, ok := v.(*E); ok {
    23  		if target.E == 0 {
    24  			target.E = 4
    25  		}
    26  	}
    27  }
    28  
    29  type D struct {
    30  	E
    31  	F float64
    32  	G string
    33  }
    34  
    35  type StructStruct struct {
    36  	IntPointer *int
    37  	A          int
    38  	B          *bool
    39  	C          uint
    40  	D          D
    41  	DP         *D
    42  	List       []string
    43  	Timeout    time.Duration
    44  }
    45  
    46  func TestUnmarshal(t *testing.T) {
    47  	config := &StructStruct{
    48  		A: 1,
    49  		B: ptr.Bool(false),
    50  		C: 2,
    51  		D: D{
    52  			E: E{},
    53  		},
    54  		DP: &D{
    55  			E: E{
    56  				E: 4,
    57  			},
    58  			F: 2.0,
    59  			G: "abc",
    60  		},
    61  		List: []string{"def"},
    62  	}
    63  
    64  	os.Setenv("TEST_INTPOINTER", "0")
    65  	os.Setenv("TEST_A", "2")
    66  	os.Setenv("TEST_B", "true")
    67  	os.Setenv("TEST_D_E", "5")
    68  	os.Setenv("TEST_DP_E", "5")
    69  	os.Setenv("TEST_LIST", "gds,123")
    70  	os.Setenv("TEST_TIMEOUT", "5s")
    71  
    72  	ok, errMsgs := conf.NewScanner("TEST").Unmarshal(reflect.ValueOf(config), reflect.TypeOf(config))
    73  	if !ok {
    74  		t.Log(errMsgs)
    75  	}
    76  
    77  	assert.Equal(t, &StructStruct{
    78  		A: 2,
    79  		B: ptr.Bool(true),
    80  		C: 2,
    81  		D: D{
    82  			E: E{
    83  				E: 5,
    84  			},
    85  		},
    86  		DP: &D{
    87  			E: E{
    88  				E: 5,
    89  			},
    90  			F: 2.0,
    91  			G: "abc",
    92  		},
    93  		IntPointer: ptr.Int(0),
    94  		List:       []string{"gds", "123"},
    95  		Timeout:    5 * time.Second,
    96  	}, config)
    97  }
    98  
    99  func TestUnmarshalWithValidate(t *testing.T) {
   100  	tt := assert.New(t)
   101  
   102  	type Struct struct {
   103  		StringRange string           `validate:"@string[10,)"`
   104  		Hostname    string           `validate:"@hostname"`
   105  		Password    presets.Password `validate:"@string[1,)"`
   106  		Struct      struct {
   107  			Int32 int32 `validate:"@int32[2,)"`
   108  		}
   109  	}
   110  
   111  	config := &Struct{}
   112  	config.StringRange = "string"
   113  	config.Struct.Int32 = 1
   114  
   115  	ok, errMsgs := conf.NewScanner("TEST").Unmarshal(reflect.ValueOf(config), reflect.TypeOf(config))
   116  	tt.False(ok)
   117  
   118  	if !ok {
   119  		t.Log(errMsgs)
   120  	}
   121  
   122  	assert.Equal(t, transform.ErrMsgMap{
   123  		"Hostname":     "Hostname 类型值错误",
   124  		"Password":     "字符串长度不在[1, 1024]范围内,当前长度:0",
   125  		"StringRange":  "字符串长度不在[10, 1024]范围内,当前长度:6",
   126  		"Struct.Int32": "整形值不在[2, 2147483647)范围内,当前值:1",
   127  	}, errMsgs)
   128  }