| 1 | package iniscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/ini`/ |
| 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 | "github.com/tomwright/dasel/v3/modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/" |
| 7 | "github.com/tomwright/dasel/v3/parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/" |
| 8 | "gopkg.in/ini.v1scip-go gomod gopkg.in/ini.v1 v1.67.0 `gopkg.in/ini.v1`/" |
| 9 | "stringsscip-go gomod github.com/golang/go/src go1.25 strings/" |
| 10 | ) |
| 11 | |
| 12 | var _ parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.Writerscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer# = (*iniWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/ini`/iniWriter#)(nil) |
| 13 | |
| 14 | func newINIWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/ini`/newINIWriter().(options parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.WriterOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#) (parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.Writerscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#, error) { |
| 15 | return &iniWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/ini`/iniWriter#{ |
| 16 | optionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/ini`/iniWriter#options.: options, |
| 17 | }, nil |
| 18 | } |
| 19 | |
| 20 | type iniWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/ini`/iniWriter# struct { |
| 21 | optionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/ini`/iniWriter#options. parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.WriterOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions# |
| 22 | } |
| 23 | |
| 24 | // Write writes a value to a byte slice. |
| 25 | func (j *iniWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/ini`/iniWriter#) Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/ini`/iniWriter#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) { |
| 26 | if !value.IsMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsMap().() { |
| 27 | 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().("ini can only represent map values") |
| 28 | } |
| 29 | |
| 30 | f := iniscip-go gomod gopkg.in/ini.v1 v1.67.0 `gopkg.in/ini.v1`/.Emptyscip-go gomod gopkg.in/ini.v1 v1.67.0 `gopkg.in/ini.v1`/Empty().(iniscip-go gomod gopkg.in/ini.v1 v1.67.0 `gopkg.in/ini.v1`/.LoadOptionsscip-go gomod gopkg.in/ini.v1 v1.67.0 `gopkg.in/ini.v1`/LoadOptions#{ |
| 31 | AllowNestedValuesscip-go gomod gopkg.in/ini.v1 v1.67.0 `gopkg.in/ini.v1`/LoadOptions#AllowNestedValues.: true, |
| 32 | }) |
| 33 | |
| 34 | if err := j.writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/ini`/iniWriter#write().(f, iniscip-go gomod gopkg.in/ini.v1 v1.67.0 `gopkg.in/ini.v1`/.DefaultSectionscip-go gomod gopkg.in/ini.v1 v1.67.0 `gopkg.in/ini.v1`/DefaultSection., value); err != nil { |
| 35 | return nil, err |
| 36 | } |
| 37 | |
| 38 | 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#) |
| 39 | |
| 40 | if _, err := f.WriteToscip-go gomod gopkg.in/ini.v1 v1.67.0 `gopkg.in/ini.v1`/File#WriteTo().(buf); err != nil { |
| 41 | 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 write ini: %w", err) |
| 42 | } |
| 43 | |
| 44 | return buf.Bytesscip-go gomod github.com/golang/go/src go1.25 bytes/Buffer#Bytes().(), nil |
| 45 | } |
| 46 | |
| 47 | func (j *iniWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/ini`/iniWriter#) writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/ini`/iniWriter#write().(f *iniscip-go gomod gopkg.in/ini.v1 v1.67.0 `gopkg.in/ini.v1`/.Filescip-go gomod gopkg.in/ini.v1 v1.67.0 `gopkg.in/ini.v1`/File#, path string, 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#) error { |
| 48 | section, err := f.NewSectionscip-go gomod gopkg.in/ini.v1 v1.67.0 `gopkg.in/ini.v1`/File#NewSection().(path) |
| 49 | if err != nil { |
| 50 | 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 create section %s: %w", path, err) |
| 51 | } |
| 52 | |
| 53 | nextSectionName := func(x string) string { |
| 54 | path := stringsscip-go gomod github.com/golang/go/src go1.25 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25 strings/TrimSpace().(stringsscip-go gomod github.com/golang/go/src go1.25 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.25 strings/TrimPrefix().(path, iniscip-go gomod gopkg.in/ini.v1 v1.67.0 `gopkg.in/ini.v1`/.DefaultSectionscip-go gomod gopkg.in/ini.v1 v1.67.0 `gopkg.in/ini.v1`/DefaultSection.)) |
| 55 | return stringsscip-go gomod github.com/golang/go/src go1.25 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25 strings/TrimSpace().(path + " " + x) |
| 56 | } |
| 57 | |
| 58 | switch value.Typescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Type().() { |
| 59 | case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeMap.: |
| 60 | if err := value.RangeMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#RangeMap().(func(s string, 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#) error { |
| 61 | switch { |
| 62 | case value.IsScalarscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsScalar().(): |
| 63 | strVal, err := valueToStringscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/ini`/valueToString().(value) |
| 64 | if err != nil { |
| 65 | 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 convert value to string: %w", err) |
| 66 | } |
| 67 | _, err = section.NewKeyscip-go gomod gopkg.in/ini.v1 v1.67.0 `gopkg.in/ini.v1`/Section#NewKey().(s, strVal) |
| 68 | if err != nil { |
| 69 | 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 create key %s: %w", s, err) |
| 70 | } |
| 71 | return nil |
| 72 | |
| 73 | case value.IsSlicescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsSlice().(): |
| 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().("ini writer cannot represent slice values directly; consider using nested sections") |
| 75 | |
| 76 | case value.IsMapscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsMap().(): |
| 77 | if err := j.writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/ini`/iniWriter#write().(f, nextSectionName(s), value); err != nil { |
| 78 | return err |
| 79 | } |
| 80 | return nil |
| 81 | |
| 82 | default: |
| 83 | return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("ini writer cannot represent value of type %s", value.Typescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Type().()) |
| 84 | } |
| 85 | }); err != nil { |
| 86 | return err |
| 87 | } |
| 88 | default: |
| 89 | return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25 fmt/Errorf().("ini sections can only represent map values") |
| 90 | } |
| 91 | return nil |
| 92 | } |
| 93 | |
| 94 | func valueToStringscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/ini`/valueToString().(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#) (string, error) { |
| 95 | if v.IsNullscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IsNull().() { |
| 96 | return "", nil |
| 97 | } |
| 98 | |
| 99 | switch v.Typescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Type().() { |
| 100 | case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeStringscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeString.: |
| 101 | stringValue, err := v.StringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#StringValue().() |
| 102 | if err != nil { |
| 103 | return "", err |
| 104 | } |
| 105 | return stringValue, nil |
| 106 | case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeIntscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeInt.: |
| 107 | i, err := v.IntValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#IntValue().() |
| 108 | if err != nil { |
| 109 | return "", err |
| 110 | } |
| 111 | return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25 fmt/Sprintf().("%d", i), nil |
| 112 | case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeFloatscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeFloat.: |
| 113 | i, err := v.FloatValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#FloatValue().() |
| 114 | if err != nil { |
| 115 | return "", err |
| 116 | } |
| 117 | return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25 fmt/Sprintf().("%g", i), nil |
| 118 | case modelscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/.TypeBoolscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/TypeBool.: |
| 119 | i, err := v.BoolValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#BoolValue().() |
| 120 | if err != nil { |
| 121 | return "", err |
| 122 | } |
| 123 | return fmtscip-go gomod github.com/golang/go/src go1.25 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25 fmt/Sprintf().("%t", i), nil |
| 124 | default: |
| 125 | return "", 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 cannot format type %s to string", v.Typescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#Type().()) |
| 126 | } |
| 127 | } |