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

     1  package anyfn
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/gin-gonic/gin"
     7  )
     8  
     9  func singlePrimitiveValue(c *gin.Context, argIns []ArgIn) string {
    10  	if countPrimitiveArgs(argIns) != 1 { // nolint:gomnd
    11  		return ""
    12  	}
    13  
    14  	if len(c.Params) == 1 { // nolint:gomnd
    15  		return c.Params[0].Value
    16  	}
    17  
    18  	q := c.Request.URL.Query()
    19  	if len(q) == 1 { // nolint:gomnd
    20  		for _, v := range q {
    21  			return v[0]
    22  		}
    23  	}
    24  
    25  	return ""
    26  }
    27  
    28  func countPrimitiveArgs(argIns []ArgIn) int {
    29  	primitiveArgsNum := 0
    30  
    31  	for _, arg := range argIns {
    32  		switch arg.Kind {
    33  		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
    34  			reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
    35  			reflect.Bool,
    36  			reflect.String,
    37  			reflect.Float32, reflect.Float64:
    38  			primitiveArgsNum++
    39  		}
    40  	}
    41  
    42  	return primitiveArgsNum
    43  }