Preserve comments between formatting

This commit is contained in:
世界 2026-04-28 08:10:05 +08:00
parent d9c9edd6f8
commit fd1624e426
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 18 additions and 3 deletions

View file

@ -38,10 +38,12 @@ func format() error {
return err
}
for _, optionsEntry := range optionsList {
comments := optionsEntry.options.Comments()
optionsEntry.options, err = badjson.Omitempty(globalCtx, optionsEntry.options)
if err != nil {
return err
}
optionsEntry.options.SetComments(comments)
buffer := new(bytes.Buffer)
encoder := json.NewEncoder(buffer)
encoder.SetIndent("", " ")

2
go.mod
View file

@ -37,7 +37,7 @@ require (
github.com/sagernet/gomobile v0.1.12
github.com/sagernet/gvisor v0.0.0-20250811.0-sing-box-mod.1
github.com/sagernet/quic-go v0.59.0-sing-box-mod.4
github.com/sagernet/sing v0.8.10-0.20260424005254-7b2d7ac5204c
github.com/sagernet/sing v0.8.10-0.20260427232324-f758c54b7409
github.com/sagernet/sing-cloudflared v0.1.0
github.com/sagernet/sing-mux v0.3.4
github.com/sagernet/sing-quic v0.6.2-0.20260412143638-8f65b6be7cd6

4
go.sum
View file

@ -242,8 +242,8 @@ github.com/sagernet/nftables v0.3.0-mod.2 h1:ck2KMU02OxL1eDFgGaWYglMDpoOZ7OHzxje
github.com/sagernet/nftables v0.3.0-mod.2/go.mod h1:8kslHG4VvYNihcco+i6uxIX7qbT8A56T0y5q7U44ZaQ=
github.com/sagernet/quic-go v0.59.0-sing-box-mod.4 h1:6qvrUW79S+CrPwWz6cMePXohgjHoKxLo3c+MDhNwc3o=
github.com/sagernet/quic-go v0.59.0-sing-box-mod.4/go.mod h1:OqILvS182CyOol5zNNo6bguvOGgXzV459+chpRaUC+4=
github.com/sagernet/sing v0.8.10-0.20260424005254-7b2d7ac5204c h1:hSVSiYyv3x0wNn38mnlOwoTwod+vW4XE251KG/uaA4U=
github.com/sagernet/sing v0.8.10-0.20260424005254-7b2d7ac5204c/go.mod h1:ARkL0gM13/Iv5VCZmci/NuoOlePoIsW0m7BWfln/Hak=
github.com/sagernet/sing v0.8.10-0.20260427232324-f758c54b7409 h1:TjqWXRWjiZi98SGX0Rmdl/XF7066+q3Idf+xjqJFF1U=
github.com/sagernet/sing v0.8.10-0.20260427232324-f758c54b7409/go.mod h1:olXxWQNqRW/l2Q6JI3b2Qmz8iQnIFlOeeH8bx6JhgUA=
github.com/sagernet/sing-cloudflared v0.1.0 h1:to+2fcCx8zu4X/DirRw9Ihc+FrEZ7oEyIqeCoJiwIpw=
github.com/sagernet/sing-cloudflared v0.1.0/go.mod h1:bH2NKX+NpDTY1Zkxfboxw6MXB/ZywaNLmrDJYgKMJ2Y=
github.com/sagernet/sing-mux v0.3.4 h1:ZQplKl8MNXutjzbMVtWvWG31fohhgOfCuUZR4dVQ8+s=

View file

@ -11,6 +11,7 @@ import (
type _Options struct {
RawMessage json.RawMessage `json:"-"`
CommentsSet *json.CommentSet `json:"-"`
Schema string `json:"$schema,omitempty"`
Log *LogOptions `json:"log,omitempty"`
DNS *DNSOptions `json:"dns,omitempty"`
@ -28,6 +29,10 @@ type _Options struct {
type Options _Options
func (o Options) MarshalJSONContext(ctx context.Context) ([]byte, error) {
return json.MarshalContext(ctx, (_Options)(o))
}
func (o *Options) UnmarshalJSONContext(ctx context.Context, content []byte) error {
decoder := json.NewDecoderContext(ctx, bytes.NewReader(content))
decoder.DisallowUnknownFields()
@ -39,6 +44,14 @@ func (o *Options) UnmarshalJSONContext(ctx context.Context, content []byte) erro
return checkOptions(o)
}
func (o Options) Comments() *json.CommentSet {
return o.CommentsSet
}
func (o *Options) SetComments(comments *json.CommentSet) {
o.CommentsSet = comments
}
type LogOptions struct {
Disabled bool `json:"disabled,omitempty"`
Level string `json:"level,omitempty"`