github.com/kaydxh/golang@v0.0.131/go/reflect/struct_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package reflect_test
    23  
    24  import (
    25  	"testing"
    26  
    27  	"github.com/google/uuid"
    28  	reflect_ "github.com/kaydxh/golang/go/reflect"
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func TestRetrieveStructField(t *testing.T) {
    33  	type HttpRequest struct {
    34  		RequestId string
    35  		Username  string
    36  	}
    37  
    38  	id := uuid.NewString()
    39  	req := &HttpRequest{
    40  		RequestId: id,
    41  	}
    42  
    43  	requestId := reflect_.RetrieveStructField(req, "RequestId")
    44  	t.Logf("requestId: %v", requestId)
    45  	assert.Equal(t, id, requestId)
    46  }
    47  
    48  func TestTrySetStructField(t *testing.T) {
    49  	type HttpRequest struct {
    50  		RequestId string
    51  		Username  string
    52  	}
    53  
    54  	id := uuid.NewString()
    55  	req := &HttpRequest{
    56  		//	RequestId: id,
    57  	}
    58  
    59  	reflect_.TrySetStructFiled(req, "RequestId", id)
    60  	t.Logf("requestId: %v", req.RequestId)
    61  	assert.Equal(t, id, req.RequestId)
    62  }
    63  
    64  func TestNonzeroFieldTags(t *testing.T) {
    65  	type HttpRequest struct {
    66  		RequestId string `db:"request_id"`
    67  		Username  string `db:"username"`
    68  	}
    69  
    70  	id := uuid.NewString()
    71  	req := &HttpRequest{
    72  		RequestId: id,
    73  		//	Username:  "username 1",
    74  	}
    75  	fields := reflect_.NonzeroFieldTags(req, "db")
    76  	t.Logf("fields: %v", fields)
    77  	assert.Equal(t, []string{"request_id"}, fields)
    78  }
    79  
    80  func TestAllFieldTags(t *testing.T) {
    81  	type HttpRequest struct {
    82  		RequestId string `db:"request_id"`
    83  		Username  string `db:"username"`
    84  	}
    85  
    86  	id := uuid.NewString()
    87  	req := HttpRequest{
    88  		RequestId: id,
    89  		//	Username:  "username 1",
    90  	}
    91  	fields := reflect_.AllFieldTags(req, "db")
    92  	t.Logf("fields: %v", fields)
    93  	//assert.Equal(t, []string{"request_id"}, fields)
    94  }
    95  
    96  func TestAllTagsValues(t *testing.T) {
    97  	type HttpRequest struct {
    98  		RequestId string `db:"request_id"`
    99  		Username  string `db:"username"`
   100  	}
   101  
   102  	id := uuid.NewString()
   103  	req := &HttpRequest{
   104  		RequestId: id,
   105  		Username:  "admin",
   106  	}
   107  	tagsValues := reflect_.AllTagsValues(req, "db")
   108  	t.Logf("tagsValues: %v", tagsValues)
   109  	//assert.Equal(t, []string{"request_id"}, fields)
   110  }