yaml

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2022 License: Apache-2.0 Imports: 14 Imported by: 45

Documentation

Overview

Package yaml can marshal and unmarshal cty values in YAML format.

Index

Constants

This section is empty.

Variables

View Source
var YAMLDecodeFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name: "src",
			Type: cty.String,
		},
	},
	Type: func(args []cty.Value) (cty.Type, error) {
		if !args[0].IsKnown() {
			return cty.DynamicPseudoType, nil
		}
		if args[0].IsNull() {
			return cty.NilType, function.NewArgErrorf(0, "YAML source code cannot be null")
		}
		return Standard.ImpliedType([]byte(args[0].AsString()))
	},
	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
		return Standard.Unmarshal([]byte(args[0].AsString()), retType)
	},
})

YAMLDecodeFunc is a cty function for decoding arbitrary YAML source code into a cty Value, using the ImpliedType and Unmarshal methods of the Standard pre-defined converter.

View Source
var YAMLEncodeFunc = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name:             "value",
			Type:             cty.DynamicPseudoType,
			AllowNull:        true,
			AllowDynamicType: true,
		},
	},
	Type: function.StaticReturnType(cty.String),
	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
		if !args[0].IsWhollyKnown() {
			return cty.UnknownVal(retType), nil
		}
		raw, err := Standard.Marshal(args[0])
		if err != nil {
			return cty.NilVal, err
		}
		return cty.StringVal(string(raw)), nil
	},
})

YAMLEncodeFunc is a cty function for encoding an arbitrary cty value into YAML.

Functions

func ImpliedType

func ImpliedType(src []byte) (cty.Type, error)

ImpliedType analyzes the given source code and returns a suitable type that it could be decoded into.

For a converter that is using standard YAML rather than cty-specific custom tags, only a subset of cty types can be produced: strings, numbers, bools, tuple types, and object types.

This is an alias for ImpliedType on the predefined Converter in "Standard".

func Marshal

func Marshal(v cty.Value) ([]byte, error)

Marshal serializes the given value into a YAML document, using a fixed mapping from cty types to YAML constructs.

This is an alias for Marshal on the predefined Converter in "Standard".

Note that unlike the function of the same name in the cty JSON package, this does not take a type constraint and therefore the YAML serialization cannot preserve late-bound type information in the serialization to be recovered from Unmarshal. Instead, any cty.DynamicPseudoType in the type constraint given to Unmarshal will be decoded as if the corresponding portion of the input were processed with ImpliedType to find a target type.

func Unmarshal

func Unmarshal(src []byte, ty cty.Type) (cty.Value, error)

Unmarshal reads the document found within the given source buffer and attempts to convert it into a value conforming to the given type constraint.

This is an alias for Unmarshal on the predefined Converter in "Standard".

An error is returned if the given source contains any YAML document delimiters.

Types

type Converter

type Converter struct {
	// contains filtered or unexported fields
}

A Converter can marshal and unmarshal between cty values and YAML bytes.

Because there are many different ways to map cty to YAML and vice-versa, a converter is configurable using the settings in ConverterConfig, which allow for a few different permutations of mapping to YAML.

If you are just trying to work with generic, standard YAML, the predefined converter in Standard should be good enough.

var Standard *Converter = NewConverter(&ConverterConfig{})

Standard is a predefined Converter that produces and consumes generic YAML using only built-in constructs that any other YAML implementation ought to understand.

func NewConverter

func NewConverter(config *ConverterConfig) *Converter

NewConverter creates a new Converter with the given configuration.

func (*Converter) ImpliedType

func (c *Converter) ImpliedType(src []byte) (cty.Type, error)

ImpliedType analyzes the given source code and returns a suitable type that it could be decoded into.

For a converter that is using standard YAML rather than cty-specific custom tags, only a subset of cty types can be produced: strings, numbers, bools, tuple types, and object types.

func (*Converter) Marshal

func (c *Converter) Marshal(v cty.Value) ([]byte, error)

Marshal serializes the given value into a YAML document, using a fixed mapping from cty types to YAML constructs.

Note that unlike the function of the same name in the cty JSON package, this does not take a type constraint and therefore the YAML serialization cannot preserve late-bound type information in the serialization to be recovered from Unmarshal. Instead, any cty.DynamicPseudoType in the type constraint given to Unmarshal will be decoded as if the corresponding portion of the input were processed with ImpliedType to find a target type.

func (*Converter) Unmarshal

func (c *Converter) Unmarshal(src []byte, ty cty.Type) (cty.Value, error)

Unmarshal reads the document found within the given source buffer and attempts to convert it into a value conforming to the given type constraint.

An error is returned if the given source contains more than one YAML document.

type ConverterConfig

type ConverterConfig struct {
	// EncodeAsFlow, when set to true, causes Marshal to produce flow-style
	// mapping and sequence serializations.
	EncodeAsFlow bool
}

ConverterConfig is used to configure a new converter, using NewConverter.

type Error

type Error struct {
	Line, Column int
	// contains filtered or unexported fields
}

Error is an error implementation used to report errors that correspond to a particular position in an input buffer.

func (Error) Cause

func (e Error) Cause() error

Cause is an implementation of the interface used by github.com/pkg/errors.Cause, returning the underlying error without the position information.

func (Error) Error

func (e Error) Error() string

func (Error) WrappedErrors

func (e Error) WrappedErrors() []error

WrappedErrors is an implementation of github.com/hashicorp/errwrap.Wrapper returning the underlying error without the position information.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL