From 5afb06bdce6f535e0bbdc4fc555781371854cc76 Mon Sep 17 00:00:00 2001 From: Nite07 Date: Mon, 5 Aug 2024 01:53:16 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Improve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/vmess.go | 30 +++++++++---------- util/marshal.go | 25 ---------------- util/merge.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 40 deletions(-) delete mode 100644 util/marshal.go create mode 100644 util/merge.go diff --git a/model/vmess.go b/model/vmess.go index 2063987..a915b3b 100644 --- a/model/vmess.go +++ b/model/vmess.go @@ -1,21 +1,21 @@ package model type VmessJson struct { - V string `json:"v"` - Ps string `json:"ps"` - Add string `json:"add"` - Port interface{} `json:"port"` - Id string `json:"id"` - Aid interface{} `json:"aid"` - Scy string `json:"scy"` - Net string `json:"net"` - Type string `json:"type"` - Host string `json:"host"` - Path string `json:"path"` - Tls string `json:"tls"` - Sni string `json:"sni"` - Alpn string `json:"alpn"` - Fp string `json:"fp"` + V string `json:"v"` + Ps string `json:"ps"` + Add string `json:"add"` + Port any `json:"port"` + Id string `json:"id"` + Aid any `json:"aid"` + Scy string `json:"scy"` + Net string `json:"net"` + Type string `json:"type"` + Host string `json:"host"` + Path string `json:"path"` + Tls string `json:"tls"` + Sni string `json:"sni"` + Alpn string `json:"alpn"` + Fp string `json:"fp"` } type VMessOutboundOptions struct { diff --git a/util/marshal.go b/util/marshal.go deleted file mode 100644 index 389d8b3..0000000 --- a/util/marshal.go +++ /dev/null @@ -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 -} diff --git a/util/merge.go b/util/merge.go new file mode 100644 index 0000000..562a5b7 --- /dev/null +++ b/util/merge.go @@ -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 +}