mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-16 08:52:21 +08:00
41 lines
737 B
Go
41 lines
737 B
Go
|
package option
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
)
|
||
|
|
||
|
func ToMap(v any) (map[string]any, error) {
|
||
|
bytes, err := json.Marshal(v)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
var content map[string]any
|
||
|
err = json.Unmarshal(bytes, &content)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return content, nil
|
||
|
}
|
||
|
|
||
|
func MergeObjects(objects ...any) (map[string]any, error) {
|
||
|
content := make(map[string]any)
|
||
|
for _, object := range objects {
|
||
|
objectMap, err := ToMap(object)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
for k, v := range objectMap {
|
||
|
content[k] = v
|
||
|
}
|
||
|
}
|
||
|
return content, nil
|
||
|
}
|
||
|
|
||
|
func MarshallObjects(objects ...any) ([]byte, error) {
|
||
|
content, err := MergeObjects(objects...)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return json.Marshal(content)
|
||
|
}
|