Assisted-by: GLM 5.3 Flash
This commit is contained in:
+28
-16
@@ -545,12 +545,25 @@ func MarshalContext(ctx context.Context, v any) ([]byte, error) {
|
||||
return NewEncoder().MarshalContext(ctx, v)
|
||||
}
|
||||
|
||||
// A LayoutKind names the layout the encoder writes a document's entries in.
|
||||
type LayoutKind int
|
||||
|
||||
const (
|
||||
// LayoutKindGrouped reorders entries at one level: scalars first, then
|
||||
// sub-tables, then arrays of tables. The default.
|
||||
LayoutKindGrouped LayoutKind = iota
|
||||
// LayoutKindDeclaration preserves the declaration order: struct field
|
||||
// order, or sorted key order for maps.
|
||||
LayoutKindDeclaration
|
||||
)
|
||||
|
||||
// An Encoder encodes Go values into TOML.
|
||||
//
|
||||
// All options default to the behaviour that passes the toml-test compliance
|
||||
// suite in both directions:
|
||||
//
|
||||
// GroupByKind: true (scalars first, then tables, then arrays of tables)
|
||||
// Layout: LayoutKindGrouped (scalars first, then tables, then
|
||||
// arrays of tables)
|
||||
// OmitEmptyArrays: false (a nil/empty []string slice emits [] as a value;
|
||||
// a nil/empty []Item struct slice is still skipped)
|
||||
// LiteralMultilineAt: 0 (always emit the escaped basic form, never a
|
||||
@@ -562,22 +575,21 @@ func MarshalContext(ctx context.Context, v any) ([]byte, error) {
|
||||
// callers that need the underlying knobs reach for the methods rather than
|
||||
// reading or mutating fields.
|
||||
type Encoder struct {
|
||||
groupByKind bool // default true; set via (*Encoder).GroupByKind
|
||||
omitEmptyArrays bool // default false; set via (*Encoder).OmitEmptyArrays
|
||||
literalMultilineAt int // default 0; set via (*Encoder).UseLiteralMultiline
|
||||
inlineTablesAt int // default 0; set via (*Encoder).InlineTables
|
||||
emitFieldComments bool // default false; set via (*Encoder).EmitFieldComments
|
||||
layout LayoutKind // default LayoutKindGrouped; set via (*Encoder).Layout
|
||||
omitEmptyArrays bool // default false; set via (*Encoder).OmitEmptyArrays
|
||||
literalMultilineAt int // default 0; set via (*Encoder).LiteralMultiline
|
||||
inlineTablesAt int // default 0; set via (*Encoder).InlineTables
|
||||
emitFieldComments bool // default false; set via (*Encoder).EmitFieldComments
|
||||
}
|
||||
|
||||
// NewEncoder returns an Encoder with default options.
|
||||
func NewEncoder() *Encoder { return &Encoder{groupByKind: true} }
|
||||
func NewEncoder() *Encoder { return &Encoder{layout: LayoutKindGrouped} }
|
||||
|
||||
// GroupByKind toggles whether fields at the same TOML level are reordered
|
||||
// into the group-by-kind layout (scalars first, then tables, then arrays of
|
||||
// tables). When set to false, the emitter preserves the source declaration
|
||||
// order (struct field order, or sorted key order for maps).
|
||||
func (e *Encoder) GroupByKind(v bool) *Encoder {
|
||||
e.groupByKind = v
|
||||
// Layout sets the layout the encoder writes a document's entries in:
|
||||
// LayoutKindGrouped, the default, reorders them scalars first, then tables,
|
||||
// then arrays of tables; LayoutKindDeclaration preserves declaration order.
|
||||
func (e *Encoder) Layout(kind LayoutKind) *Encoder {
|
||||
e.layout = kind
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -589,12 +601,12 @@ func (e *Encoder) OmitEmptyArrays() *Encoder {
|
||||
return e
|
||||
}
|
||||
|
||||
// UseLiteralMultiline sets the length threshold at which a multi-line string
|
||||
// LiteralMultiline sets the length threshold at which a multi-line string
|
||||
// is emitted as a literal triple-quoted string instead of the escaped form.
|
||||
// Use 0 or any negative value to disable (always escaped). The literal form
|
||||
// is selected only when the value contains an internal newline; otherwise the
|
||||
// single-line basic form is used regardless of this setting.
|
||||
func (e *Encoder) UseLiteralMultiline(threshold int) *Encoder {
|
||||
func (e *Encoder) LiteralMultiline(threshold int) *Encoder {
|
||||
e.literalMultilineAt = threshold
|
||||
return e
|
||||
}
|
||||
@@ -609,7 +621,7 @@ func (e *Encoder) UseLiteralMultiline(threshold int) *Encoder {
|
||||
// value array. An inlined table that does not fit the line is written across
|
||||
// lines, which TOML 1.1 allows.
|
||||
//
|
||||
// With GroupByKind(false) the layout is already for presentation only, and an
|
||||
// With LayoutKindDeclaration the layout is already for presentation only, and an
|
||||
// inlined table follows the same rule as any other value line: it lands in the
|
||||
// section of the header that precedes it.
|
||||
func (e *Encoder) InlineTables(threshold int) *Encoder {
|
||||
|
||||
Reference in New Issue
Block a user