| 1 | package csvscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/csv`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.25 bytes/" |
| 5 | "encoding/csvscip-go gomod github.com/golang/go/src go1.25 `encoding/csv`/" |
| 6 | "fmtscip-go gomod github.com/golang/go/src go1.25 fmt/" |
| 7 | "github.com/tomwright/dasel/v3/modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/" |
| 8 | ) |
| 9 | |
| 10 | type csvWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/csv`/csvWriter# struct { |
| 11 | separatorscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/csv`/csvWriter#separator. rune |
| 12 | } |
| 13 | |
| 14 | // Write writes a value to a byte slice. |
| 15 | func (j *csvWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/csv`/csvWriter#) Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/csv`/csvWriter#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) { |
| 16 | if !value.IsSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsSlice().() { |
| 17 | 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().("csv writer expects root output to be a slice/array, got %s", value.Typescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Type().()) |
| 18 | } |
| 19 | |
| 20 | 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#) |
| 21 | w := csvscip-go gomod github.com/golang/go/src go1.25 `encoding/csv`/.NewWriterscip-go gomod github.com/golang/go/src go1.25 `encoding/csv`/NewWriter().(buf) |
| 22 | w.Commascip-go gomod github.com/golang/go/src go1.25 `encoding/csv`/Writer#Comma. = j.separatorscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/csv`/csvWriter#separator. |
| 23 | |
| 24 | var headers []string |
| 25 | |
| 26 | if err := value.RangeSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#RangeSlice().(func(i int, row *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 { |
| 27 | if i == 0 { |
| 28 | var err error |
| 29 | headers, err = row.MapKeysscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MapKeys().() |
| 30 | if err != nil { |
| 31 | return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error getting map keys: %w", err) |
| 32 | } |
| 33 | if err := w.Writescip-go gomod github.com/golang/go/src go1.25 `encoding/csv`/Writer#Write().(headers); err != nil { |
| 34 | return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error writing headers: %w", err) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | var values []string |
| 39 | |
| 40 | for _, headerKey := range headers { |
| 41 | colV, err := row.GetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetMapKey().(headerKey) |
| 42 | if err != nil { |
| 43 | return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error getting map key %q: %w", headerKey, err) |
| 44 | } |
| 45 | |
| 46 | csvVal, err := valueToStringscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/csv`/valueToString().(colV) |
| 47 | if err != nil { |
| 48 | return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error converting value to string: %w", err) |
| 49 | } |
| 50 | |
| 51 | values = append(values, csvVal) |
| 52 | } |
| 53 | |
| 54 | if err := w.Writescip-go gomod github.com/golang/go/src go1.25 `encoding/csv`/Writer#Write().(values); err != nil { |
| 55 | return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("error writing row: %w", err) |
| 56 | } |
| 57 | |
| 58 | return nil |
| 59 | }); err != nil { |
| 60 | 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().("error ranging slice: %w", err) |
| 61 | } |
| 62 | |
| 63 | w.Flushscip-go gomod github.com/golang/go/src go1.25 `encoding/csv`/Writer#Flush().() |
| 64 | |
| 65 | return buf.Bytesscip-go gomod github.com/golang/go/src go1.25 bytes/Buffer#Bytes().(), nil |
| 66 | } |