aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/zclconf/go-cty-yaml/converter.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/zclconf/go-cty-yaml/converter.go')
-rw-r--r--vendor/github.com/zclconf/go-cty-yaml/converter.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/vendor/github.com/zclconf/go-cty-yaml/converter.go b/vendor/github.com/zclconf/go-cty-yaml/converter.go
new file mode 100644
index 0000000..a73b34a
--- /dev/null
+++ b/vendor/github.com/zclconf/go-cty-yaml/converter.go
@@ -0,0 +1,69 @@
1package yaml
2
3import (
4 "github.com/zclconf/go-cty/cty"
5)
6
7// ConverterConfig is used to configure a new converter, using NewConverter.
8type ConverterConfig struct {
9 // EncodeAsFlow, when set to true, causes Marshal to produce flow-style
10 // mapping and sequence serializations.
11 EncodeAsFlow bool
12}
13
14// A Converter can marshal and unmarshal between cty values and YAML bytes.
15//
16// Because there are many different ways to map cty to YAML and vice-versa,
17// a converter is configurable using the settings in ConverterConfig, which
18// allow for a few different permutations of mapping to YAML.
19//
20// If you are just trying to work with generic, standard YAML, the predefined
21// converter in Standard should be good enough.
22type Converter struct {
23 encodeAsFlow bool
24}
25
26// NewConverter creates a new Converter with the given configuration.
27func NewConverter(config *ConverterConfig) *Converter {
28 return &Converter{
29 encodeAsFlow: config.EncodeAsFlow,
30 }
31}
32
33// Standard is a predefined Converter that produces and consumes generic YAML
34// using only built-in constructs that any other YAML implementation ought to
35// understand.
36var Standard *Converter = NewConverter(&ConverterConfig{})
37
38// ImpliedType analyzes the given source code and returns a suitable type that
39// it could be decoded into.
40//
41// For a converter that is using standard YAML rather than cty-specific custom
42// tags, only a subset of cty types can be produced: strings, numbers, bools,
43// tuple types, and object types.
44func (c *Converter) ImpliedType(src []byte) (cty.Type, error) {
45 return c.impliedType(src)
46}
47
48// Marshal serializes the given value into a YAML document, using a fixed
49// mapping from cty types to YAML constructs.
50//
51// Note that unlike the function of the same name in the cty JSON package,
52// this does not take a type constraint and therefore the YAML serialization
53// cannot preserve late-bound type information in the serialization to be
54// recovered from Unmarshal. Instead, any cty.DynamicPseudoType in the type
55// constraint given to Unmarshal will be decoded as if the corresponding portion
56// of the input were processed with ImpliedType to find a target type.
57func (c *Converter) Marshal(v cty.Value) ([]byte, error) {
58 return c.marshal(v)
59}
60
61// Unmarshal reads the document found within the given source buffer
62// and attempts to convert it into a value conforming to the given type
63// constraint.
64//
65// An error is returned if the given source contains any YAML document
66// delimiters.
67func (c *Converter) Unmarshal(src []byte, ty cty.Type) (cty.Value, error) {
68 return c.unmarshal(src, ty)
69}