Skip to content

Commit

Permalink
Remove slices dependency. (#2930)
Browse files Browse the repository at this point in the history
* Remove slices dependency.

* Fix nil pointer dereference bug.

---------

Co-authored-by: nobody <[email protected]>
  • Loading branch information
vrnobody and nobody authored Jan 11, 2024
1 parent 2b08d86 commit 961cf9d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
47 changes: 25 additions & 22 deletions common/reflect/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package reflect
import (
"encoding/json"
"reflect"
"slices"

cserial "github.com/xtls/xray-core/common/serial"
)
Expand All @@ -18,6 +17,9 @@ func MarshalToJson(v interface{}) (string, bool) {
}

func marshalTypedMessage(v *cserial.TypedMessage, ignoreNullValue bool) interface{} {
if v == nil {
return nil
}
tmsg, err := v.GetInstance()
if err != nil {
return nil
Expand Down Expand Up @@ -111,28 +113,29 @@ func marshalKnownType(v interface{}, ignoreNullValue bool) (interface{}, bool) {
}
}

var valueKinds = []reflect.Kind{
reflect.Bool,
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr,
reflect.Float32,
reflect.Float64,
reflect.Complex64,
reflect.Complex128,
reflect.String,
}

func isValueKind(kind reflect.Kind) bool {
return slices.Contains(valueKinds, kind)
switch kind {
case reflect.Bool,
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Uintptr,
reflect.Float32,
reflect.Float64,
reflect.Complex64,
reflect.Complex128,
reflect.String:
return true
default:
return false
}
}

func marshalInterface(v interface{}, ignoreNullValue bool) interface{} {
Expand Down
10 changes: 6 additions & 4 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package core

import (
"io"
"slices"
"strings"

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

0 comments on commit 961cf9d

Please sign in to comment.