github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/goo/example_test.go (about)

     1  package goo_test
     2  
     3  import (
     4  	"github.com/bingoohuang/gg/pkg/goo"
     5  )
     6  
     7  type MyInterface interface {
     8  	Method1(str string)
     9  	Method2(val int, str string)
    10  }
    11  
    12  type MyStruct struct {
    13  	Name  string
    14  	Price float32
    15  }
    16  
    17  func MyFunc(string, int, MyStruct) (string, error) {
    18  	return "test", nil
    19  }
    20  
    21  func ExampleTypeOf() {
    22  	fn := goo.TypeOf(MyFunc)
    23  	if fn.IsFunc() {
    24  		fnType := fn.ToFuncType()
    25  		outputs := fnType.Call([]interface{}{"test", 25, MyStruct{}})
    26  		if len(outputs) > 0 {
    27  			// ...
    28  		}
    29  	}
    30  
    31  	testStructType := goo.TypeOf(&MyStruct{})
    32  	if testStructType.IsStruct() {
    33  		structType := testStructType.ToStructType()
    34  		for _, method := range structType.Methods() {
    35  			method.Name()
    36  			// method.Invoke(...)
    37  			method.IsExported()
    38  			method.OutTypes()
    39  			method.OutNum()
    40  			method.InTypes()
    41  			method.InNum()
    42  		}
    43  
    44  		// ...
    45  		structType.FieldsExported()
    46  		for _, field := range structType.Fields() {
    47  			field.Name()
    48  			field.Type()
    49  			field.Get(&MyStruct{})
    50  			// field.Set(testStructInstance, nil)
    51  			field.Tags()
    52  			tag, err := field.TagByName("json")
    53  			if err == nil {
    54  				if tag.Value != "" && tag.Name != "" {
    55  					// ...
    56  				}
    57  			}
    58  		}
    59  	}
    60  
    61  	if testStructType.IsInstantiable() {
    62  		structType := testStructType.ToStructType()
    63  		newStructInstance := structType.New()
    64  		if newStructInstance != nil {
    65  			// ...
    66  		}
    67  	}
    68  
    69  	testInterfaceType := goo.TypeOf((*MyInterface)(nil))
    70  	if testInterfaceType.IsInterface() {
    71  		interfaceType := testInterfaceType.ToInterfaceType()
    72  		interfaceType.Methods()
    73  		// ...
    74  		interfaceType.MethodNum()
    75  	}
    76  
    77  	testSignedIntType := goo.TypeOf(25)
    78  	if testSignedIntType.IsNumber() {
    79  		numberType := testSignedIntType.ToNumberType()
    80  		if goo.IntType == numberType.Type() {
    81  			integerType := numberType.(goo.Integer)
    82  			if integerType.IsSigned() {
    83  				// ...
    84  			}
    85  		}
    86  	}
    87  
    88  	testFloat32Type := goo.TypeOf(float32(42.28))
    89  	if testFloat32Type.IsNumber() {
    90  		numberType := testFloat32Type.ToNumberType()
    91  		if goo.FloatType == numberType.Type() {
    92  			floatType := numberType.(goo.Float)
    93  			if goo.Bit32 == floatType.BitSize() {
    94  				// ...
    95  			}
    96  		}
    97  	}
    98  
    99  	testMapType := goo.TypeOf(make(map[string]bool, 0))
   100  	if testMapType.IsMap() {
   101  		mapType := testMapType.ToMapType()
   102  
   103  		keyType := mapType.KeyType()
   104  		if keyType.IsString() {
   105  			// ...
   106  		}
   107  
   108  		valueType := mapType.ValueType()
   109  		if valueType.IsBool() {
   110  			// ...
   111  		}
   112  	}
   113  
   114  	if testMapType.IsInstantiable() {
   115  		mapType := testMapType.ToMapType()
   116  		newMapInstance := mapType.New()
   117  		if newMapInstance != nil {
   118  			// ...
   119  		}
   120  	}
   121  
   122  	stringTestType := goo.TypeOf("test")
   123  	if stringTestType.IsString() {
   124  		stringType := stringTestType.ToStringType()
   125  		stringType.ToUint8("20")
   126  		stringType.ToUint64("58745")
   127  		stringType.ToInt8("-23")
   128  		stringType.ToUint64("9823")
   129  		stringType.ToFloat32("23.52")
   130  		stringType.ToFloat64("82387.32")
   131  	}
   132  
   133  	array := [5]string{}
   134  	testArrayType := goo.TypeOf(array)
   135  	if testArrayType.IsArray() {
   136  		arrayType := testArrayType.ToArrayType()
   137  		arrayType.ElemType()
   138  		// ...
   139  		arrayType.Len()
   140  	}
   141  
   142  	testSliceType := goo.TypeOf(array[2:])
   143  	if testSliceType.IsSlice() {
   144  		sliceType := testSliceType.ToSliceType()
   145  		sliceType.GetElementType()
   146  		// ...
   147  		sliceType.New()
   148  	}
   149  
   150  	// Output:
   151  }