...

Source file src/cuelang.org/go/encoding/yaml/yaml.go

Documentation: cuelang.org/go/encoding/yaml

     1  // Copyright 2019 The CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package yaml converts YAML encodings to and from CUE. When converting to CUE,
    16  // comments and position information are retained.
    17  package yaml
    18  
    19  import (
    20  	"bytes"
    21  	"io"
    22  
    23  	"cuelang.org/go/cue"
    24  	"cuelang.org/go/cue/ast"
    25  	cueyaml "cuelang.org/go/internal/encoding/yaml"
    26  	"cuelang.org/go/internal/third_party/yaml"
    27  	pkgyaml "cuelang.org/go/pkg/encoding/yaml"
    28  )
    29  
    30  // Extract parses the YAML specified by src to a CUE expression. If
    31  // there's more than one document, the documents will be returned as a
    32  // list. The src argument may be a nil, string, []byte, or io.Reader. If
    33  // src is nil, the result of reading the file specified by filename will
    34  // be used.
    35  func Extract(filename string, src interface{}) (*ast.File, error) {
    36  	a := []ast.Expr{}
    37  	d, err := yaml.NewDecoder(filename, src)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	for {
    42  		expr, err := d.Decode()
    43  		if err != nil {
    44  			if err != io.EOF {
    45  				return nil, err
    46  			}
    47  			if expr != nil {
    48  				a = append(a, expr)
    49  			}
    50  			break
    51  		}
    52  		a = append(a, expr)
    53  	}
    54  	f := &ast.File{Filename: filename}
    55  	switch len(a) {
    56  	case 0:
    57  	case 1:
    58  		switch x := a[0].(type) {
    59  		case *ast.StructLit:
    60  			f.Decls = x.Elts
    61  		default:
    62  			f.Decls = []ast.Decl{&ast.EmbedDecl{Expr: x}}
    63  		}
    64  	default:
    65  		f.Decls = []ast.Decl{&ast.EmbedDecl{Expr: &ast.ListLit{Elts: a}}}
    66  	}
    67  	return f, nil
    68  }
    69  
    70  // Decode converts a YAML file to a CUE value. Streams are returned as a list
    71  // of the streamed values.
    72  //
    73  // Deprecated: use Extract and build the File with cue.Context.BuildFile.
    74  func Decode(r *cue.Runtime, filename string, src interface{}) (*cue.Instance, error) {
    75  	file, err := Extract(filename, src)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	return r.CompileFile(file)
    80  }
    81  
    82  // Encode returns the YAML encoding of v.
    83  func Encode(v cue.Value) ([]byte, error) {
    84  	n := v.Syntax(cue.Final())
    85  	b, err := cueyaml.Encode(n)
    86  	return b, err
    87  }
    88  
    89  // EncodeStream returns the YAML encoding of iter, where consecutive values
    90  // of iter are separated with a `---`.
    91  func EncodeStream(iter cue.Iterator) ([]byte, error) {
    92  	// TODO: return an io.Reader and allow asynchronous processing.
    93  	buf := &bytes.Buffer{}
    94  	for i := 0; iter.Next(); i++ {
    95  		if i > 0 {
    96  			buf.WriteString("---\n")
    97  		}
    98  		n := iter.Value().Syntax(cue.Final())
    99  		b, err := cueyaml.Encode(n)
   100  		if err != nil {
   101  			return nil, err
   102  		}
   103  		buf.Write(b)
   104  	}
   105  	return buf.Bytes(), nil
   106  }
   107  
   108  // Validate validates the YAML and confirms it matches the constraints
   109  // specified by v. For YAML streams, all values must match v.
   110  func Validate(b []byte, v cue.Value) error {
   111  	_, err := pkgyaml.Validate(b, v)
   112  	return err
   113  }
   114  

View as plain text