Preserve comments between formatting

This commit is contained in:
世界 2026-04-28 08:10:05 +08:00
parent a37fcd59bb
commit 7d13cabb6a
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 15 additions and 0 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("", " ")

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"`