...

Source file src/sigs.k8s.io/kustomize/kyaml/yaml/alias.go

Documentation: sigs.k8s.io/kustomize/kyaml/yaml

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package yaml
     5  
     6  import (
     7  	"bytes"
     8  	"io"
     9  
    10  	yaml "sigs.k8s.io/yaml/goyaml.v3"
    11  )
    12  
    13  const (
    14  	WideSequenceStyle    SequenceIndentStyle = "wide"
    15  	CompactSequenceStyle SequenceIndentStyle = "compact"
    16  	DefaultIndent                            = 2
    17  	// BareSeqNodeWrappingKey kyaml uses reader annotations to track resources, it is not possible to
    18  	// add them to bare sequence nodes, this key is used to wrap such bare
    19  	// sequence nodes into map node, byteio_writer unwraps it while writing back
    20  	BareSeqNodeWrappingKey = "bareSeqNodeWrappingKey"
    21  )
    22  
    23  // SeqIndentType holds the indentation style for sequence nodes
    24  type SequenceIndentStyle string
    25  
    26  // EncoderOptions are options that can be used to configure the encoder,
    27  // do not expose new options without considerable justification
    28  type EncoderOptions struct {
    29  	// SeqIndent is the indentation style for YAML Sequence nodes
    30  	SeqIndent SequenceIndentStyle
    31  }
    32  
    33  // Expose the yaml.v3 functions so this package can be used as a replacement
    34  
    35  type Decoder = yaml.Decoder
    36  type Encoder = yaml.Encoder
    37  type IsZeroer = yaml.IsZeroer
    38  type Kind = yaml.Kind
    39  type Marshaler = yaml.Marshaler
    40  type Node = yaml.Node
    41  type Style = yaml.Style
    42  type TypeError = yaml.TypeError
    43  type Unmarshaler = yaml.Unmarshaler
    44  
    45  var Marshal = func(in interface{}) ([]byte, error) {
    46  	var buf bytes.Buffer
    47  	err := NewEncoder(&buf).Encode(in)
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  	return buf.Bytes(), nil
    52  }
    53  var Unmarshal = yaml.Unmarshal
    54  var NewDecoder = yaml.NewDecoder
    55  var NewEncoder = func(w io.Writer) *yaml.Encoder {
    56  	e := yaml.NewEncoder(w)
    57  	e.SetIndent(DefaultIndent)
    58  	e.CompactSeqIndent()
    59  	return e
    60  }
    61  
    62  // MarshalWithOptions marshals the input interface with provided options
    63  func MarshalWithOptions(in interface{}, opts *EncoderOptions) ([]byte, error) {
    64  	var buf bytes.Buffer
    65  	err := NewEncoderWithOptions(&buf, opts).Encode(in)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	return buf.Bytes(), nil
    70  }
    71  
    72  // NewEncoderWithOptions returns the encoder with provided options
    73  func NewEncoderWithOptions(w io.Writer, opts *EncoderOptions) *yaml.Encoder {
    74  	encoder := NewEncoder(w)
    75  	encoder.SetIndent(DefaultIndent)
    76  	if opts.SeqIndent == WideSequenceStyle {
    77  		encoder.DefaultSeqIndent()
    78  	} else {
    79  		encoder.CompactSeqIndent()
    80  	}
    81  	return encoder
    82  }
    83  
    84  var AliasNode yaml.Kind = yaml.AliasNode
    85  var DocumentNode yaml.Kind = yaml.DocumentNode
    86  var MappingNode yaml.Kind = yaml.MappingNode
    87  var ScalarNode yaml.Kind = yaml.ScalarNode
    88  var SequenceNode yaml.Kind = yaml.SequenceNode
    89  
    90  func nodeKindString(k yaml.Kind) string {
    91  	return map[yaml.Kind]string{
    92  		yaml.SequenceNode: "SequenceNode",
    93  		yaml.MappingNode:  "MappingNode",
    94  		yaml.ScalarNode:   "ScalarNode",
    95  		yaml.DocumentNode: "DocumentNode",
    96  		yaml.AliasNode:    "AliasNode",
    97  	}[k]
    98  }
    99  
   100  var DoubleQuotedStyle yaml.Style = yaml.DoubleQuotedStyle
   101  var FlowStyle yaml.Style = yaml.FlowStyle
   102  var FoldedStyle yaml.Style = yaml.FoldedStyle
   103  var LiteralStyle yaml.Style = yaml.LiteralStyle
   104  var SingleQuotedStyle yaml.Style = yaml.SingleQuotedStyle
   105  var TaggedStyle yaml.Style = yaml.TaggedStyle
   106  
   107  const (
   108  	MergeTag = "!!merge"
   109  )
   110  

View as plain text