]>
Commit | Line | Data |
---|---|---|
863486a6 AG |
1 | package yaml |
2 | ||
3 | import ( | |
4 | "github.com/zclconf/go-cty/cty" | |
5 | ) | |
6 | ||
7 | // ConverterConfig is used to configure a new converter, using NewConverter. | |
8 | type 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. | |
22 | type Converter struct { | |
23 | encodeAsFlow bool | |
24 | } | |
25 | ||
26 | // NewConverter creates a new Converter with the given configuration. | |
27 | func 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. | |
36 | var 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. | |
44 | func (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. | |
57 | func (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. | |
67 | func (c *Converter) Unmarshal(src []byte, ty cty.Type) (cty.Value, error) { | |
68 | return c.unmarshal(src, ty) | |
69 | } |