| 1 | package ini |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "github.com/tomwright/dasel/v3/model" |
| 7 | "github.com/tomwright/dasel/v3/parsing" |
| 8 | "gopkg.in/ini.v1" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | var _ parsing.Writer = (*iniWriter)(nil) |
| 13 | |
| 14 | func newINIWriter(options parsing.WriterOptions) (parsing.Writer, error) { |
| 15 | return &iniWriter{ |
| 16 | options: options, |
| 17 | }, nil |
| 18 | } |
| 19 | |
| 20 | type iniWriter struct { |
| 21 | options parsing.WriterOptions |
| 22 | } |
| 23 | |
| 24 | // Write writes a value to a byte slice. |
| 25 | func (j *iniWriter) Write(value *model.Value) ([]byte, error) { |
| 26 | if !value.IsMap() { |
| 27 | return nil, fmt.Errorf("ini can only represent map values") |
| 28 | } |
| 29 | |
| 30 | f := ini.Empty(ini.LoadOptions{ |
| 31 | AllowNestedValues: true, |
| 32 | }) |
| 33 | |
| 34 | if err := j.write(f, ini.DefaultSection, value); err != nil { |
| 35 | return nil, err |
| 36 | } |
| 37 | |
| 38 | buf := new(bytes.Buffer) |
| 39 | |
| 40 | if _, err := f.WriteTo(buf); err != nil { |
| 41 | return nil, fmt.Errorf("failed to write ini: %w", err) |
| 42 | } |
| 43 | |
| 44 | return buf.Bytes(), nil |
| 45 | } |
| 46 | |
| 47 | func (j *iniWriter) write(f *ini.File, path string, value *model.Value) error { |
| 48 | section, err := f.NewSection(path) |
| 49 | if err != nil { |
| 50 | return fmt.Errorf("failed to create section %s: %w", path, err) |
| 51 | } |
| 52 | |
| 53 | nextSectionName := func(x string) string { |
| 54 | path := strings.TrimSpace(strings.TrimPrefix(path, ini.DefaultSection)) |
| 55 | return strings.TrimSpace(path + " " + x) |
| 56 | } |
| 57 | |
| 58 | switch value.Type() { |
| 59 | case model.TypeMap: |
| 60 | if err := value.RangeMap(func(s string, value *model.Value) error { |
| 61 | switch { |
| 62 | case value.IsScalar(): |
| 63 | strVal, err := valueToString(value) |
| 64 | if err != nil { |
| 65 | return fmt.Errorf("failed to convert value to string: %w", err) |
| 66 | } |
| 67 | _, err = section.NewKey(s, strVal) |
| 68 | if err != nil { |
| 69 | return fmt.Errorf("failed to create key %s: %w", s, err) |
| 70 | } |
| 71 | return nil |
| 72 | |
| 73 | case value.IsSlice(): |
| 74 | return fmt.Errorf("ini writer cannot represent slice values directly; consider using nested sections") |
| 75 | |
| 76 | case value.IsMap(): |
| 77 | if err := j.write(f, nextSectionName(s), value); err != nil { |
| 78 | return err |
| 79 | } |
| 80 | return nil |
| 81 | |
| 82 | default: |
| 83 | return fmt.Errorf("ini writer cannot represent value of type %s", value.Type()) |
| 84 | } |
| 85 | }); err != nil { |
| 86 | return err |
| 87 | } |
| 88 | default: |
| 89 | return fmt.Errorf("ini sections can only represent map values") |
| 90 | } |
| 91 | return nil |
| 92 | } |
| 93 | |
| 94 | func valueToString(v *model.Value) (string, error) { |
| 95 | if v.IsNull() { |
| 96 | return "", nil |
| 97 | } |
| 98 | |
| 99 | switch v.Type() { |
| 100 | case model.TypeString: |
| 101 | stringValue, err := v.StringValue() |
| 102 | if err != nil { |
| 103 | return "", err |
| 104 | } |
| 105 | return stringValue, nil |
| 106 | case model.TypeInt: |
| 107 | i, err := v.IntValue() |
| 108 | if err != nil { |
| 109 | return "", err |
| 110 | } |
| 111 | return fmt.Sprintf("%d", i), nil |
| 112 | case model.TypeFloat: |
| 113 | i, err := v.FloatValue() |
| 114 | if err != nil { |
| 115 | return "", err |
| 116 | } |
| 117 | return fmt.Sprintf("%g", i), nil |
| 118 | case model.TypeBool: |
| 119 | i, err := v.BoolValue() |
| 120 | if err != nil { |
| 121 | return "", err |
| 122 | } |
| 123 | return fmt.Sprintf("%t", i), nil |
| 124 | default: |
| 125 | return "", fmt.Errorf("csv writer cannot format type %s to string", v.Type()) |
| 126 | } |
| 127 | } |