Improve configuration merge
This commit is contained in:
@@ -2,13 +2,12 @@ package option
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/common/json"
|
||||
)
|
||||
|
||||
type _Options struct {
|
||||
RawMessage json.RawMessage `json:"-"`
|
||||
Schema string `json:"$schema,omitempty"`
|
||||
Log *LogOptions `json:"log,omitempty"`
|
||||
DNS *DNSOptions `json:"dns,omitempty"`
|
||||
@@ -22,19 +21,14 @@ type _Options struct {
|
||||
type Options _Options
|
||||
|
||||
func (o *Options) UnmarshalJSON(content []byte) error {
|
||||
decoder := json.NewDecoder(json.NewCommentFilter(bytes.NewReader(content)))
|
||||
decoder := json.NewDecoder(bytes.NewReader(content))
|
||||
decoder.DisallowUnknownFields()
|
||||
err := decoder.Decode((*_Options)(o))
|
||||
if err == nil {
|
||||
return nil
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if syntaxError, isSyntaxError := err.(*json.SyntaxError); isSyntaxError {
|
||||
prefix := string(content[:syntaxError.Offset])
|
||||
row := strings.Count(prefix, "\n") + 1
|
||||
column := len(prefix) - strings.LastIndex(prefix, "\n") - 1
|
||||
return E.Extend(syntaxError, "row ", row, ", column ", column)
|
||||
}
|
||||
return err
|
||||
o.RawMessage = content
|
||||
return nil
|
||||
}
|
||||
|
||||
type LogOptions struct {
|
||||
|
||||
Reference in New Issue
Block a user