| 1 | package yaml |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/tomwright/dasel/v3/model" |
| 7 | "github.com/tomwright/dasel/v3/parsing" |
| 8 | "go.yaml.in/yaml/v4" |
| 9 | ) |
| 10 | |
| 11 | var _ parsing.Writer = (*yamlWriter)(nil) |
| 12 | |
| 13 | func newYAMLWriter(options parsing.WriterOptions) (parsing.Writer, error) { |
| 14 | return &yamlWriter{options: options}, nil |
| 15 | } |
| 16 | |
| 17 | type yamlWriter struct { |
| 18 | options parsing.WriterOptions |
| 19 | } |
| 20 | |
| 21 | func (j *yamlWriter) Separator() []byte { |
| 22 | return []byte("---\n") |
| 23 | } |
| 24 | |
| 25 | // Write writes a value to a byte slice. |
| 26 | func (j *yamlWriter) Write(value *model.Value) ([]byte, error) { |
| 27 | yv := &yamlValue{value: value, compact: j.options.Compact} |
| 28 | res, err := yv.ToNode() |
| 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | return yaml.Marshal(res) |
| 33 | } |
| 34 | |
| 35 | func (yv *yamlValue) ToNode() (*yaml.Node, error) { |
| 36 | res := &yaml.Node{} |
| 37 | |
| 38 | // TODO : Handle yaml aliases. |
| 39 | //yamlAlias, ok := yv.value.Metadata["yaml-alias"].(string) |
| 40 | //if ok { |
| 41 | //res.Kind = yaml.AliasNode |
| 42 | //res.Value = yamlAlias |
| 43 | //return res, nil |
| 44 | //} |
| 45 | |
| 46 | switch yv.value.Type() { |
| 47 | case model.TypeString: |
| 48 | v, err := yv.value.StringValue() |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | res.Kind = yaml.ScalarNode |
| 53 | res.Value = v |
| 54 | res.Tag = "!!str" |
| 55 | if styleVal, ok := yv.value.MetadataValue("yaml-style"); ok { |
| 56 | if style, ok := styleVal.(yaml.Style); ok { |
| 57 | res.Style = style |
| 58 | } |
| 59 | } |
| 60 | case model.TypeBool: |
| 61 | v, err := yv.value.BoolValue() |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | res.Kind = yaml.ScalarNode |
| 66 | res.Value = fmt.Sprintf("%t", v) |
| 67 | res.Tag = "!!bool" |
| 68 | case model.TypeInt: |
| 69 | v, err := yv.value.IntValue() |
| 70 | if err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 | res.Kind = yaml.ScalarNode |
| 74 | res.Value = fmt.Sprintf("%d", v) |
| 75 | res.Tag = "!!int" |
| 76 | case model.TypeFloat: |
| 77 | v, err := yv.value.FloatValue() |
| 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | res.Kind = yaml.ScalarNode |
| 82 | res.Value = fmt.Sprintf("%g", v) |
| 83 | res.Tag = "!!float" |
| 84 | case model.TypeMap: |
| 85 | res.Kind = yaml.MappingNode |
| 86 | if yv.compact { |
| 87 | res.Style = yaml.FlowStyle |
| 88 | } |
| 89 | if err := yv.value.RangeMap(func(key string, val *model.Value) error { |
| 90 | keyNode := &yamlValue{value: model.NewStringValue(key), compact: yv.compact} |
| 91 | valNode := &yamlValue{value: val, compact: yv.compact} |
| 92 | |
| 93 | marshalledKey, err := keyNode.ToNode() |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | marshalledVal, err := valNode.ToNode() |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | |
| 102 | res.Content = append(res.Content, marshalledKey) |
| 103 | res.Content = append(res.Content, marshalledVal) |
| 104 | |
| 105 | return nil |
| 106 | }); err != nil { |
| 107 | return nil, err |
| 108 | } |
| 109 | case model.TypeSlice: |
| 110 | res.Kind = yaml.SequenceNode |
| 111 | if yv.compact { |
| 112 | res.Style = yaml.FlowStyle |
| 113 | } |
| 114 | if err := yv.value.RangeSlice(func(i int, val *model.Value) error { |
| 115 | valNode := &yamlValue{value: val, compact: yv.compact} |
| 116 | marshalledVal, err := valNode.ToNode() |
| 117 | if err != nil { |
| 118 | return err |
| 119 | } |
| 120 | res.Content = append(res.Content, marshalledVal) |
| 121 | return nil |
| 122 | }); err != nil { |
| 123 | return nil, err |
| 124 | } |
| 125 | case model.TypeNull: |
| 126 | res.Kind = yaml.ScalarNode |
| 127 | res.Value = "null" |
| 128 | res.Tag = "!!null" |
| 129 | case model.TypeUnknown: |
| 130 | return nil, fmt.Errorf("unknown type: %s", yv.value.Type()) |
| 131 | } |
| 132 | |
| 133 | return res, nil |
| 134 | } |
| 135 | |
| 136 | func (yv *yamlValue) MarshalYAML() (any, error) { |
| 137 | res, err := yv.ToNode() |
| 138 | if err != nil { |
| 139 | return nil, err |
| 140 | } |
| 141 | return res, nil |
| 142 | } |