github.com/cloudwego/hertz@v0.9.3/pkg/app/server/binding/reflect_internal_test.go (about)

     1  // Copyright 2023 CloudWeGo Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package binding
    17  
    18  import (
    19  	"reflect"
    20  	"testing"
    21  
    22  	"github.com/cloudwego/hertz/pkg/app/server/binding/internal/decoder"
    23  	"github.com/cloudwego/hertz/pkg/common/test/assert"
    24  )
    25  
    26  type foo2 struct {
    27  	F1 string
    28  }
    29  
    30  type fooq struct {
    31  	F1 **string
    32  }
    33  
    34  func Test_ReferenceValue(t *testing.T) {
    35  	foo1 := foo2{F1: "f1"}
    36  	foo1Val := reflect.ValueOf(foo1)
    37  	foo1PointerVal := decoder.ReferenceValue(foo1Val, 5)
    38  	assert.DeepEqual(t, "f1", foo1.F1)
    39  	assert.DeepEqual(t, "f1", foo1Val.Field(0).Interface().(string))
    40  	if foo1PointerVal.Kind() != reflect.Ptr {
    41  		t.Errorf("expect a pointer, but get nil")
    42  	}
    43  	assert.DeepEqual(t, "*****binding.foo2", foo1PointerVal.Type().String())
    44  
    45  	deFoo1PointerVal := decoder.ReferenceValue(foo1PointerVal, -5)
    46  	if deFoo1PointerVal.Kind() == reflect.Ptr {
    47  		t.Errorf("expect a non-pointer, but get a pointer")
    48  	}
    49  	assert.DeepEqual(t, "f1", deFoo1PointerVal.Field(0).Interface().(string))
    50  }
    51  
    52  func Test_GetNonNilReferenceValue(t *testing.T) {
    53  	foo1 := (****foo)(nil)
    54  	foo1Val := reflect.ValueOf(foo1)
    55  	foo1ValNonNil, ptrDepth := decoder.GetNonNilReferenceValue(foo1Val)
    56  	if !foo1ValNonNil.IsValid() {
    57  		t.Errorf("expect a valid value, but get nil")
    58  	}
    59  	if !foo1ValNonNil.CanSet() {
    60  		t.Errorf("expect can set value, but not")
    61  	}
    62  
    63  	foo1ReferPointer := decoder.ReferenceValue(foo1ValNonNil, ptrDepth)
    64  	if foo1ReferPointer.Kind() != reflect.Ptr {
    65  		t.Errorf("expect a pointer, but get nil")
    66  	}
    67  }
    68  
    69  func Test_GetFieldValue(t *testing.T) {
    70  	type bar struct {
    71  		B1 **fooq
    72  	}
    73  	bar1 := (***bar)(nil)
    74  	parentIdx := []int{0}
    75  	idx := 0
    76  
    77  	bar1Val := reflect.ValueOf(bar1)
    78  	parentFieldVal := decoder.GetFieldValue(bar1Val, parentIdx)
    79  	if parentFieldVal.Kind() == reflect.Ptr {
    80  		t.Errorf("expect a non-pointer, but get a pointer")
    81  	}
    82  	if !parentFieldVal.CanSet() {
    83  		t.Errorf("expect can set value, but not")
    84  	}
    85  	fooFieldVal := parentFieldVal.Field(idx)
    86  	assert.DeepEqual(t, "**string", fooFieldVal.Type().String())
    87  	if !fooFieldVal.CanSet() {
    88  		t.Errorf("expect can set value, but not")
    89  	}
    90  }