Remove slices dependency. (#2930)

* Remove slices dependency.

* Fix nil pointer dereference bug.

---------

Co-authored-by: nobody <nobody@nowhere.mars>
This commit is contained in:
nobody 2024-01-11 23:34:26 +08:00 committed by GitHub
parent 2b08d8638e
commit 961cf9d3b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 26 deletions

View File

@ -3,7 +3,6 @@ package reflect
import ( import (
"encoding/json" "encoding/json"
"reflect" "reflect"
"slices"
cserial "github.com/xtls/xray-core/common/serial" cserial "github.com/xtls/xray-core/common/serial"
) )
@ -18,6 +17,9 @@ func MarshalToJson(v interface{}) (string, bool) {
} }
func marshalTypedMessage(v *cserial.TypedMessage, ignoreNullValue bool) interface{} { func marshalTypedMessage(v *cserial.TypedMessage, ignoreNullValue bool) interface{} {
if v == nil {
return nil
}
tmsg, err := v.GetInstance() tmsg, err := v.GetInstance()
if err != nil { if err != nil {
return nil return nil
@ -111,8 +113,9 @@ func marshalKnownType(v interface{}, ignoreNullValue bool) (interface{}, bool) {
} }
} }
var valueKinds = []reflect.Kind{ func isValueKind(kind reflect.Kind) bool {
reflect.Bool, switch kind {
case reflect.Bool,
reflect.Int, reflect.Int,
reflect.Int8, reflect.Int8,
reflect.Int16, reflect.Int16,
@ -128,11 +131,11 @@ var valueKinds = []reflect.Kind{
reflect.Float64, reflect.Float64,
reflect.Complex64, reflect.Complex64,
reflect.Complex128, reflect.Complex128,
reflect.String, reflect.String:
} return true
default:
func isValueKind(kind reflect.Kind) bool { return false
return slices.Contains(valueKinds, kind) }
} }
func marshalInterface(v interface{}, ignoreNullValue bool) interface{} { func marshalInterface(v interface{}, ignoreNullValue bool) interface{} {

View File

@ -2,7 +2,6 @@ package core
import ( import (
"io" "io"
"slices"
"strings" "strings"
"github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common"
@ -60,9 +59,12 @@ func GetMergedConfig(args cmdarg.Arg) (string, error) {
supported := []string{"json", "yaml", "toml"} supported := []string{"json", "yaml", "toml"}
for _, file := range args { for _, file := range args {
format := getFormat(file) format := getFormat(file)
if slices.Contains(supported, format) { for _, s := range supported {
if s == format {
files = append(files, file) files = append(files, file)
formats = append(formats, format) formats = append(formats, format)
break
}
} }
} }
return ConfigMergedFormFiles(files, formats) return ConfigMergedFormFiles(files, formats)