MCPcopy
hub / github.com/pelletier/go-toml / Encode

Method Encode

marshaler.go:186–205  ·  view source on GitHub ↗

Encode writes a TOML representation of v to the stream. If v cannot be represented to TOML it returns an error. # Encoding rules A top level slice containing only maps or structs is encoded as [[table array]]. All slices not matching rule 1 are encoded as [array]. As a result, any map or struct

(v interface{})

Source from the content-addressed store, hash-verified

184// inside inline tables. For array tables, the comment is only present before
185// the first element of the array.
186func (enc *Encoder) Encode(v interface{}) error {
187 e := encoderStatePool.Get().(*encoderState)
188 e.Encoder = enc
189 e.buf = e.buf[:0]
190 e.keyStack = e.keyStack[:0]
191 e.lastWasHeader = false
192
193 err := e.encodeRoot(v)
194 if err != nil {
195 encoderStatePool.Put(e)
196 return err
197 }
198
199 _, err = enc.w.Write(e.buf)
200 encoderStatePool.Put(e)
201 if err != nil {
202 return fmt.Errorf("toml: cannot write: %w", err)
203 }
204 return nil
205}
206
207var encoderStatePool = sync.Pool{
208 New: func() interface{} { return &encoderState{} },

Callers 15

TestMarshalFunction · 0.95
TestMarshalIndentTablesFunction · 0.95
TestEncodeToBrokenWriterFunction · 0.95
TestIssue786Function · 0.95
TestMarshalElementErrorsFunction · 0.95
marshalFunction · 0.95

Calls 2

encodeRootMethod · 0.80
WriteMethod · 0.45

Tested by 15

TestMarshalFunction · 0.76
TestMarshalIndentTablesFunction · 0.76
TestEncodeToBrokenWriterFunction · 0.76
TestIssue786Function · 0.76
TestMarshalElementErrorsFunction · 0.76
marshalFunction · 0.76