| 1 | package parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.25 bytes/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.25 fmt/" |
| 6 | |
| 7 | "github.com/tomwright/dasel/v3/modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/" |
| 8 | ) |
| 9 | |
| 10 | var writersscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/writers. = map[Formatscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#]NewWriterFnscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/NewWriterFn#{} |
| 11 | |
| 12 | type WriterOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions# struct { |
| 13 | Compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#Compact. bool |
| 14 | Indentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#Indent. string |
| 15 | Extscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#Ext. map[string]string |
| 16 | } |
| 17 | |
| 18 | // DefaultWriterOptions returns the default writer options. |
| 19 | func DefaultWriterOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/DefaultWriterOptions().() WriterOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions# { |
| 20 | return WriterOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#{ |
| 21 | Compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#Compact.: false, |
| 22 | Indentscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#Indent.: " ", |
| 23 | Extscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#Ext.: make(map[string]string), |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // Writer writes a value to a byte slice. |
| 28 | type Writerscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer# interface { |
| 29 | // Write writes a value to a byte slice. |
| 30 | Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#Write.(*modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) ([]byte, error) |
| 31 | } |
| 32 | |
| 33 | // NewWriterFn is a function that creates a new writer. |
| 34 | type NewWriterFnscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/NewWriterFn# func(options WriterOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#) (Writerscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#, error) |
| 35 | |
| 36 | // RegisterWriter registers a new writer for the format. |
| 37 | func RegisterWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/RegisterWriter().(format Formatscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#, fn NewWriterFnscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/NewWriterFn#) { |
| 38 | writersscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/writers.[format] = fn |
| 39 | } |
| 40 | |
| 41 | // DocumentSeparator is an interface that can be implemented by writers to allow for custom document separators. |
| 42 | type DocumentSeparatorscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/DocumentSeparator# interface { |
| 43 | // Separator returns the document separator. |
| 44 | Separatorscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/DocumentSeparator#Separator.() []byte |
| 45 | } |
| 46 | |
| 47 | // MultiDocumentWriter is a writer that can write multiple documents. |
| 48 | func MultiDocumentWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/MultiDocumentWriter().(w Writerscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#) Writerscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer# { |
| 49 | return &multiDocumentWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/multiDocumentWriter#{wscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/multiDocumentWriter#w.: w} |
| 50 | } |
| 51 | |
| 52 | type multiDocumentWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/multiDocumentWriter# struct { |
| 53 | wscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/multiDocumentWriter#w. Writerscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer# |
| 54 | } |
| 55 | |
| 56 | // Write writes a value to a byte slice. |
| 57 | func (w *multiDocumentWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/multiDocumentWriter#) Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/multiDocumentWriter#Write().(value *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) ([]byte, error) { |
| 58 | if value.IsBranchscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsBranch().() || value.IsSpreadscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsSpread().() { |
| 59 | buf := new(bytesscip-go gomod github.com/golang/go/src go1.25 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25 bytes/Buffer#) |
| 60 | |
| 61 | documentSeparator := []byte("\n") |
| 62 | if ds, ok := w.wscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/multiDocumentWriter#w..(DocumentSeparatorscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/DocumentSeparator#); ok { |
| 63 | documentSeparator = ds.Separatorscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/DocumentSeparator#Separator.() |
| 64 | } |
| 65 | |
| 66 | totalDocuments, err := value.SliceLenscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SliceLen().() |
| 67 | if err != nil { |
| 68 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("failed to get document length: %w", err) |
| 69 | } |
| 70 | |
| 71 | if err := value.RangeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#RangeSlice().(func(i int, v *modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.Valuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#) error { |
| 72 | docBytes, err := w.wscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/multiDocumentWriter#w..Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#Write.(v) |
| 73 | if err != nil { |
| 74 | return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("failed to write document %d: %w", i, err) |
| 75 | } |
| 76 | buf.Writescip-go gomod github.com/golang/go/src go1.25 bytes/Buffer#Write().(docBytes) |
| 77 | |
| 78 | if i < totalDocuments-1 { |
| 79 | buf.Writescip-go gomod github.com/golang/go/src go1.25 bytes/Buffer#Write().(documentSeparator) |
| 80 | } |
| 81 | |
| 82 | return nil |
| 83 | }); err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | |
| 87 | return buf.Bytesscip-go gomod github.com/golang/go/src go1.25 bytes/Buffer#Bytes().(), nil |
| 88 | } |
| 89 | return w.wscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/multiDocumentWriter#w..Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#Write.(value) |
| 90 | } |