github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/ginx/cast/cast_test.go (about)

     1  package cast_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/bingoohuang/gg/pkg/ginx/cast"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestPopulate(t *testing.T) {
    13  	prop := map[string]string{
    14  		"key1":        "value1",
    15  		"key2":        "yes",
    16  		"key3":        "true",
    17  		"XingMing":    "kongrong",
    18  		"foo-bar":     "foobar",
    19  		"NI-HAO":      "10s",
    20  		"ta-hao":      "10s",
    21  		"ORDER_PRICE": "100",
    22  		"order_items": "10",
    23  		"HelloWorld":  "10",
    24  	}
    25  
    26  	type MySub2 struct {
    27  		XingMing string
    28  	}
    29  
    30  	type MySub struct {
    31  		Key1 string `prop:"key1"`
    32  		Key2 bool
    33  		Key3 *bool
    34  	}
    35  
    36  	type my struct {
    37  		MySub
    38  		*MySub2
    39  		FooBar     *string
    40  		NiHao      time.Duration
    41  		TaHao      *time.Duration
    42  		xx         string
    43  		YY         string
    44  		OrderPrice int
    45  		OrderItems int
    46  		HelloWorld *int
    47  	}
    48  
    49  	var (
    50  		m my
    51  		x int
    52  	)
    53  
    54  	it := assert.New(t)
    55  
    56  	err := cast.ToStruct(m, "prop", nil)
    57  	it.Error(err)
    58  
    59  	err = cast.ToStruct(&x, "prop", nil)
    60  	it.Error(err)
    61  
    62  	mapGetter := func(m map[string]string) func(filedName, tagValue string) (interface{}, bool) {
    63  		return func(filedName, tagValue string) (interface{}, bool) {
    64  			return cast.TryFind(filedName, tagValue, func(name string) (interface{}, bool) {
    65  				v, ok := m[name]
    66  				return v, ok
    67  			})
    68  		}
    69  	}
    70  
    71  	err = cast.ToStruct(&m, "prop", mapGetter(prop))
    72  	it.Nil(err)
    73  
    74  	foobar := "foobar"
    75  	HelloWorld := 10
    76  	key3 := true
    77  	taHao := 10 * time.Second
    78  
    79  	it.Equal(my{
    80  		MySub: MySub{
    81  			Key1: "value1",
    82  			Key2: true,
    83  			Key3: &key3,
    84  		},
    85  		MySub2: &MySub2{
    86  			XingMing: "kongrong",
    87  		},
    88  		FooBar:     &foobar,
    89  		NiHao:      10 * time.Second,
    90  		TaHao:      &taHao,
    91  		xx:         "",
    92  		YY:         "",
    93  		OrderPrice: 100,
    94  		OrderItems: 10,
    95  		HelloWorld: &HelloWorld,
    96  	}, m)
    97  
    98  	prop = map[string]string{
    99  		"NI-HAO":      "10x",
   100  		"ta-hao":      "10s",
   101  		"ORDER_PRICE": "100",
   102  		"order_items": "10",
   103  		"HelloWorld":  "10",
   104  	}
   105  
   106  	type Myx struct {
   107  		NiHao time.Duration
   108  	}
   109  
   110  	type Myy struct {
   111  		*Myx
   112  	}
   113  
   114  	var (
   115  		myx Myx
   116  		myy Myy
   117  	)
   118  
   119  	it.Error(cast.ToStruct(&myx, "prop", mapGetter(prop)))
   120  	it.Error(cast.ToStruct(&myy, "prop", mapGetter(prop)))
   121  }
   122  
   123  func TestCastAny(t *testing.T) {
   124  	type args struct {
   125  		s string
   126  		t reflect.Type
   127  	}
   128  
   129  	vTrue := true
   130  	tests := []struct {
   131  		name    string
   132  		args    args
   133  		want    reflect.Value
   134  		wantErr bool
   135  	}{
   136  		{
   137  			name:    "bool",
   138  			args:    args{"yes", reflect.TypeOf(false)},
   139  			want:    reflect.ValueOf(true),
   140  			wantErr: false,
   141  		},
   142  		{
   143  			name:    "*bool",
   144  			args:    args{"yes", reflect.PtrTo(reflect.TypeOf(false))},
   145  			want:    reflect.ValueOf(&vTrue),
   146  			wantErr: false,
   147  		},
   148  		{
   149  			name:    "bad bool",
   150  			args:    args{"bad", reflect.TypeOf(false)},
   151  			want:    cast.InvalidValue,
   152  			wantErr: true,
   153  		},
   154  	}
   155  
   156  	for _, tt := range tests {
   157  		tt := tt
   158  
   159  		t.Run(tt.name, func(t *testing.T) {
   160  			got, err := cast.To(tt.args.s, tt.args.t)
   161  			if (err != nil) != tt.wantErr {
   162  				t.Errorf("To() error = %v, wantErr %v", err, tt.wantErr)
   163  				return
   164  			}
   165  			if err != nil {
   166  				if got != cast.InvalidValue {
   167  					t.Errorf("To() got = %v, want %v", got, tt.want)
   168  				}
   169  
   170  				return
   171  			}
   172  
   173  			if !reflect.DeepEqual(got.Interface(), tt.want.Interface()) {
   174  				t.Errorf("To() got = %v, want %v", got, tt.want)
   175  			}
   176  		})
   177  	}
   178  }