Fix template extend

This commit is contained in:
世界 2024-06-02 00:54:56 +08:00
parent d83242c066
commit 3d81f1ad61
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4
2 changed files with 26 additions and 4 deletions

View File

@ -7,7 +7,8 @@ import (
"github.com/sagernet/sing/common/json" "github.com/sagernet/sing/common/json"
) )
type Template struct { type _Template struct {
RawMessage json.RawMessage `json:"-"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
Extend string `json:"extend,omitempty"` Extend string `json:"extend,omitempty"`
@ -72,6 +73,21 @@ type Template struct {
MemoryLimit option.MemoryBytes `json:"memory_limit,omitempty"` MemoryLimit option.MemoryBytes `json:"memory_limit,omitempty"`
} }
type Template _Template
func (t *Template) MarshalJSON() ([]byte, error) {
return json.Marshal((*_Template)(t))
}
func (t *Template) UnmarshalJSON(bytes []byte) error {
err := json.Unmarshal(bytes, (*_Template)(t))
if err != nil {
return err
}
t.RawMessage = bytes
return nil
}
type _RuleSet struct { type _RuleSet struct {
Type string `json:"type,omitempty"` Type string `json:"type,omitempty"`
DefaultOptions option.RuleSet `json:"-"` DefaultOptions option.RuleSet `json:"-"`

View File

@ -7,6 +7,7 @@ import (
"github.com/sagernet/serenity/option" "github.com/sagernet/serenity/option"
C "github.com/sagernet/sing-box/constant" C "github.com/sagernet/sing-box/constant"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/json"
"github.com/sagernet/sing/common/json/badjson" "github.com/sagernet/sing/common/json/badjson"
"github.com/sagernet/sing/common/logger" "github.com/sagernet/sing/common/logger"
) )
@ -40,10 +41,15 @@ func extendTemplate(rawTemplates []option.Template, root, current option.Templat
} }
next = newNext next = newNext
} }
newTemplate, err := badjson.Merge(next, current) newRawTemplate, err := badjson.MergeJSON(next.RawMessage, current.RawMessage)
if err != nil { if err != nil {
return option.Template{}, E.Cause(err, "initialize template[", current.Name, "]: merge extended template: ", current.Extend) return option.Template{}, E.Cause(err, "initialize template[", current.Name, "]: merge extended template: ", current.Extend)
} }
newTemplate, err := json.UnmarshalExtended[option.Template](newRawTemplate)
if err != nil {
return option.Template{}, E.Cause(err, "initialize template[", current.Name, "]: unmarshal extended template: ", current.Extend)
}
newTemplate.RawMessage = newRawTemplate
return newTemplate, nil return newTemplate, nil
} }