aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/zclconf/go-cty-yaml/encode.go
blob: daa1478a934c52b6fe32b9ed92bf9a1705771b0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package yaml

import (
	"bytes"
	"fmt"
	"strings"

	"github.com/zclconf/go-cty/cty"
)

func (c *Converter) marshal(v cty.Value) ([]byte, error) {
	var buf bytes.Buffer

	e := &yaml_emitter_t{}
	yaml_emitter_initialize(e)
	yaml_emitter_set_output_writer(e, &buf)
	yaml_emitter_set_unicode(e, true)

	var evt yaml_event_t
	yaml_stream_start_event_initialize(&evt, yaml_UTF8_ENCODING)
	if !yaml_emitter_emit(e, &evt) {
		return nil, emitterError(e)
	}
	yaml_document_start_event_initialize(&evt, nil, nil, true)
	if !yaml_emitter_emit(e, &evt) {
		return nil, emitterError(e)
	}

	if err := c.marshalEmit(v, e); err != nil {
		return nil, err
	}

	yaml_document_end_event_initialize(&evt, true)
	if !yaml_emitter_emit(e, &evt) {
		return nil, emitterError(e)
	}
	yaml_stream_end_event_initialize(&evt)
	if !yaml_emitter_emit(e, &evt) {
		return nil, emitterError(e)
	}

	return buf.Bytes(), nil
}

func (c *Converter) marshalEmit(v cty.Value, e *yaml_emitter_t) error {
	ty := v.Type()
	switch {
	case v.IsNull():
		return c.marshalPrimitive(v, e)
	case !v.IsKnown():
		return fmt.Errorf("cannot serialize unknown value as YAML")
	case ty.IsPrimitiveType():
		return c.marshalPrimitive(v, e)
	case ty.IsTupleType(), ty.IsListType(), ty.IsSetType():
		return c.marshalSequence(v, e)
	case ty.IsObjectType(), ty.IsMapType():
		return c.marshalMapping(v, e)
	default:
		return fmt.Errorf("can't marshal %s as YAML", ty.FriendlyName())
	}
}

func (c *Converter) marshalPrimitive(v cty.Value, e *yaml_emitter_t) error {
	var evt yaml_event_t

	if v.IsNull() {
		yaml_scalar_event_initialize(
			&evt,
			nil,
			nil,
			[]byte("null"),
			true,
			true,
			yaml_PLAIN_SCALAR_STYLE,
		)
		if !yaml_emitter_emit(e, &evt) {
			return emitterError(e)
		}
		return nil
	}

	switch v.Type() {
	case cty.String:
		str := v.AsString()
		style := yaml_DOUBLE_QUOTED_SCALAR_STYLE
		if strings.Contains(str, "\n") {
			style = yaml_LITERAL_SCALAR_STYLE
		}
		yaml_scalar_event_initialize(
			&evt,
			nil,
			nil,
			[]byte(str),
			true,
			true,
			style,
		)
	case cty.Number:
		str := v.AsBigFloat().Text('f', -1)
		yaml_scalar_event_initialize(
			&evt,
			nil,
			nil,
			[]byte(str),
			true,
			true,
			yaml_PLAIN_SCALAR_STYLE,
		)
	case cty.Bool:
		var str string
		switch v {
		case cty.True:
			str = "true"
		case cty.False:
			str = "false"
		}
		yaml_scalar_event_initialize(
			&evt,
			nil,
			nil,
			[]byte(str),
			true,
			true,
			yaml_PLAIN_SCALAR_STYLE,
		)
	}
	if !yaml_emitter_emit(e, &evt) {
		return emitterError(e)
	}
	return nil
}

func (c *Converter) marshalSequence(v cty.Value, e *yaml_emitter_t) error {
	style := yaml_BLOCK_SEQUENCE_STYLE
	if c.encodeAsFlow {
		style = yaml_FLOW_SEQUENCE_STYLE
	}

	var evt yaml_event_t
	yaml_sequence_start_event_initialize(&evt, nil, nil, true, style)
	if !yaml_emitter_emit(e, &evt) {
		return emitterError(e)
	}

	for it := v.ElementIterator(); it.Next(); {
		_, v := it.Element()
		err := c.marshalEmit(v, e)
		if err != nil {
			return err
		}
	}

	yaml_sequence_end_event_initialize(&evt)
	if !yaml_emitter_emit(e, &evt) {
		return emitterError(e)
	}
	return nil
}

func (c *Converter) marshalMapping(v cty.Value, e *yaml_emitter_t) error {
	style := yaml_BLOCK_MAPPING_STYLE
	if c.encodeAsFlow {
		style = yaml_FLOW_MAPPING_STYLE
	}

	var evt yaml_event_t
	yaml_mapping_start_event_initialize(&evt, nil, nil, true, style)
	if !yaml_emitter_emit(e, &evt) {
		return emitterError(e)
	}

	for it := v.ElementIterator(); it.Next(); {
		k, v := it.Element()
		err := c.marshalEmit(k, e)
		if err != nil {
			return err
		}
		err = c.marshalEmit(v, e)
		if err != nil {
			return err
		}
	}

	yaml_mapping_end_event_initialize(&evt)
	if !yaml_emitter_emit(e, &evt) {
		return emitterError(e)
	}
	return nil
}