1
0
mirror of https://github.com/nitezs/sub2sing-box.git synced 2024-12-23 21:34:41 -05:00

️ Improve

This commit is contained in:
Nite07 2024-08-05 01:53:16 +08:00
parent 5395417a31
commit 5afb06bdce
3 changed files with 93 additions and 40 deletions

View File

@ -4,9 +4,9 @@ type VmessJson struct {
V string `json:"v"` V string `json:"v"`
Ps string `json:"ps"` Ps string `json:"ps"`
Add string `json:"add"` Add string `json:"add"`
Port interface{} `json:"port"` Port any `json:"port"`
Id string `json:"id"` Id string `json:"id"`
Aid interface{} `json:"aid"` Aid any `json:"aid"`
Scy string `json:"scy"` Scy string `json:"scy"`
Net string `json:"net"` Net string `json:"net"`
Type string `json:"type"` Type string `json:"type"`

View File

@ -1,25 +0,0 @@
package util
import "encoding/json"
func MergeAndMarshal(args ...interface{}) (string, error) {
merged := make(map[string]interface{})
for _, arg := range args {
jsonBytes, err := json.Marshal(arg)
if err != nil {
return "", err
}
var m map[string]interface{}
if err := json.Unmarshal(jsonBytes, &m); err != nil {
return "", err
}
for k, v := range m {
merged[k] = v
}
}
result, err := json.Marshal(merged)
if err != nil {
return "", err
}
return string(result), nil
}

78
util/merge.go Normal file
View File

@ -0,0 +1,78 @@
package util
import (
"encoding/json"
"fmt"
"reflect"
)
func MergeAndMarshal(s1, s2 any) (string, error) {
merged, err := mergeStructs(s1, s2)
if err != nil {
return "", err
}
jsonBytes, err := json.Marshal(merged)
if err != nil {
return "", err
}
return string(jsonBytes), nil
}
func mergeStructs(s1, s2 any) (any, error) {
v1 := reflect.ValueOf(s1)
v2 := reflect.ValueOf(s2)
if v1.Kind() == reflect.Pointer {
v1 = v1.Elem()
}
if v2.Kind() == reflect.Pointer {
v2 = v2.Elem()
}
if v1.Kind() != reflect.Struct || v2.Kind() != reflect.Struct {
return nil, fmt.Errorf("both arguments must be structs")
}
t1 := v1.Type()
t2 := v2.Type()
var fields []reflect.StructField
var fieldsSet = make(map[string]reflect.Type)
for i := 0; i < t1.NumField(); i++ {
field := t1.Field(i)
fields = append(fields, field)
fieldsSet[field.Name] = field.Type
}
for i := 0; i < t2.NumField(); i++ {
field := t2.Field(i)
if existingType, ok := fieldsSet[field.Name]; ok {
if existingType != field.Type {
return nil, fmt.Errorf("field %s has conflicting types: %s and %s", field.Name, existingType, field.Type)
}
} else {
fields = append(fields, field)
fieldsSet[field.Name] = field.Type
}
}
newType := reflect.StructOf(fields)
newValue := reflect.New(newType).Elem()
for i := 0; i < t1.NumField(); i++ {
valueField := newValue.FieldByName(t1.Field(i).Name)
if valueField.IsValid() {
valueField.Set(v1.Field(i))
}
}
for i := 0; i < t2.NumField(); i++ {
valueField := newValue.FieldByName(t2.Field(i).Name)
if valueField.IsValid() {
valueField.Set(v2.Field(i))
}
}
return newValue.Interface(), nil
}