2022-07-03 01:57:04 +08:00
|
|
|
package option
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-07-03 03:42:57 +08:00
|
|
|
|
|
|
|
"github.com/sagernet/sing/common/x/linkedhashmap"
|
2022-07-03 01:57:04 +08:00
|
|
|
)
|
|
|
|
|
2022-07-03 03:42:57 +08:00
|
|
|
func ToMap(v any) (*linkedhashmap.Map[string, any], error) {
|
2022-07-03 01:57:04 +08:00
|
|
|
bytes, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-03 03:42:57 +08:00
|
|
|
var content linkedhashmap.Map[string, any]
|
2022-07-03 01:57:04 +08:00
|
|
|
err = json.Unmarshal(bytes, &content)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-03 03:42:57 +08:00
|
|
|
return &content, nil
|
2022-07-03 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
2022-07-03 03:42:57 +08:00
|
|
|
func MergeObjects(objects ...any) (*linkedhashmap.Map[string, any], error) {
|
|
|
|
var content linkedhashmap.Map[string, any]
|
2022-07-03 01:57:04 +08:00
|
|
|
for _, object := range objects {
|
|
|
|
objectMap, err := ToMap(object)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-03 03:42:57 +08:00
|
|
|
content.PutAll(objectMap)
|
2022-07-03 01:57:04 +08:00
|
|
|
}
|
2022-07-03 03:42:57 +08:00
|
|
|
return &content, nil
|
2022-07-03 01:57:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func MarshallObjects(objects ...any) ([]byte, error) {
|
|
|
|
content, err := MergeObjects(objects...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return json.Marshal(content)
|
|
|
|
}
|