From e746fcc41bf2ce8fe6d574e6522ed4d675b636bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 10 Dec 2023 22:57:28 +0800 Subject: [PATCH] Improve configuration merge --- cmd/sing-box/cmd_run.go | 10 ++++++++-- option/config.go | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cmd/sing-box/cmd_run.go b/cmd/sing-box/cmd_run.go index d76d3b9c..ba729472 100644 --- a/cmd/sing-box/cmd_run.go +++ b/cmd/sing-box/cmd_run.go @@ -17,6 +17,7 @@ import ( "github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/option" E "github.com/sagernet/sing/common/exceptions" + "github.com/sagernet/sing/common/json" "github.com/sagernet/sing/common/json/badjson" "github.com/spf13/cobra" @@ -107,13 +108,18 @@ func readConfigAndMerge() (option.Options, error) { if len(optionsList) == 1 { return optionsList[0].options, nil } - var mergedOptions option.Options + var mergedMessage json.RawMessage for _, options := range optionsList { - mergedOptions, err = badjson.Merge(options.options, mergedOptions) + mergedMessage, err = badjson.MergeJSON(options.options.RawMessage, mergedMessage) if err != nil { return option.Options{}, E.Cause(err, "merge config at ", options.path) } } + var mergedOptions option.Options + err = mergedOptions.UnmarshalJSON(mergedMessage) + if err != nil { + return option.Options{}, E.Cause(err, "unmarshal merged config") + } return mergedOptions, nil } diff --git a/option/config.go b/option/config.go index 3cec5520..a07173f2 100644 --- a/option/config.go +++ b/option/config.go @@ -9,6 +9,7 @@ import ( ) type _Options struct { + RawMessage json.RawMessage `json:"-"` Schema string `json:"$schema,omitempty"` Log *LogOptions `json:"log,omitempty"` DNS *DNSOptions `json:"dns,omitempty"` @@ -34,6 +35,7 @@ func (o *Options) UnmarshalJSON(content []byte) error { column := len(prefix) - strings.LastIndex(prefix, "\n") - 1 return E.Extend(syntaxError, "row ", row, ", column ", column) } + o.RawMessage = content return err }