caddyhttp: refactor to use reflect.TypeFor (#7187)

This commit is contained in:
cui 2025-08-19 07:08:46 +08:00 committed by GitHub
parent 05acc5131e
commit b15ed9b084
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 12 deletions

View File

@ -20,7 +20,6 @@ import (
"net"
"net/http"
"net/netip"
"reflect"
"strings"
"github.com/google/cel-go/cel"
@ -109,7 +108,7 @@ func (MatchRemoteIP) CELLibrary(ctx caddy.Context) (cel.Library, error) {
[]*cel.Type{cel.ListType(cel.StringType)},
// function to convert a constant list of strings to a MatchPath instance.
func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{})
refStringList := stringSliceType
strList, err := data.ConvertToNative(refStringList)
if err != nil {
return nil, err
@ -222,7 +221,7 @@ func (MatchClientIP) CELLibrary(ctx caddy.Context) (cel.Library, error) {
[]*cel.Type{cel.ListType(cel.StringType)},
// function to convert a constant list of strings to a MatchPath instance.
func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{})
refStringList := stringSliceType
strList, err := data.ConvertToNative(refStringList)
if err != nil {
return nil, err

View File

@ -23,7 +23,6 @@ import (
"net/textproto"
"net/url"
"path"
"reflect"
"regexp"
"runtime"
"slices"
@ -373,7 +372,7 @@ func (MatchHost) CELLibrary(ctx caddy.Context) (cel.Library, error) {
"host_match_request_list",
[]*cel.Type{cel.ListType(cel.StringType)},
func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{})
refStringList := stringSliceType
strList, err := data.ConvertToNative(refStringList)
if err != nil {
return nil, err
@ -654,7 +653,7 @@ func (MatchPath) CELLibrary(ctx caddy.Context) (cel.Library, error) {
[]*cel.Type{cel.ListType(cel.StringType)},
// function to convert a constant list of strings to a MatchPath instance.
func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{})
refStringList := stringSliceType
strList, err := data.ConvertToNative(refStringList)
if err != nil {
return nil, err
@ -733,7 +732,7 @@ func (MatchPathRE) CELLibrary(ctx caddy.Context) (cel.Library, error) {
"path_regexp_request_string_string",
[]*cel.Type{cel.StringType, cel.StringType},
func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{})
refStringList := stringSliceType
params, err := data.ConvertToNative(refStringList)
if err != nil {
return nil, err
@ -802,7 +801,7 @@ func (MatchMethod) CELLibrary(_ caddy.Context) (cel.Library, error) {
"method_request_list",
[]*cel.Type{cel.ListType(cel.StringType)},
func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{})
refStringList := stringSliceType
strList, err := data.ConvertToNative(refStringList)
if err != nil {
return nil, err
@ -1173,7 +1172,7 @@ func (MatchHeaderRE) CELLibrary(ctx caddy.Context) (cel.Library, error) {
"header_regexp_request_string_string",
[]*cel.Type{cel.StringType, cel.StringType},
func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{})
refStringList := stringSliceType
params, err := data.ConvertToNative(refStringList)
if err != nil {
return nil, err
@ -1196,7 +1195,7 @@ func (MatchHeaderRE) CELLibrary(ctx caddy.Context) (cel.Library, error) {
"header_regexp_request_string_string_string",
[]*cel.Type{cel.StringType, cel.StringType, cel.StringType},
func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{})
refStringList := stringSliceType
params, err := data.ConvertToNative(refStringList)
if err != nil {
return nil, err

View File

@ -28,6 +28,8 @@ import (
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
)
var stringSliceType = reflect.TypeFor[[]string]()
func init() {
caddy.RegisterModule(VarsMiddleware{})
caddy.RegisterModule(VarsMatcher{})
@ -353,7 +355,7 @@ func (MatchVarsRE) CELLibrary(ctx caddy.Context) (cel.Library, error) {
"vars_regexp_request_string_string",
[]*cel.Type{cel.StringType, cel.StringType},
func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{})
refStringList := stringSliceType
params, err := data.ConvertToNative(refStringList)
if err != nil {
return nil, err
@ -376,7 +378,7 @@ func (MatchVarsRE) CELLibrary(ctx caddy.Context) (cel.Library, error) {
"vars_regexp_request_string_string_string",
[]*cel.Type{cel.StringType, cel.StringType, cel.StringType},
func(data ref.Val) (RequestMatcherWithError, error) {
refStringList := reflect.TypeOf([]string{})
refStringList := stringSliceType
params, err := data.ConvertToNative(refStringList)
if err != nil {
return nil, err