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